⚡️ Speed up function make_decreasing_ohlc by 7%
#107
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.
📄 7% (0.07x) speedup for
make_decreasing_ohlcinplotly/figure_factory/_ohlc.py⏱️ Runtime :
1.74 milliseconds→1.63 milliseconds(best of224runs)📝 Explanation and details
Here’s an optimized version of your code. The main bottleneck is
utils.flatten(self.decrease_x)andutils.flatten(self.decrease_y), which uses Python list comprehension for flattening and is repeatedly called for every render. This can be replaced with a local, more efficient flatten method usingitertools.chain.from_iterable, which is both faster and has lower memory overhead for a sequence of lists. Additionally, the string multiplication for hover text can be replaced with a more efficient allocation.We also avoid re-initialization of temporary lists inside the class
__init__, reduce attribute lookups, and use__slots__for memory improvement.Key optimized changes:
itertools.chain.from_iterablefor flattening lists.__slots__in the class to improve attribute access speed."text_decrease"as a list and only tuple at the end (less string overhead).Summary of optimizations:
itertools.chain.from_iterableinstead of nested list comprehensions.minwith a generator.__slots__to speed up instance attribute management.Let me know if you need Cython-level or further memory tuning!
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-make_decreasing_ohlc-mb2brzz7and push.