⚡️ Speed up method Document.select_one by 8%
#64
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.
📄 8% (0.08x) speedup for
Document.select_oneinsrc/bokeh/document/document.py⏱️ Runtime :
18.2 microseconds→16.9 microseconds(best of43runs)📝 Explanation and details
The optimized code improves the
select_onemethod by adding a fast path for name-based queries, avoiding unnecessary overhead from the genericselectmethod.Key optimization: When
select_onereceives a simple name selector (e.g.,{"name": "mycircle"}), it now directly callsself.models.get_all_by_name()instead of going through the more generalselect()method and converting its result to a list.Why this is faster:
select()and itslist()conversionget_all_by_name()is already optimized for name-based lookups with internal indexingPerformance characteristics: The line profiler shows the optimization works best for name-based queries (which appear to be common in the test cases), reducing time spent in
select_onefrom ~58μs to ~35μs. The 7% overall speedup indicates this fast path is frequently used, making it a worthwhile micro-optimization for a core document querying method that's likely called frequently in Bokeh applications.The optimization maintains identical behavior and error handling while providing a direct path for the most common query pattern.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/document/test_document.py::TestDocument.test_selectunit/bokeh/model/test_model.py::test_select🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Document.select_one-mhb530dwand push.