⚡️ Speed up method PropertyValueList._saved_copy by 47%#162
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up method PropertyValueList._saved_copy by 47%#162codeflash-ai[bot] wants to merge 1 commit into
PropertyValueList._saved_copy by 47%#162codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimization replaces `list(self)` with `self.copy()` in the `_saved_copy` method, achieving a **47% speedup** by leveraging Python's built-in list copying mechanism more efficiently. **Key optimization:** - Changed `return list(self)` to `return self.copy()` - `self.copy()` is a direct method call on the list object that bypasses the overhead of the `list()` constructor - The `list()` constructor has to perform argument validation, type checking, and generic sequence processing, while `copy()` is optimized specifically for list shallow copying **Performance benefits:** - **Small lists (1-5 elements)**: 13-36% faster, reducing per-hit time from ~1460ns to ~1064ns - **Large lists (1000 elements)**: 55-78% faster, with the most dramatic improvements on lists with repeated elements or simple types - Consistent speedup across all test cases, from simple integers to complex nested structures **Why this works:** Since `PropertyValueList` inherits from `list[T]`, it gets the built-in `copy()` method which directly calls the C-level list copying implementation. This avoids the Python-level overhead of `list()` constructor's generic iterable processing, argument parsing, and temporary object creation. **Impact on workloads:** This optimization is particularly beneficial for: - Frequent property value copying in Bokeh's property system - Large lists or lists with many elements - Any scenario where `_saved_copy` is called repeatedly, as the per-call overhead reduction compounds significantly The optimization maintains identical shallow copy semantics - both approaches create independent list objects that share references to the same nested objects, ensuring no behavioral changes.
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.
📄 47% (0.47x) speedup for
PropertyValueList._saved_copyinsrc/bokeh/core/property/wrappers.py⏱️ Runtime :
59.2 microseconds→40.2 microseconds(best of250runs)📝 Explanation and details
The optimization replaces
list(self)withself.copy()in the_saved_copymethod, achieving a 47% speedup by leveraging Python's built-in list copying mechanism more efficiently.Key optimization:
return list(self)toreturn self.copy()self.copy()is a direct method call on the list object that bypasses the overhead of thelist()constructorlist()constructor has to perform argument validation, type checking, and generic sequence processing, whilecopy()is optimized specifically for list shallow copyingPerformance benefits:
Why this works:
Since
PropertyValueListinherits fromlist[T], it gets the built-incopy()method which directly calls the C-level list copying implementation. This avoids the Python-level overhead oflist()constructor's generic iterable processing, argument parsing, and temporary object creation.Impact on workloads:
This optimization is particularly beneficial for:
_saved_copyis called repeatedly, as the per-call overhead reduction compounds significantlyThe optimization maintains identical shallow copy semantics - both approaches create independent list objects that share references to the same nested objects, ensuring no behavioral changes.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-PropertyValueList._saved_copy-mhwxy5n1and push.