⚡️ Speed up function can_user_call_model by 27%
#446
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.
📄 27% (0.27x) speedup for
can_user_call_modelinlitellm/proxy/auth/auth_checks.py⏱️ Runtime :
6.73 milliseconds→5.29 milliseconds(best of12runs)📝 Explanation and details
The optimized code achieves a 27% runtime improvement through three key optimizations that reduce unnecessary work in auth validation:
1. Early Return for Global Access
if not models or "*" in models or SpecialModelNames.all_proxy_models.value in models: return True_check_model_access_helpercalls when users have unrestricted access2. Optimized Dictionary Lookups
if model in litellm.model_alias_map:+litellm.model_alias_map[model]to singlelitellm.model_alias_map.get(model)getattr(llm_router, "model_group_alias", None)to avoid repeated attribute access3. Streamlined Membership Checks
can_user_call_model, replaced directinoperation withhasattr(user_models, "__contains__")checkuser_modelsmight be a set (O(1) lookup) rather than list (O(n))Impact Analysis:
common_checks()in authentication hot path, making these micro-optimizations valuable_check_model_access_helpercalls reduced from 1,846 to 764 hits (58% reduction)"*"andSpecialModelNames.all_proxy_models.valueThe optimizations maintain identical behavior and error handling while eliminating redundant computations in the authorization pipeline.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-can_user_call_model-mhx1k2yland push.