⚡️ Speed up method Bundle.from_bokeh by 9%
#11
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.
📄 9% (0.09x) speedup for
Bundle.from_bokehinpanel/io/resources.py⏱️ Runtime :
129 microseconds→119 microseconds(best of282runs)📝 Explanation and details
The optimized code replaces inefficient
kwargs.pop()operations with direct dictionary key checks and deletions in the__init__method.Key optimization: Instead of using
kwargs.pop("key", default)which performs a dictionary lookup, retrieval, and deletion even when the key doesn't exist, the optimized version uses"key" in kwargschecks followed by direct access and deletion only when needed.Why this is faster: The original
pop()method has overhead from:The optimized approach eliminates unnecessary operations when keys are missing (which appears common based on test results), and uses more efficient direct dictionary operations when keys are present.
Performance characteristics: The optimization shows consistent 8-18% speedup across most test cases, with particularly good gains on:
The
from_bokehmethod remains unchanged, so the gains come purely from more efficient kwargs processing during Bundle initialization.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Bundle.from_bokeh-mh9xo1ywand push.