⚡️ Speed up method Figure.select_coloraxes by 332%
#74
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.
📄 332% (3.32x) speedup for
Figure.select_coloraxesinplotly/graph_objs/_figure.py⏱️ Runtime :
181 microseconds→42.0 microseconds(best of6runs)📝 Explanation and details
The optimization significantly improves the performance of the
_select_layout_subplots_by_prefixmethod by eliminating inefficient filtering chains and reducing redundant operations.Key optimizations applied:
Pre-filtering before sorting: Instead of sorting all layout keys and then filtering, the code now first filters to only keys starting with the prefix and having non-None values (
filtered_keys). This reduces the input size to_natural_sort_stringsby ~73% (from all layout keys to just relevant ones).Eliminated redundant filter chain: The original code used
functools.reduceto chain 4 lambda functions that filtered the same keys multiple times. The optimized version does a single pass with explicit conditional logic, avoiding the overhead of multiplefilter()calls and lambda invocations.Reduced dictionary lookups: The original code called
container_to_row_col.get()for every key in every filter, even when row/col filtering wasn't needed. The optimized version only builds and uses the mapping whencontainer_to_row_col is not None.Eliminated duplicate list creation: The original code converted
self.layoutto a list twice - once for sorting and once for the final list comprehension. The optimized version works with the pre-filtered keys throughout.Performance impact by test case:
The line profiler shows the optimization reduced the most expensive operations from 99% of runtime (sorting + list comprehension) to 96% (pre-filtering + sorting), with a 3x+ speedup overall.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Figure.select_coloraxes-mhg080xcand push.