⚡️ Speed up method GridPlot._check_repeated_layout_children by 29%
#132
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.
📄 29% (0.29x) speedup for
GridPlot._check_repeated_layout_childreninsrc/bokeh/models/plots.py⏱️ Runtime :
12.8 microseconds→9.98 microseconds(best of9runs)📝 Explanation and details
The optimized version achieves a 28% speedup by replacing an inefficient duplicate detection algorithm with a more efficient early-exit approach.
Key optimizations:
Eliminated unnecessary list creation: The original code created an intermediate list
[child[0] for child in self.children]before checking for duplicates. The optimized version uses a generator expression(item[0] for item in children)that produces elements on-demand.Early exit duplicate detection: Instead of creating a complete set and comparing lengths, the optimization uses an iterative approach with a
seenset that immediately returns upon finding the first duplicate. This is significantly faster when duplicates exist early in the list.Added early return for empty children: The optimized version checks if
childrenis empty upfront and returns immediately, avoiding unnecessary computation.Why this is faster:
len()calls and set conversion of the entire listPerformance characteristics:
Based on the test results, the optimization shows consistent 23-38% improvements across different scenarios:
This optimization is especially valuable in Bokeh's layout validation pipeline, where grid plots may contain many child elements and duplicate detection is part of the validation workflow.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-GridPlot._check_repeated_layout_children-mhwdzl4eand push.