⚡️ Speed up function _ext_use_mathjax by 9,805%
#71
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,805% (98.05x) speedup for
_ext_use_mathjaxinsrc/bokeh/embed/bundle.py⏱️ Runtime :
41.8 milliseconds→422 microseconds(best of116runs)📝 Explanation and details
The optimization eliminates a massive performance bottleneck in the original code by caching model lookups and avoiding redundant iterations.
What was optimized:
_module_models_map) that groups models by their top-level module name, stored as a function attributeHasProps.model_class_reverse_map.values()for every unique module name (causing O(n×m) complexity), the optimized version directly looks up only the relevant models for each moduleKey performance gains:
startswith()checks (61.2% of total time)module_models_map.get(name)Why this works:
all_objstest_large_set_with_duplicate_module_namesshowing 113% speedup)Best for: Scenarios with large numbers of objects sharing common module prefixes, where the original's redundant full-registry scans become prohibitively expensive.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_ext_use_mathjax-mhb7l5kkand push.