⚡️ Speed up method _NamedVectors.none by 62%
#87
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.
📄 62% (0.62x) speedup for
_NamedVectors.noneinweaviate/collections/classes/config_named_vectors.py⏱️ Runtime :
1.79 milliseconds→1.10 milliseconds(best of47runs)📝 Explanation and details
The optimization introduces a simple but effective object creation caching strategy. Instead of creating a new
_VectorizerConfigCreate(vectorizer=Vectorizers.NONE)object on every function call, the optimized version pre-creates this constant object once at module import time as_PRECREATED_NONE_VECTORIZER_CONFIG.Key changes:
_PRECREATED_NONE_VECTORIZER_CONFIGthat's created once at importWhy this speeds up the code:
The line profiler shows that creating
_VectorizerConfigCreate(vectorizer=Vectorizers.NONE)took 1.77ms (44.8% of total time) in the original version. By eliminating this repeated object instantiation, the optimized version reduces this to just 188μs (7.2% of total time) - simply referencing the pre-existing object.Performance characteristics:
This optimization is particularly effective for:
The 62% overall speedup comes from eliminating redundant object allocation while maintaining identical functionality and thread safety (since the vectorizer config is immutable).
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_NamedVectors.none-mh2vm719and push.