⚡️ Speed up function _ext_use_mathjax by 2,554%
#119
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.
📄 2,554% (25.54x) speedup for
_ext_use_mathjaxinsrc/bokeh/embed/bundle.py⏱️ Runtime :
86.8 milliseconds→3.27 milliseconds(best of250runs)📝 Explanation and details
The optimization significantly improves performance by precomputing a lookup map that groups models by their top-level package name, eliminating a costly nested loop structure.
Key optimization:
all_objs, the code iterated through ALL models inHasProps.model_class_reverse_map.values()(~15,000+ models based on profiler data) and checkedmodel.__module__.startswith(name)for each onemodel_mapkeyed by top-level package name, so the inner loop only processes relevant models for each packagePerformance impact from profiler data:
startswithcheck)_query_extensions(37x faster)Why this works:
startswith()check on every model is expensive when repeated across thousands of modelsmodel_map.get(name, ())is O(1) vs O(N) iterationImpact on workloads:
Based on
function_references, this function is called by_use_mathjax()which determines MathJax requirements for Bokeh document embedding. The optimization particularly benefits scenarios with:The optimization maintains identical behavior while dramatically reducing computational complexity from O(objects × total_models) to O(objects + total_models).
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_ext_use_mathjax-mhw6azz9and push.