⚡️ Speed up method Logging.initialize_standard_built_in_tools_params by 32%
          #114
        
          
      
  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.
  
    
  
    
📄 32% (0.32x) speedup for
Logging.initialize_standard_built_in_tools_paramsinlitellm/litellm_core_utils/litellm_logging.py⏱️ Runtime :
8.03 microseconds→6.10 microseconds(best of35runs)📝 Explanation and details
The optimized code achieves a 31% speedup by eliminating redundant function calls and unnecessary dictionary traversals in tool extraction logic.
Key optimizations:
Eliminated redundant
_get_tools_from_kwargscalls: The original code called this helper function multiple times with differenttool_typeparameters, but the function ignored the parameter and always returnedkwargs.get("tools"). The optimized version directly callskwargs.get("tools")once, removing function call overhead.Consolidated tool type checking: Instead of making separate calls to search for "web_search_preview" and "web_search" tools, the optimized version retrieves the tools list once and checks for multiple tool types (
web_search_preview,web_search, or presence ofsearch_context_size) in a single loop iteration.Simplified tool matching logic: The optimized code inlines the tool type checking directly in the loop, avoiding additional function calls to
_is_web_search_tool_calland_is_file_search_tool_call.Performance impact from profiler data:
_get_web_search_optionsexecution time reduced from 31.3μs to 5.9μs (81% improvement)_get_file_search_tool_callexecution time reduced from 5.6μs to 3.0μs (47% improvement)These optimizations are particularly effective for scenarios where the
toolsparameter is frequently accessed during logging initialization, as they reduce both CPU cycles and memory allocations from repeated dictionary lookups and function calls.✅ Correctness verification report:
⏪ Replay Tests and Runtime
test_pytest_teststest_litellmresponseslitellm_completion_transformationtest_litellm_completion_responses___replay_test_0.py::test_litellm_litellm_core_utils_litellm_logging_Logging_initialize_standard_built_in_tools_paramsTo edit these changes
git checkout codeflash/optimize-Logging.initialize_standard_built_in_tools_params-mhbuqgqeand push.