⚡️ Speed up function _sphinx_type_seq by 6%
#137
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.
📄 6% (0.06x) speedup for
_sphinx_type_seqinsrc/bokeh/core/property/container.py⏱️ Runtime :
775 microseconds→728 microseconds(best of250runs)📝 Explanation and details
The optimization introduces memoization caching to the
property_linkfunction, which eliminates redundant string formatting operations for objects of the same class.Key Optimization:
_property_link_cachedictionary to store pre-computed string results per class typeWhy This Works:
The original code performed string formatting (
f":class:~bokeh.core.properties.{obj.class.name}\\ ") on every call, even for objects of the same class. String formatting with f-strings and attribute access (obj.__class__.__name__) has measurable overhead when called frequently. The cache eliminates this redundant work by storing the result after the first computation.Performance Impact:
property_linkcalls from 328ns to 214ns per hit on averageTest Case Performance:
The optimization performs best with repeated calls using the same types - test cases show 10-30% improvements for scenarios with common built-in types (int, str, float) and custom classes. Large-scale tests with 1000+ iterations show consistent 8-16% improvements, validating the caching strategy's effectiveness for high-volume documentation generation workloads.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_sphinx_type_seq-mhwiavjdand push.