mirror of
https://github.com/we-promise/sure.git
synced 2026-05-07 12:54:04 +00:00
* Performance improvements in holding calculation pipeline
Investment accounts with large histories were pegging CPU at 100% during
sync. Root cause was a cluster of quadratic and superlinear algorithms in
the inner holding calculation loop. All are replaced with O(1) hash lookups
built from single-pass indexes over the already-loaded data.
Holding::PortfolioCache - load_prices:
Three O(SxN) patterns inside the per-security loop:
1. DB prices: `security.prices.where(...)` fired one SQL query per
security (N+1). Replaced with a single bulk query before the loop:
Security::Price.where(security_id: ..., date: ...).group_by(&:security_id)
70 securities -> 70 queries becomes 1.
2. Trade prices: `trades.select { |t| t.entryable.security_id == id }`
scanned the full trades array for every security - O(SxT). Replaced
with trades_by_security_id, pre-indexed once from the loaded array.
3. Holding prices: `holdings.select { |h| h.security_id == id }` - same
O(SxH) pattern. Replaced with holdings_by_security_id.
Prices are now indexed into prices_by_date and prices_by_date_and_source
hashes during load_prices, making get_price O(1) instead of scanning the
flat prices array on every lookup.
Holding::PortfolioCache - get_trades / get_price:
- get_trades(date:): `trades.select { |t| t.date == date }` (O(T) scan)
replaced with trades_by_date hash (O(1)).
- get_price: two `prices.select { p.date == date ... }.min_by` linear
scans replaced with direct hash lookups into prices_by_date and
prices_by_date_and_source.
Holding::PortfolioCache - collect_unique_securities:
`holdings.map(&:security)` traversed the security association on every
holding record (N+1 if not preloaded). Replaced with a pluck of
security_ids followed by a single Security.where(id: ...) batch load.
Holding::ForwardCalculator / ReverseCalculator:
`holdings += build_holdings(...)` allocated a new array copy on every
iteration - O(N) per day x thousands of days = O(D^2) total allocations.
Replaced with holdings.concat(...) which appends in place, O(1).
Holding::ReverseCalculator - precompute_cost_basis:
Old: walked every date from account.start_date to Date.current (O(D)),
writing a cost_basis entry for every security on every date. For an
account with 2 trades over 9,250 days this wrote ~18,500 hash entries
and consumed the full date range in the outer loop regardless of trade
density.
New: walks only buy trades (O(T)), appending one [date, avg_cost]
snapshot per trade. cost_basis_for binary-searches the sparse snapshot
array - O(log T) per lookup. Memory drops from O(DxS) to O(T).
Holding::Gapfillable:
`security_holdings.find { |h| h.date == date }` was called on every
date in the gapfill range - O(H) per date, O(HxD) total. Replaced with
security_holdings.index_by(:date) built once before the loop, making
each date lookup O(1).
Holding::Materializer - purge_stale_holdings:
`account.entries.trades.map { |entry| entry.entryable.security_id }.uniq`
loaded all trade entry records into Ruby then traversed the entryable
association on each (N+1). Replaced with account.trades.pluck(:security_id).uniq
(single SQL query returning only the IDs).
In testing, these changes were able to reduce sync time of an account with
25 years of history and 70 securities from about 90 minutes down to under
3 minutes.
* Lint fix
* Lint fix
* addressing the open review nits I agreed with:
* return dup'd arrays from PortfolioCache#get_trades so callers can't mutate memoized cache state
* use the precomputed security-id indexes in collect_unique_securities
* keep security-id dedupe in SQL via distinct.pluck(:security_id)
* tighten the DB price preload to select only needed columns
* harden cost-basis assertions with assert_in_delta
* Back out unnecessary AI slop
* Add back dup to trades array returned from memoized hash
trades_by_date[date] returns a live reference into the memoized hash.
Any caller that mutates the result would silently corrupt the cache for
subsequent calls on the same date within the same sync run. Add .dup to
return a shallow copy, matching the safety of the original select path.
169 lines
4.9 KiB
Ruby
169 lines
4.9 KiB
Ruby
class Holding::PortfolioCache
|
|
attr_reader :account, :use_holdings
|
|
|
|
class SecurityNotFound < StandardError
|
|
def initialize(security_id, account_id)
|
|
super("Security id=#{security_id} not found in portfolio cache for account #{account_id}. This should not happen unless securities were preloaded incorrectly.")
|
|
end
|
|
end
|
|
|
|
def initialize(account, use_holdings: false, security_ids: nil)
|
|
@account = account
|
|
@use_holdings = use_holdings
|
|
@security_ids = security_ids
|
|
load_prices
|
|
end
|
|
|
|
def get_trades(date: nil)
|
|
if date.blank?
|
|
trades
|
|
else
|
|
trades_by_date[date]&.dup || []
|
|
end
|
|
end
|
|
|
|
def get_price(security_id, date, source: nil)
|
|
security = @security_cache[security_id]
|
|
raise SecurityNotFound.new(security_id, account.id) unless security
|
|
|
|
price_with_priority = if source.present?
|
|
security[:prices_by_date_and_source][[ date, source ]]
|
|
else
|
|
security[:prices_by_date][date]
|
|
end
|
|
|
|
return nil unless price_with_priority
|
|
|
|
price = price_with_priority.price
|
|
return nil unless price
|
|
|
|
price_money = Money.new(price.price, price.currency)
|
|
|
|
begin
|
|
converted_amount = price_money.exchange_to(account.currency).amount
|
|
rescue Money::ConversionError
|
|
converted_amount = price.price
|
|
end
|
|
|
|
Security::Price.new(
|
|
security_id: security_id,
|
|
date: price.date,
|
|
price: converted_amount,
|
|
currency: account.currency
|
|
)
|
|
end
|
|
|
|
def get_securities
|
|
@security_cache.map { |_, v| v[:security] }
|
|
end
|
|
|
|
private
|
|
PriceWithPriority = Data.define(:price, :priority, :source)
|
|
|
|
def trades
|
|
@trades ||= account.entries.includes(entryable: :security).trades.chronological.to_a
|
|
end
|
|
|
|
def trades_by_date
|
|
@trades_by_date ||= trades.group_by(&:date)
|
|
end
|
|
|
|
def trades_by_security_id
|
|
@trades_by_security_id ||= trades.group_by { |t| t.entryable.security_id }
|
|
end
|
|
|
|
def holdings
|
|
@holdings ||= account.holdings.chronological.to_a
|
|
end
|
|
|
|
def holdings_by_security_id
|
|
@holdings_by_security_id ||= holdings.group_by(&:security_id)
|
|
end
|
|
|
|
def collect_unique_securities
|
|
ids = trades_by_security_id.keys
|
|
ids |= holdings_by_security_id.keys if use_holdings
|
|
ids &= @security_ids if @security_ids
|
|
|
|
Security.where(id: ids).to_a
|
|
end
|
|
|
|
# Loads all known prices for all securities in the account with priority based on source:
|
|
# 1 - DB or provider prices
|
|
# 2 - Trade prices
|
|
# 3 - Holding prices
|
|
def load_prices
|
|
@security_cache = {}
|
|
securities = collect_unique_securities
|
|
|
|
Rails.logger.info "Preloading #{securities.size} securities for account #{account.id}"
|
|
|
|
security_ids = securities.map(&:id)
|
|
|
|
# Bulk-load all DB prices for all securities in one query, grouped by security_id
|
|
db_prices_by_security_id = Security::Price
|
|
.where(security_id: security_ids, date: account.start_date..Date.current)
|
|
.group_by(&:security_id)
|
|
|
|
securities.each do |security|
|
|
Rails.logger.info "Loading security: ID=#{security.id} Ticker=#{security.ticker}"
|
|
|
|
# High priority prices from DB (synced from provider)
|
|
db_prices = (db_prices_by_security_id[security.id] || []).map do |price|
|
|
PriceWithPriority.new(
|
|
price: price,
|
|
priority: 1,
|
|
source: "db"
|
|
)
|
|
end
|
|
|
|
# Medium priority prices from trades
|
|
trade_prices = (trades_by_security_id[security.id] || [])
|
|
.map do |trade|
|
|
PriceWithPriority.new(
|
|
price: Security::Price.new(
|
|
security: security,
|
|
price: trade.entryable.price,
|
|
currency: trade.entryable.currency,
|
|
date: trade.date
|
|
),
|
|
priority: 2,
|
|
source: "trade"
|
|
)
|
|
end
|
|
|
|
# Low priority prices from holdings (if applicable)
|
|
holding_prices = if use_holdings
|
|
(holdings_by_security_id[security.id] || []).map do |holding|
|
|
PriceWithPriority.new(
|
|
price: Security::Price.new(
|
|
security: security,
|
|
price: holding.price,
|
|
currency: holding.currency,
|
|
date: holding.date
|
|
),
|
|
priority: 3,
|
|
source: "holding"
|
|
)
|
|
end
|
|
else
|
|
[]
|
|
end
|
|
|
|
all_prices = db_prices + trade_prices + holding_prices
|
|
|
|
# Index by date for O(1) lookup in get_price instead of O(N) linear scan
|
|
prices_by_date = all_prices.group_by { |p| p.price.date }
|
|
.transform_values { |ps| ps.min_by(&:priority) }
|
|
prices_by_date_and_source = all_prices.group_by { |p| [ p.price.date, p.source ] }
|
|
.transform_values { |ps| ps.min_by(&:priority) }
|
|
|
|
@security_cache[security.id] = {
|
|
security: security,
|
|
prices_by_date: prices_by_date,
|
|
prices_by_date_and_source: prices_by_date_and_source
|
|
}
|
|
end
|
|
end
|
|
end
|