Perf: make carl_sta_trig O(n) regardless of window size - #3724
Open
jberg5 wants to merge 5 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
The previous implementation of
carl_sta_trigon an array ofnelements over a window sizemwasO(nm). For every position in an output array, we have to compute the sum across a window ofmelements.The "trick" we can take advantage of here is that by computing the cumulative sum of the original array once, the window sum (for any window size!) can be computed in constant time by subtracting the value of the cumsum array at the end of the window from the value of the cumsum array at the start of the window.
To walk through an example:
For larger window sizes, the vectorization + O(n) change produces a pronounced speedup:
AI used?
I asked claude code to do some profiling on common use cases and look for python loops that could be vectorized. It flagged this pretty quickly and identified that cumsum could be a big win here. I've had to modify its output a bunch to get rid of useless extra features and make it feel more idiomatic. Also it seems to love getting rid of original code comments, even though they're very helpful.
I also used it a lot for writing and running benchmarking scripts both locally on my macbook and on a gcloud vm.
PR Checklist
masterfor new features,maintenance_?.?.xfor bug fixesCHANGELOG.txt(only obsolete if fixing a bug introduced after the last release)ready for reviewlabel when you the PR is ready to be reviewedRare actions items:
CONTRIBUTORS.txtCODEOWNERSwith your github handleIssue labels
The PR can be flagged with the following "issue labels" to alter CI runs:
no_cito skip CI builds while work-in-progressbuild_docsto trigger automatic docs build to see how docs render for the PRtest_networkto include "networked" tests if the PR touches any tests marked as "network"upload_imagesto attach plots generated in tests as artifacts to the CI run