From 8945efaf107dfb2a7e73fd34ff7c6c67d73a0cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20K=C3=A4chele?= Date: Thu, 12 Dec 2024 10:54:04 +0100 Subject: [PATCH] Start using an object pool for shipments --- lib/planet_express_express.rb | 2 ++ lib/planet_express_express/shipment.rb | 10 +++++++ lib/planet_express_express/shipment_pool.rb | 24 +++++++++++++++++ lib/planet_express_express/shipment_reader.rb | 27 ++++++++----------- 4 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 lib/planet_express_express/shipment.rb create mode 100644 lib/planet_express_express/shipment_pool.rb diff --git a/lib/planet_express_express.rb b/lib/planet_express_express.rb index 3c7b1ab..205ec02 100644 --- a/lib/planet_express_express.rb +++ b/lib/planet_express_express.rb @@ -2,6 +2,8 @@ require_relative "planet_express_express/version" require_relative "planet_express_express/constants" +require_relative "planet_express_express/shipment" +require_relative "planet_express_express/shipment_pool" require_relative "planet_express_express/shipment_reader" require_relative "planet_express_express/rules" require_relative "planet_express_express/discount_budget" diff --git a/lib/planet_express_express/shipment.rb b/lib/planet_express_express/shipment.rb new file mode 100644 index 0000000..23fa95b --- /dev/null +++ b/lib/planet_express_express/shipment.rb @@ -0,0 +1,10 @@ +module PlanetExpressExpress + Shipment = Struct.new( + :date, + :size, + :provider, + :price, + :discount, + keyword_init: true + ) +end diff --git a/lib/planet_express_express/shipment_pool.rb b/lib/planet_express_express/shipment_pool.rb new file mode 100644 index 0000000..5ac4c5b --- /dev/null +++ b/lib/planet_express_express/shipment_pool.rb @@ -0,0 +1,24 @@ +module PlanetExpressExpress + # A very simple implementation of an object pool for + # shipments. + # + # Using it you can reduce the memory allocations + class ShipmentPool + def initialize(size: 100) + @entries = [] + size.times do + @entries.push(Shipment.new()) + end + end + + def get() + entry = @entries.pop + return entry if entry + Shipment.new + end + + def put_back(shipment_entries) + @entries.push(*shipment_entries) + end + end +end diff --git a/lib/planet_express_express/shipment_reader.rb b/lib/planet_express_express/shipment_reader.rb index f04d583..a83f9b0 100644 --- a/lib/planet_express_express/shipment_reader.rb +++ b/lib/planet_express_express/shipment_reader.rb @@ -1,15 +1,8 @@ module PlanetExpressExpress require "date" class ShipmentReader - Shipment = Struct.new( - :date, - :size, - :provider, - :price, - :discount, - keyword_init: true - ) - def initialize(prices) + def initialize(prices, shipment_pool: ShipmentPool.new(size: 10)) + @shipment_pool = shipment_pool @prices = prices @id = 0 end @@ -31,6 +24,8 @@ module PlanetExpressExpress if shipment.date.year != current_year || shipment.date.month != current_month yield shipments + + @shipment_pool.put_back(shipments[:all]) shipments.each do |_,v| v.clear end @@ -52,13 +47,13 @@ module PlanetExpressExpress def parse(line) parts = line.split(" ") - shipment = Shipment.new( - date: Date.strptime(parts[0], "%Y-%m-%d"), - size: parts[1]&.to_sym, - provider: parts[2]&.to_sym, - price: 0, - discount: 0 - ) + shipment = @shipment_pool.get + + shipment.date = Date.strptime(parts[0], "%Y-%m-%d") + shipment.size = parts[1]&.to_sym + shipment.provider = parts[2]&.to_sym + shipment.price = 0 + shipment.discount = 0 shipment.price = prices[ [