⚡️ Speed up method VertexGeminiConfig.get_tool_value by 9%
#417
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.
📄 9% (0.09x) speedup for
VertexGeminiConfig.get_tool_valueinlitellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py⏱️ Runtime :
139 microseconds→127 microseconds(best of162runs)📝 Explanation and details
The optimized code achieves a 9% speedup through two key optimizations:
1.
__init__Method Optimization:The original code used
locals().copy()and iterated through all local variables, which creates unnecessary overhead. The optimized version directly checks each parameter individually with explicitifstatements, avoiding dictionary creation and iteration. This is more efficient since most parameters are typicallyNoneduring initialization.2.
get_tool_valueMethod Optimization:tool.get()multiple times for the same key. The optimized version stores the result in a variable and reuses it, reducing dictionary access overhead."".join(), the optimized version uses a simple loop with explicit character appending, which is more efficient for string building operations.Performance Benefits by Test Case:
The optimizations are particularly effective for frequently called utility functions like
get_tool_value, which appears to be used extensively in tool configuration scenarios. The improvements scale well with both simple and complex inputs while maintaining identical functionality.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-VertexGeminiConfig.get_tool_value-mhodwf6band push.