⚡️ Speed up method Figure.for_each_mapbox by 513%
#75
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.
📄 513% (5.13x) speedup for
Figure.for_each_mapboxinplotly/graph_objs/_figure.py⏱️ Runtime :
707 microseconds→115 microseconds(best of5runs)📝 Explanation and details
The optimization adds a fast path for the most common case in
select_mapboxes()when no filtering criteria are specified (selector=None, row=None, col=None).Key Changes:
_select_layout_subplots_by_prefix()method, it directly accessesself.layoutand filters mapbox keys with a list comprehension(layout[k] for k in mapbox_keys)instead of going through the complex generic selection machineryWhy it's faster:
The original code always calls
_select_layout_subplots_by_prefix(), which involves generic parameter validation, complex filtering logic, and multiple function calls. The optimized version bypasses this entirely for the common "select all" case, using simple string prefix matching and direct dictionary access instead.Performance impact:
select_mapboxes()time reduced from 2.19ms to 0.64ms (71% faster)for_each_mapbox()runtime improved from 707μs to 115μs (512% speedup)Test case benefits:
This optimization is particularly effective for test cases with no filtering criteria, as shown in the annotated tests where all cases achieved 500%+ speedup. The optimization maintains identical behavior while dramatically improving performance for the most common usage pattern of selecting all mapbox objects without any filters.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Figure.for_each_mapbox-mhg1gsywand push.