Closed qwhelan closed 1 month ago
@eprbell Of course, no rush. In the event it's useful, I've uploaded https://github.com/eprbell/rp2/pull/116 so you can check out the LIFO
/HIFO
changes as well. The latter can wait until this is merged if it over-complicates things.
@eprbell Sure, didn't realize GitHub made it hard to review. The force-pushing was mostly cleaning up early commits with issues I realized later, so I'll try to avoid going forward.
@eprbell Should be ready for final review/merge now
The
FIFO
accounting method is unfortunately quadratic time, even in trivial cases:The "One In, One Out" alternates
n
pairs ofBUY, SELL
such that there is only everk=1
active lots. This represents the most trivial case for any accounting method. A potential worst case is represented by "Buys, then Sells" which createsn
BUYS
followed byn
SELLS
. This creates a worst case scenario ofk=n
.In the first case, the
LIFO
method is roughly linear; in the second case, it is clearly quadratic. The quadratic behavior ofLIFO
andHIFO
will be addressed in a separate PR by usingheapq
.It is relatively straightforward to make
FIFO
linear time in both of these cases by simply advancing an index of the next, un-exhausted lot onAcquiredLotCandidates
and persisting this object between invocations ofget_acquired_lot_for_taxable_event()
. In the event of different accounting methods being used for different tax years, a one-time scan is performed for each switch of accounting method to find the appropriate index.With this PR,
FIFO
performance improves to linear time in both cases: