⚡️ Speed up function _sphinx_type_link by 9%#168
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization introduces an **LRU cache** to avoid redundant string formatting operations in the `property_link` function. The key changes are:
**What was optimized:**
- **Extracted string formatting logic** into a separate cached function `_property_link_class_name(cls: type)` decorated with `@lru_cache(maxsize=128)`
- **Changed `obj.__class__` to `type(obj)`** for more direct type retrieval
- **Cached the expensive string interpolation** `f":class:`~bokeh.core.properties.{cls.__name__}`\\ "` based on class type
**Why this improves performance:**
The original code performed string formatting on every call to `property_link`, even when the same class types were processed repeatedly. With the LRU cache, identical class types only compute the formatted string once and reuse the cached result. Since Sphinx documentation generation typically processes many objects of the same types (like `Required`, `int`, `str`, etc.), this caching provides significant benefit.
**Performance impact:**
- **9% overall speedup** (663μs → 608μs runtime)
- **Per-hit improvement** in `property_link`: 340.4ns → 325.1ns per call (4.5% faster per call)
- The line profiler shows similar hit counts but reduced total time, confirming the caching benefit
**Test case effectiveness:**
The optimization excels with **repeated class types** - test cases with builtin types (`int`, `str`, `list`, `dict`) and identical custom classes show 10-40% speedups. The cache is less beneficial for unique class types (some tests show slight slowdowns due to cache overhead), but the overall workload benefits since documentation generation typically involves many instances of common property types.
The `maxsize=128` limit prevents unbounded memory growth while accommodating the typical variety of property classes in Bokeh's documentation generation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
_sphinx_type_linkinsrc/bokeh/core/property/required.py⏱️ Runtime :
663 microseconds→608 microseconds(best of250runs)📝 Explanation and details
The optimization introduces an LRU cache to avoid redundant string formatting operations in the
property_linkfunction. The key changes are:What was optimized:
_property_link_class_name(cls: type)decorated with@lru_cache(maxsize=128)obj.__class__totype(obj)for more direct type retrievalf":class:~bokeh.core.properties.{cls.name}\\ "based on class typeWhy this improves performance:
The original code performed string formatting on every call to
property_link, even when the same class types were processed repeatedly. With the LRU cache, identical class types only compute the formatted string once and reuse the cached result. Since Sphinx documentation generation typically processes many objects of the same types (likeRequired,int,str, etc.), this caching provides significant benefit.Performance impact:
property_link: 340.4ns → 325.1ns per call (4.5% faster per call)Test case effectiveness:
The optimization excels with repeated class types - test cases with builtin types (
int,str,list,dict) and identical custom classes show 10-40% speedups. The cache is less beneficial for unique class types (some tests show slight slowdowns due to cache overhead), but the overall workload benefits since documentation generation typically involves many instances of common property types.The
maxsize=128limit prevents unbounded memory growth while accommodating the typical variety of property classes in Bokeh's documentation generation.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_sphinx_type_link-mhx0f92hand push.