⚡️ Speed up method VertexGeminiConfig._map_response_schema by 12%
#418
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.
📄 12% (0.12x) speedup for
VertexGeminiConfig._map_response_schemainlitellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py⏱️ Runtime :
13.8 milliseconds→12.3 milliseconds(best of66runs)📝 Explanation and details
The optimization achieves a 12% speedup by addressing two key performance bottlenecks identified in the line profiler:
Primary Optimization - Module-Level Caching:
The biggest improvement comes from moving
set(get_type_hints(Schema).keys())to a module-level constant_VALID_SCHEMA_FIELDS. The line profiler shows this operation consumed 7.1% of total runtime (3.68ms out of 52ms) and was called 17 times. By caching this computation at import time, we eliminate this repeated overhead entirely - the optimized version shows negligible time (7.1μs) for the assignment.Secondary Optimization - Conditional Dict Processing:
The original code always called
parameters.pop("$defs", {}), which creates an empty dict even when no "$defs" key exists. The optimization checksparameters.pop("$defs", None)first and only processes defs if they exist. While this saves minimal time in the profiler (the pop operation was already fast), it reduces unnecessary dict operations and makes the code more efficient for schemas without definitions.Performance Analysis:
get_type_hints()call involves expensive reflection operations that inspect theSchemaTypedDict structureTest Case Performance:
The annotated tests show consistent 200-300% speedups across all schema types, with particularly strong gains for simple schemas (287% faster for type lists) where the
get_type_hintsoverhead was proportionally larger. Complex schemas with many properties still benefit (6-26% improvement) as the fixed overhead is amortized across more processing work.This optimization is especially valuable since
_build_vertex_schemaappears to be called from schema mapping operations that could be invoked frequently during API request processing.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-VertexGeminiConfig._map_response_schema-mhoe8qzjand push.