⚡️ Speed up function update_response_metadata by 286%
#389
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.
📄 286% (2.86x) speedup for
update_response_metadatainlitellm/litellm_core_utils/llm_response_utils/response_metadata.py⏱️ Runtime :
108 milliseconds→27.9 milliseconds(best of148runs)📝 Explanation and details
The optimized code achieves a 285% speedup by targeting the most expensive operations identified in the line profiler:
Primary optimization - LRU caching for
get_api_base:get_api_base()repeatedly@lru_cache(maxsize=128)to cache expensive API base lookups using hashable tuples of kwargsSecondary optimizations:
logging_obj.model_call_detailsandlogging_obj.caching_detailsto local variables to avoid repeated dot notation access_hidden_params_cacheto avoid repeated lookups of the same hidden parameter keysmodel_infohandling to avoid redundantor {}operationsPerformance by test case type:
The optimization is most effective for workloads with repeated calls using the same model configurations, which is common in production LLM applications where the same models are used across many requests.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_teststest_litellmresponseslitellm_completion_transformationtest_litellm_completion_responses___replay_test_0.py::test_litellm_litellm_core_utils_llm_response_utils_response_metadata_update_response_metadataTo edit these changes
git checkout codeflash/optimize-update_response_metadata-mhl4ji2jand push.