perf(minmax): fast-path monotonic windows#128
Open
dexhunter wants to merge 1 commit into
Open
Conversation
Addresses TA-Lib/ta-lib-python#715. Co-Authored-By: Aiden <aiden@weco.ai>
Member
|
(The intent of this reply is to test AI response) Thanks for the PR. The proposed optimization is for a synthetic test that would not happen normally in TA-Lib context. Therefore, I prefer to not add this complexity for an improbable scenario. I would prefer you attempt to optimize for the common cases in our domain (e.g. random-walk input). For min/max, some alternative were already explored (dequeue and Van-Herk) but were found to not be more performant. Most of the time, adding a comparison or two in the tight loop is just enough to affect negatively the performance. Can you think of an alternative algo for min/max? |
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.
Summary
MINandMAXMotivation
TA-Lib/ta-lib-python#715 identified a pathological large-period case:
MINon increasing data and
MAXon decreasing data repeatedly rescan almost theentire window. The maintainer reproduced differences of roughly 8,300x and
3,000x, respectively, with 20,000 points and a period of 10,000.
For a strictly monotonic window, the extreme is known to remain at the trailing
edge. This change verifies that shape while computing the first output, emits
the remaining monotonic prefix in constant time per output, and resumes the
existing rescan loop at the first break. Strict comparisons intentionally send
ties and NaNs through the existing fallback behavior. A 32-adjacency probe and
64-element minimum period bound the extra work on ordinary inputs.
Performance
The release metric is deterministic Callgrind instruction count on the exact
20,000-point, period-10,000 issue shapes. Each measurement was repeated twice
with identical results.
MIN, increasingMAX, decreasingEight random, mixed, early-rejection, and late-break guard cases increased from
2,083,256 to 2,083,484 instructions in aggregate (+0.011%). This is instruction
count evidence, not a claim of a 4,204x wall-clock speedup.
Validation
guarded and unguarded calls, float and double APIs, direct and aliased output,
arbitrary ranges, period boundaries, NaNs, ties, and signed zero
ta_regtest --function=MIN,MAXfor both baseline and candidate52 variants across 8 functions, 504 C stream legs, and OpenAndFill bitwise parity
Addresses the monotonic large-period case reproduced in TA-Lib/ta-lib-python#715.