Start using an object pool for shipments
This commit is contained in:
parent
e91e12c7ea
commit
8945efaf10
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
require_relative "planet_express_express/version"
|
require_relative "planet_express_express/version"
|
||||||
require_relative "planet_express_express/constants"
|
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/shipment_reader"
|
||||||
require_relative "planet_express_express/rules"
|
require_relative "planet_express_express/rules"
|
||||||
require_relative "planet_express_express/discount_budget"
|
require_relative "planet_express_express/discount_budget"
|
||||||
|
10
lib/planet_express_express/shipment.rb
Normal file
10
lib/planet_express_express/shipment.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module PlanetExpressExpress
|
||||||
|
Shipment = Struct.new(
|
||||||
|
:date,
|
||||||
|
:size,
|
||||||
|
:provider,
|
||||||
|
:price,
|
||||||
|
:discount,
|
||||||
|
keyword_init: true
|
||||||
|
)
|
||||||
|
end
|
24
lib/planet_express_express/shipment_pool.rb
Normal file
24
lib/planet_express_express/shipment_pool.rb
Normal file
@ -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
|
@ -1,15 +1,8 @@
|
|||||||
module PlanetExpressExpress
|
module PlanetExpressExpress
|
||||||
require "date"
|
require "date"
|
||||||
class ShipmentReader
|
class ShipmentReader
|
||||||
Shipment = Struct.new(
|
def initialize(prices, shipment_pool: ShipmentPool.new(size: 10))
|
||||||
:date,
|
@shipment_pool = shipment_pool
|
||||||
:size,
|
|
||||||
:provider,
|
|
||||||
:price,
|
|
||||||
:discount,
|
|
||||||
keyword_init: true
|
|
||||||
)
|
|
||||||
def initialize(prices)
|
|
||||||
@prices = prices
|
@prices = prices
|
||||||
@id = 0
|
@id = 0
|
||||||
end
|
end
|
||||||
@ -31,6 +24,8 @@ module PlanetExpressExpress
|
|||||||
|
|
||||||
if shipment.date.year != current_year || shipment.date.month != current_month
|
if shipment.date.year != current_year || shipment.date.month != current_month
|
||||||
yield shipments
|
yield shipments
|
||||||
|
|
||||||
|
@shipment_pool.put_back(shipments[:all])
|
||||||
shipments.each do |_,v|
|
shipments.each do |_,v|
|
||||||
v.clear
|
v.clear
|
||||||
end
|
end
|
||||||
@ -52,13 +47,13 @@ module PlanetExpressExpress
|
|||||||
def parse(line)
|
def parse(line)
|
||||||
parts = line.split(" ")
|
parts = line.split(" ")
|
||||||
|
|
||||||
shipment = Shipment.new(
|
shipment = @shipment_pool.get
|
||||||
date: Date.strptime(parts[0], "%Y-%m-%d"),
|
|
||||||
size: parts[1]&.to_sym,
|
shipment.date = Date.strptime(parts[0], "%Y-%m-%d")
|
||||||
provider: parts[2]&.to_sym,
|
shipment.size = parts[1]&.to_sym
|
||||||
price: 0,
|
shipment.provider = parts[2]&.to_sym
|
||||||
discount: 0
|
shipment.price = 0
|
||||||
)
|
shipment.discount = 0
|
||||||
|
|
||||||
shipment.price = prices[
|
shipment.price = prices[
|
||||||
[
|
[
|
||||||
|
Loading…
Reference in New Issue
Block a user