⚡️ Speed up method PropertyValueDict.clear by 8%
#167
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.
📄 8% (0.08x) speedup for
PropertyValueDict.clearinsrc/bokeh/core/property/wrappers.py⏱️ Runtime :
145 microseconds→134 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves an 8% speedup by eliminating Python's method resolution overhead in two critical areas of the
PropertyValueDictclass:Key Optimizations:
Direct Constructor Calls: Instead of using
super().__init__(*args, **kwargs)which triggers Python's Method Resolution Order (MRO) lookup, the optimized version directly calls each parent class constructor:PropertyValueContainer.__init__(self)dict.__init__(self, *args, **kwargs)This avoids the MRO dispatch mechanism that must determine which parent's
__init__to call in the multiple inheritance hierarchy.Direct Method Invocation: The
clear()method replacessuper().clear()withdict.clear(self), bypassing thesuper()proxy object and directly invoking the C-implemented dict method.Why This Speeds Things Up:
super()creates a proxy object and performs dynamic method lookup through the MRO chain, adding Python-level overheaddictexecute at C speed without Python dispatch overheadPerformance Impact Based on Tests:
The annotated tests show consistent 8-28% improvements across all scenarios, with the largest gains (20-28%) occurring in cases with:
The optimization is particularly valuable since
PropertyValueDictis a wrapper around Python's built-indictand these methods may be called frequently in property update scenarios within the Bokeh framework.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-PropertyValueDict.clear-mhwzg7gsand push.