⚡️ Speed up function make_increasing_ohlc
by 102%
#106
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.
📄 102% (1.02x) speedup for
make_increasing_ohlc
inplotly/figure_factory/_ohlc.py
⏱️ Runtime :
2.12 milliseconds
→1.05 milliseconds
(best of380
runs)📝 Explanation and details
Here’s the optimized version of your program. The bulk of the runtime is spent in
utils.flatten
, so the best speedup comes from avoiding repeated flattening and instead building the already-flat data in-place while separating the increasing sticks. This avoids both the intermediate nested lists and the overhead offlatten
. You can also avoid storing and separating decrease data, since only the increasing sticks are relevant. All other logic and signatures are left unchanged.All required comments are preserved.
Key optimizations and details:
flat_increase_x
andflat_increase_y
in one pass while processing the data.This should be much faster, especially for large data. Let me know if you have performance constraints around decreasing traces (they can be optimized similarly if needed).
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-make_increasing_ohlc-mb2bo46m
and push.