⚡️ Speed up method _VectorIndex.hnsw by 7%
#96
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.hnswinweaviate/collections/classes/config_vector_index.py⏱️ Runtime :
29.7 microseconds→27.8 microseconds(best of85runs)📝 Explanation and details
The optimization removes an unnecessary keyword argument in the
warnings.warn()call. In the original code, the warning message was passed usingmessage=, butwarnings.warn()accepts the message as the first positional argument. By removing themessage=keyword and passing the warning text directly as a positional argument, the optimized version eliminates the overhead of keyword argument processing in Python's function call mechanism.This micro-optimization reduces the cost of parameter binding during function calls. When Python processes keyword arguments, it must perform dictionary lookups and parameter matching, whereas positional arguments are passed more directly. While the performance gain is small (6% speedup, reducing runtime from 29.7μs to 27.8μs), it's meaningful for frequently called deprecation warnings in library code.
The optimization is particularly effective for test cases that trigger the deprecation warning path (when
multi_vectoris not None), as shown in the test suite where the warning is issued during HNSW configuration creation.✅ Correctness verification report:
⏪ Replay Tests and Runtime
test_pytest_testcollectiontest_batch_py_testcollectiontest_classes_generative_py_testcollectiontest_confi__replay_test_0.py::test_weaviate_collections_classes_config_vector_index__VectorIndex_hnswTo edit these changes
git checkout codeflash/optimize-_VectorIndex.hnsw-mh31sw1nand push.