⚡️ Speed up method _VectorIndex.dynamic by 7%
#97
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.
📄 7% (0.07x) speedup for
_VectorIndex.dynamicinweaviate/collections/classes/config_vector_index.py⏱️ Runtime :
62.0 microseconds→57.7 microseconds(best of57runs)📝 Explanation and details
The optimization introduces a local variable assignment to cache the class reference before instantiation. By assigning
_VectorIndexConfigDynamicCreateto the local variablecls, the optimized version reduces the overhead of repeated global namespace lookups during constructor calls.Key optimization:
cls = _VectorIndexConfigDynamicCreatestores the class reference locally, then usescls(...)instead of the full class name.Why this improves performance:
In Python, accessing global names (like class constructors) involves namespace lookup overhead. By storing the class reference in a local variable, subsequent access becomes a faster local variable lookup rather than a global namespace search. This is particularly effective for functions that are called frequently, as demonstrated by the 7% speedup (62.0μs → 57.7μs).
Test case performance:
The optimization shows consistent improvements across different parameter combinations:
This optimization is most beneficial for high-frequency method calls or when this function is part of a performance-critical path, as the cumulative savings from reduced namespace lookups compound over many invocations.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_VectorIndex.dynamic-mh31zmuyand push.