-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Improve the performance of RollingWindow.GetEnumerator #8444
Improve the performance of RollingWindow.GetEnumerator #8444
Conversation
💪💪 Nice one @starteleport , drop us an email [email protected] for a gift of cloud credit. |
Thanks @jaredbroad! I wonder whether it's worth adding a performance benchmark for this case? There are not so many benchmarks now in the code base, hence the question. |
Nice work @starteleport! Could you please add some unit tests at
A regression algorithm? I think for this case because it's a micro optimization we would need a benchmark unit test like the ones you've shared but atm we don't have support for those, believe we would need to add a new benchmark project and CI for them, out of scope for this PR 👍 |
@Martin-Molinero, I would be happy to do so but sadly this method wouldn't get inlined even if I use |
@Martin-Molinero done, I reverted custom indexing logic and extended |
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.
Love it!
There's more to come 😄 |
Description
This optimises the
RollingWindow.GetEnumerator
method in terms of execution time and memory allocation.Related Issue
Closes #8443
Motivation and Context
The
RollingWindow.GetEnumerator
method allocates a new list for each invocation and copies values to the list via this[int], which in turn enters/exits reader lock for each invocation. I was able to speed up this method by more than 50% in terms of execution time and achieve marginally better memory consumption by switching fromList<T>
toT[]
and inlining the respective part ofthis[int]
.Many built-in indicators as well as some reasonable real-world use cases for RollingWindow would benefit from this change in backtesting/optimisation.
Requires Documentation Change
No
How Has This Been Tested?
Benchmark code
Several options were considered:
Baseline
: currentRollingWindow
implementation inmaster
ToArray
: changedList<T>
toT[]
ToArrayInlinedIndexer
: manually inlined the appropriate part of thethis[int]
indexerToArrayInlinedIndexerReversed
: changedfor
loop to the reversed version (iterating with counter being decremented)Benchmarking results for
RollingWindow
of size 100:Benchmarking results for
RollingWindow
of size 10:Types of changes
Checklist:
bug-<issue#>-<description>
orfeature-<issue#>-<description>