⚡️ Speed up method AzureAISearchVectorStore.similarity_search_by_vector by 6%#58
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves a 5% speedup by eliminating repeated attribute lookups in the list comprehension loop. **Key optimizations:** 1. **Local variable caching**: The field names (`self.id_field`, `self.text_field`, etc.) are cached as local variables before the loop, avoiding repeated `self.` attribute lookups during iteration. 2. **Constructor reference caching**: Function references for `VectorStoreDocument`, `VectorStoreSearchResult`, and `json.loads` are stored in local variables (`vdoc_ctor`, `vsres_ctor`, `json_loads`), eliminating repeated global/module-level lookups. **Why this improves performance:** - Python's attribute lookup (`self.field`) and global name resolution are relatively expensive operations when performed repeatedly in tight loops - Local variable access is significantly faster than attribute or global lookups in Python's bytecode execution - The optimization is most effective when processing many documents, as shown by the larger speedups in large-scale tests (5-8% improvement with 1000 documents vs. smaller gains with few documents) **Test case performance patterns:** - Small result sets (k=0, k=1): Minimal or slight regression due to setup overhead - Medium result sets (k=2-10): Modest improvements (2-4%) - Large result sets (k=100-1000): Significant improvements (5-8%) The optimization maintains identical functionality while reducing the per-document processing overhead in the critical list comprehension loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 6% (0.06x) speedup for
AzureAISearchVectorStore.similarity_search_by_vectoringraphrag/vector_stores/azure_ai_search.py⏱️ Runtime :
7.35 milliseconds→6.96 milliseconds(best of89runs)📝 Explanation and details
The optimized code achieves a 5% speedup by eliminating repeated attribute lookups in the list comprehension loop.
Key optimizations:
Local variable caching: The field names (
self.id_field,self.text_field, etc.) are cached as local variables before the loop, avoiding repeatedself.attribute lookups during iteration.Constructor reference caching: Function references for
VectorStoreDocument,VectorStoreSearchResult, andjson.loadsare stored in local variables (vdoc_ctor,vsres_ctor,json_loads), eliminating repeated global/module-level lookups.Why this improves performance:
self.field) and global name resolution are relatively expensive operations when performed repeatedly in tight loopsTest case performance patterns:
The optimization maintains identical functionality while reducing the per-document processing overhead in the critical list comprehension loop.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-AzureAISearchVectorStore.similarity_search_by_vector-mglhryt4and push.