⚡️ Speed up function _get_router_metadata_variable_name
by 137%
#10
+18
−10
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.
📄 137% (1.37x) speedup for
_get_router_metadata_variable_name
inlitellm/router_utils/batch_utils.py
⏱️ Runtime :
2.48 milliseconds
→1.05 milliseconds
(best of17
runs)📝 Explanation and details
The optimized code achieves a 136% speedup through two key optimizations:
1. Eliminated Repeated Set Construction
The original code recreates the same set of 5 method names on every function call (lines showing 13.3% + 5.8% = 19.1% of total time). The optimization moves this to a module-level
frozenset
, creating it only once when the module loads.2. Replaced
any()
Generator with Early-Return LoopThe original code uses
any(method in function_name for method in ROUTER_METHODS...)
which creates a generator and has function call overhead (72.2% of total time). The optimization uses a simple for-loop that returns immediately upon finding the first match, eliminating generator overhead.Performance Benefits by Test Case:
The optimization is particularly effective because the method set is small (5 items), making the linear search very fast while eliminating the overhead of set construction and generator expressions that dominated the original performance profile.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
router_unit_tests/test_router_batch_utils.py::test_router_metadata_variable_name
router_unit_tests/test_router_helper_utils.py::test_handle_clientside_credential_metadata_variable_name
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_ualnrfea/tmpudh2n0ld/test_concolic_coverage.py::test__get_router_metadata_variable_name
codeflash_concolic_ualnrfea/tmpudh2n0ld/test_concolic_coverage.py::test__get_router_metadata_variable_name_2
To edit these changes
git checkout codeflash/optimize-_get_router_metadata_variable_name-mh2n74iw
and push.