-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make FIFO accounting_method linear time #115
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on accounting method performance: this was a known issue and it's great to have proposals in this area.
Accounting method management is a critical part of the engine, so I'm still going through the PR carefully to understand all the implications. This is just initial feedback (nits, mostly): as soon as I finish studying your code, I'd like to brainstorm this with you to make sure we're getting everything right before merging. So give me a few days and I'll post a follow up here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great improvement! I finally got some time to sit and study the change and I couldn't find any major issues with this approach. I have a few nits here and there, but this is almost ready to merge, I think.
One request: if possible, could you just git push future changes without force? Force makes reviewing a bit harder because I have to start from scratch every time (which is not a major problem with a small change like this, but still). Thanks!
@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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This is a really good improvement, thanks for doing it!
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: