⚡️ Speed up method Figure.select_polars by 320%
#76
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.
📄 320% (3.20x) speedup for
Figure.select_polarsinplotly/graph_objs/_figure.py⏱️ Runtime :
183 microseconds→43.5 microseconds(best of5runs)📝 Explanation and details
The optimized code improves performance by pre-filtering layout keys before expensive operations. The key optimization is in the
_select_layout_subplots_by_prefixmethod:What was optimized:
prefix_filtered_keys = [k for k in self.layout if k.startswith(prefix) and self.layout[k] is not None]to pre-filter keys oncereducecall to uselayout_keys_filters[1:](skipping the first filter since it's already applied)_natural_sort_stringsto the smaller pre-filtered set instead of the full layoutWhy this is faster:
_natural_sort_stringsnow operates on a much smaller dataset (only keys matching the prefix) rather than all layout keysreducechainPerformance characteristics:
_natural_sort_stringscall dropped from 466,725ns to 162,970ns (65% reduction)This optimization excels when selecting specific subplot types (like "polar") from figures with many different layout elements, as demonstrated by the 320% speedup in the test cases.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Figure.select_polars-mhg1oey5and push.