⚡️ Speed up function _can_object_call_vector_stores by 3,113%
#450
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.
📄 3,113% (31.13x) speedup for
_can_object_call_vector_storesinlitellm/proxy/auth/auth_checks.py⏱️ Runtime :
10.0 milliseconds→311 microseconds(best of27runs)📝 Explanation and details
The optimized code achieves a 31x speedup through three key improvements:
1. Combined early exit conditions - The original code checked
object_permissions is Noneandobject_permissions.vector_stores is Noneseparately. The optimized version combines these into a single condition, reducing redundant attribute access and improving branch prediction.2. Local variable caching - By storing
object_permissions.vector_storesin the local variablevec_stores, the optimization eliminates repeated attribute lookups during the permission checking loop, which is faster than accessing object attributes repeatedly.3. Adaptive data structure optimization - The most significant improvement converts the allowed vector stores list to a
setwhen both the permission list is large (>16 items) and multiple stores are being checked. This transforms O(N) list membership checks into O(1) set lookups.Performance impact by test case:
Real-world impact: Based on the function reference, this optimization is called from
vector_store_access_check(), which runs during API request authorization. The adaptive threshold ensures small, typical permission lists aren't penalized by set conversion overhead, while large enterprise deployments with extensive permission lists see massive performance gains. This is particularly valuable in high-throughput proxy scenarios where authorization checks are in the critical path of every vector store operation.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_can_object_call_vector_stores-mhx36s8mand push.