⚡️ Speed up function _model_custom_llm_provider_matches_wildcard_pattern by 8%
#449
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.
📄 8% (0.08x) speedup for
_model_custom_llm_provider_matches_wildcard_patterninlitellm/proxy/auth/auth_checks.py⏱️ Runtime :
23.7 milliseconds→22.1 milliseconds(best of88runs)📝 Explanation and details
The optimized code achieves a 7% speedup by reducing redundant string operations and improving dictionary-based lookups, particularly beneficial for the authentication pattern matching hot path.
Key Optimizations
1. Eliminated Redundant String Splitting
model.split("/", 1)result insplit_modelvariable, reusing it throughout the functionmodel_prefix = split_model[0]to avoid repeated indexingnum_parts = len(split_model)for length checks2. Pre-computed Provider Membership Checks
model_prefix in litellm.provider_listasmodel_prefix_in_providermodel_prefix not in litellm.model_list_setasmodel_prefix_not_in_model_set3. Optimized API Base Endpoint Matching
if/elifchain with a dictionary mapping (endpoint_map) for most static endpointsPerformance Impact
The optimizations particularly benefit the authentication hot path where
get_llm_provideris called frequently through_model_custom_llm_provider_matches_wildcard_pattern. Based on the function reference, this is used in_model_matches_any_wildcard_pattern_in_listfor validating model patterns against allowed lists - a critical security check that runs on every API request.Test Results Show Consistent Gains:
The optimization is especially effective for workloads with frequent model validation, model provider lookups, or batch processing scenarios where the same function is called repeatedly with different models.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_model_custom_llm_provider_matches_wildcard_pattern-mhx2m80vand push.