⚡️ Speed up function _can_object_call_model by 21%
#444
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.
📄 21% (0.21x) speedup for
_can_object_call_modelinlitellm/proxy/auth/auth_checks.py⏱️ Runtime :
159 milliseconds→132 milliseconds(best of12runs)📝 Explanation and details
The optimized code achieves a 20% speedup through several key micro-optimizations in the
_check_model_access_helperfunction:Primary Optimization - Conditional List Filtering:
The original code always created
filtered_modelsby filtering out access groups, even whenaccess_groupswas empty. The optimized version only performs this expensive list comprehension when access groups actually exist:Performance Impact:
Secondary Optimization - Set-based Lookups:
Replaced repeated list membership checks with set-based operations:
Why This Matters:
This function is called in authentication hot paths from
can_key_call_model,can_team_access_model,can_user_call_model, andcan_org_access_model- all critical for API request validation. The test results show consistent 5-20% improvements across various scenarios, with the largest gains (20.9%) in thetest_large_scale_list_of_models_all_allowedcase where the conditional filtering optimization has maximum impact.The optimizations are particularly effective for deployments with many models but few/no access groups, which represents typical production configurations.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_can_object_call_model-mhx00tg1and push.