⚡️ Speed up function prepare_object_detection_prompt by 10% in PR #1869 (api-key-passthrough/gemini)
#1874
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.
⚡️ This pull request contains optimizations for PR #1869
If you approve this dependent PR, these changes will be merged into the original PR branch
api-key-passthrough/gemini.📄 10% (0.10x) speedup for
prepare_object_detection_promptininference/core/workflows/core_steps/models/foundation/google_gemini/v3.py⏱️ Runtime :
206 microseconds→187 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 10% speedup through two key changes:
1. Frozenset Lookup for Model Version Checking
The original code stores
MODELS_SUPPORTING_THINKING_LEVELas a list:The optimized version uses a
frozenset:Why this is faster: In
prepare_generation_config, the code checksmodel_version in MODELS_SUPPORTING_THINKING_LEVEL. With a list, this is an O(n) linear search through all elements. With a frozenset, it's an O(1) hash lookup. SinceGEMINI_MODELShas 6 entries (with only 1 supporting thinking level), this provides a modest but consistent improvement on every call.2. Pre-computed System Instruction Dictionary
The original code constructs the entire system instruction dictionary on every call to
prepare_object_detection_prompt:The optimized version pre-builds this static structure at module load time:
Why this is faster: Python dictionary construction has overhead - allocating memory, setting keys, and nesting structures. The system instruction never changes between calls, so building it once and reusing the same reference eliminates redundant work. The line profiler shows this reduces time spent in the systemInstruction section from ~8% to ~2.7% of total runtime.
Performance Impact by Test Case
Based on the annotated tests, these optimizations are most effective for:
The optimizations provide consistent benefits across all use cases since
prepare_object_detection_promptis called on every inference request, making even small per-call savings valuable in production environments with high request volumes.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1869-2026-01-04T11.37.50and push.