⚡️ Speed up function _make_getset_interval by 15%
#225
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.
📄 15% (0.15x) speedup for
_make_getset_intervalinlib/matplotlib/axis.py⏱️ Runtime :
53.6 microseconds→46.5 microseconds(best of135runs)📝 Explanation and details
The optimized version achieves a 15% speedup through three key optimizations that reduce redundant attribute lookups and function call overhead:
1. Eliminated redundant attribute chains: The original code performed
getattr(getattr(self.axes, lim_name), attr_name)which requires two separate attribute lookups. The optimized version caches the intermediatelimobject withlim = getattr(self.axes, lim_name_str), then usesgetattr(lim, attr_name_str), reducing the attribute traversal overhead.2. Removed recursive function calls: The original setter used recursive calls to itself (
setter(self, min(...), max(...), ignore=True)), which incurs function call overhead and stack frame creation. The optimized version directly callssetattr(lim, attr_name_str, (...))with the computed tuple, eliminating this overhead entirely.3. Pre-computed string references: While the original explanation mentioned this optimization, the actual performance benefit comes from the reduced attribute chain traversals rather than the string variable assignments themselves.
Performance characteristics: The test results show consistent 12-22% improvements across all scenarios, with the largest gains (17-22%) occurring in complex setter operations involving the
ignore=Falsepath where the recursive call elimination has the most impact. Even simple operations likeignore=Truepaths see 15-19% improvements due to the reduced attribute lookups.Impact on workloads: Since this is a factory function that creates getter/setter pairs for matplotlib axis intervals, the optimization benefits any code that frequently reads or updates plot limits - a common operation in data visualization workflows. The elimination of recursive calls also improves stack efficiency for applications that perform many rapid limit adjustments.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_make_getset_interval-miyi8jmband push.