⚡️ Speed up method _PQEncoderConfigUpdate.merge_with_existing by 22%
#95
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.
📄 22% (0.22x) speedup for
_PQEncoderConfigUpdate.merge_with_existinginweaviate/collections/classes/config_vector_index.py⏱️ Runtime :
2.37 microseconds→1.95 microsecondss(best of79runs)📝 Explanation and details
The optimization caches attribute lookups by storing
self.type_andself.distributionin local variables at the beginning of the method. This eliminates repeated attribute access overhead during the conditional checks.Key changes:
type_ = self.type_anddistribution = self.distributionassignmentsself.attributeaccessWhy this improves performance:
In Python, attribute access involves dictionary lookups and potential descriptor protocol calls. By caching these values as local variables, the method avoids redundant attribute resolution during the
is not Nonechecks. Local variable access is significantly faster than attribute access because it uses direct array indexing in the local namespace rather than hash table lookups.Test case performance:
The optimization shows consistent 20-24% speedups across all test scenarios, particularly effective for cases where both attributes are None (most common path). The speedup is most pronounced in simple cases like empty schemas with None fields, demonstrating that even basic attribute caching can yield meaningful performance gains in frequently called methods.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_PQEncoderConfigUpdate.merge_with_existing-mh30tjhcand push.