⚡️ Speed up function _use_gl by 52%
#118
Open
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.
📄 52% (0.52x) speedup for
_use_glinsrc/bokeh/embed/bundle.py⏱️ Runtime :
988 microseconds→650 microseconds(best of50runs)📝 Explanation and details
The optimization achieves a 52% speedup by eliminating Python function call overhead and generator inefficiencies through three key changes:
1. Replaced
any()+ generator with explicit loopany(query(x) for x in objs)creates a generator and delegates to Python's built-inany(), introducing overheadforloop with early return (return Trueon first match), eliminating generator creation and function call indirection2. Inlined the lambda function in
_use_gl_any()with a lambda that performedisinstance()and attribute access_any()3. Pre-computed local variables
Plottype and"webgl"string as local variables to avoid repeated lookups during iterationPerformance Impact Analysis:
Based on the function reference,
_use_gl()is called frombundle_for_objs_and_resources(), which appears to be in the critical path for rendering Bokeh visualizations. The 52% speedup is particularly valuable for:isinstance()check avoids function call overhead for each objectThe optimization maintains identical behavior while being especially effective for larger datasets where the function call and generator overhead becomes more pronounced.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/embed/test_bundle.py::Test__use_gl.test_with_glunit/bokeh/embed/test_bundle.py::Test__use_gl.test_without_gl🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_use_gl-mhw5x97aand push.