mirror of
https://github.com/we-promise/sure.git
synced 2026-05-07 21:04:12 +00:00
* Performance improvements in balance sync cache
Balance::SyncCache#converted_holdings called account.holdings.map { |h| h.dup }
which duplicated every holding record into a new ActiveRecord object, converted
its currency, and stored the full object in a holdings_by_date array hash.
For an investment account with years of history this allocates 100,000+
AR objects on every sync - one per holding row - creating proportional GC
pressure that scaled with account age.
The only consumer of get_holdings(date) was BaseCalculator#holdings_value_for_date,
which immediately discarded the objects after calling .sum(&:amount). The
individual holding objects were never accessed for any other attribute.
Replace the dup-and-group approach with a single aggregation pass that stores
only the per-date sum:
holdings_value_by_date: account.holdings.each_with_object(Hash.new(0)) do |h, totals|
converted = Money.new(h.amount, h.currency).exchange_to(account.currency, date: h.date).amount
totals[h.date] += converted
end
Interface change: get_holdings(date) -> get_holdings_value(date) returns a
Numeric directly rather than an Array. BaseCalculator#holdings_value_for_date
is updated accordingly, and its own per-date memoization layer is removed
since holdings_value_by_date is already fully memoized at the SyncCache level.
* fall back to 1:1 rate in SyncCache when holding exchange rate is missing; update tests to use investment class
57 lines
1.6 KiB
Ruby
57 lines
1.6 KiB
Ruby
class Balance::SyncCache
|
|
def initialize(account)
|
|
@account = account
|
|
end
|
|
|
|
def get_valuation(date)
|
|
entries_by_date[date]&.find { |e| e.valuation? }
|
|
end
|
|
|
|
def get_holdings_value(date)
|
|
holdings_value_by_date[date] || 0
|
|
end
|
|
|
|
def get_entries(date)
|
|
entries_by_date[date]&.select { |e| e.transaction? || e.trade? } || []
|
|
end
|
|
|
|
private
|
|
attr_reader :account
|
|
|
|
def entries_by_date
|
|
@entries_by_date ||= converted_entries.group_by(&:date)
|
|
end
|
|
|
|
def holdings_value_by_date
|
|
@holdings_value_by_date ||= account.holdings.each_with_object(Hash.new(0)) do |h, totals|
|
|
begin
|
|
converted = Money.new(h.amount, h.currency).exchange_to(account.currency, date: h.date).amount
|
|
rescue Money::ConversionError
|
|
converted = h.amount # fallback to 1:1 conversion rate if exchange rate unavailable
|
|
end
|
|
totals[h.date] += converted
|
|
end
|
|
end
|
|
|
|
def converted_entries
|
|
@converted_entries ||= account.entries.excluding_split_parents.includes(:entryable).order(:date).to_a.map do |e|
|
|
converted_entry = e.dup
|
|
|
|
# Extract custom exchange rate if present on Transaction
|
|
custom_rate = if e.entryable.is_a?(Transaction)
|
|
e.entryable.extra&.dig("exchange_rate")
|
|
end
|
|
|
|
# Use Money#exchange_to with custom rate if available, standard lookup otherwise
|
|
converted_entry.amount = converted_entry.amount_money.exchange_to(
|
|
account.currency,
|
|
date: e.date,
|
|
custom_rate: custom_rate
|
|
).amount
|
|
|
|
converted_entry.currency = account.currency
|
|
converted_entry
|
|
end
|
|
end
|
|
end
|