⚡️ Speed up method PropertyValueList.__iadd__ by 16%#163
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up method PropertyValueList.__iadd__ by 16%#163codeflash-ai[bot] wants to merge 1 commit into
PropertyValueList.__iadd__ by 16%#163codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimization achieves a 15% speedup by eliminating Python's `super()` method resolution overhead through two key changes: **1. Direct parent class calls instead of `super()`:** - In `__init__()`: Replaced `super().__init__(*args, **kwargs)` with explicit calls to `list.__init__(self, *args, **kwargs)` and `PropertyValueContainer.__init__(self)` - In `__iadd__()`: Replaced `super().__iadd__(y)` with `list.__iadd__(self, y)` **2. Added `__slots__ = ()` to prevent `__dict__` creation:** - Saves memory overhead for instances that don't need additional attributes - Provides slight performance improvement for attribute access **Why this is faster:** The `super()` function in Python performs Method Resolution Order (MRO) lookup at runtime, which involves traversing the inheritance hierarchy to find the correct method. For simple multiple inheritance like `PropertyValueList(PropertyValueContainer, list[T])`, this adds unnecessary overhead when we know exactly which parent method to call. Direct method calls bypass this resolution entirely. **Performance context:** Based on the test results, the optimization is most effective for basic list operations like `test_iadd_inplace_identity()` which showed the 15.6% improvement. The speedup is particularly valuable since `__iadd__` is a fundamental list operation that could be called frequently in data manipulation workloads. **Memory benefit:** The `__slots__ = ()` declaration prevents Python from creating a `__dict__` for each instance, reducing memory footprint when many `PropertyValueList` instances are created, which is common in property-heavy frameworks like Bokeh.
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.
📄 16% (0.16x) speedup for
PropertyValueList.__iadd__insrc/bokeh/core/property/wrappers.py⏱️ Runtime :
1.98 microsecondss→1.72 microsecondss(best of250runs)📝 Explanation and details
The optimization achieves a 15% speedup by eliminating Python's
super()method resolution overhead through two key changes:1. Direct parent class calls instead of
super():__init__(): Replacedsuper().__init__(*args, **kwargs)with explicit calls tolist.__init__(self, *args, **kwargs)andPropertyValueContainer.__init__(self)__iadd__(): Replacedsuper().__iadd__(y)withlist.__iadd__(self, y)2. Added
__slots__ = ()to prevent__dict__creation:Why this is faster:
The
super()function in Python performs Method Resolution Order (MRO) lookup at runtime, which involves traversing the inheritance hierarchy to find the correct method. For simple multiple inheritance likePropertyValueList(PropertyValueContainer, list[T]), this adds unnecessary overhead when we know exactly which parent method to call. Direct method calls bypass this resolution entirely.Performance context:
Based on the test results, the optimization is most effective for basic list operations like
test_iadd_inplace_identity()which showed the 15.6% improvement. The speedup is particularly valuable since__iadd__is a fundamental list operation that could be called frequently in data manipulation workloads.Memory benefit:
The
__slots__ = ()declaration prevents Python from creating a__dict__for each instance, reducing memory footprint when manyPropertyValueListinstances are created, which is common in property-heavy frameworks like Bokeh.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-PropertyValueList.__iadd__-mhwy5bj3and push.