⚡️ Speed up method _Stack.push by 17%
#218
Open
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.
📄 17% (0.17x) speedup for
_Stack.pushinlib/matplotlib/cbook.py⏱️ Runtime :
1.78 milliseconds→1.52 milliseconds(best of115runs)📝 Explanation and details
The optimization achieves a 17% speedup by reducing object allocation and avoiding unnecessary list operations in the common case where elements are pushed to the end of the stack.
Key optimizations:
Conditional branching based on position: Instead of always using slice assignment
self._elements[self._pos + 1:] = [o], the code now checks if we're appending to the end (next_pos == len(self._elements)) versus inserting in the middle.Direct append for common case: When pushing to the end of the stack (which happens in 6053 out of 6060 calls according to the profiler), the code uses
self._elements.append(o)instead of slice assignment. This avoids creating a temporary list[o]and the overhead of slice assignment.Optimized middle insertion: For the rare case where elements need to be discarded (7 out of 6060 calls), the code uses direct assignment
self._elements[next_pos] = ofollowed bydel self._elements[next_pos + 1:], which is more efficient than slice assignment with a list.Performance characteristics from tests:
append()pathdeloperation, but this represents <1% of actual usageThe optimization is particularly effective because it optimizes for the 99.9% common case (sequential pushes) while only adding minimal overhead to the rare edge case (mid-stack insertions). This mirrors typical stack usage patterns where elements are mostly pushed sequentially to the end.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_Stack.push-misdgu9wand push.