⚡️ Speed up method VertexAITextEmbeddingConfig.map_special_auth_params by 113%
#412
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.
📄 113% (1.13x) speedup for
VertexAITextEmbeddingConfig.map_special_auth_paramsinlitellm/llms/vertex_ai/vertex_embeddings/transformation.py⏱️ Runtime :
141 microseconds→66.3 microseconds(best of250runs)📝 Explanation and details
The optimization replaces the inefficient loop-and-check pattern in
map_special_auth_paramswith Python's set intersection operation (&).Key optimization:
non_default_params.items()and checksif param in mapped_paramsfor each onenon_default_params.keys() & mapped_params.keys()to find only the intersection of keys, then iterates only over matching parametersWhy this is faster:
The original code performs O(n×m) operations where n is the number of parameters and m is the size of the mapping dict. For each parameter, it does a dictionary lookup to check membership. The optimized version uses set intersection which is O(n+m) and only processes relevant keys.
Performance impact from test results:
non_default_paramscontains many keys that aren't in the smallmapped_paramsdict (only 2 keys: "project" and "region_name")When this optimization matters:
This is particularly beneficial for configurations with many authentication parameters where only a few need special mapping - a common pattern in cloud service integrations where you might have dozens of config options but only specific ones need transformation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-VertexAITextEmbeddingConfig.map_special_auth_params-mhoaj1waand push.