From c98a30a4b64b2198f08710101d0f69d420b2e070 Mon Sep 17 00:00:00 2001 From: AlexsanderHamir Date: Wed, 15 Oct 2025 15:44:09 -0700 Subject: [PATCH 1/2] perf(router): optimize string concatenation in hash generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace string concatenation in loop with list append + join pattern. This improves time complexity from O(n²) to O(n) and avoids creating many temporary string objects during hash ID generation. --- litellm/router.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/litellm/router.py b/litellm/router.py index 5972b06f01..f8ce4b2360 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -4915,22 +4915,25 @@ class Router: - hash - use hash as id """ - concat_str = model_group + # Optimized: Use list and join instead of string concatenation in loop + # This avoids creating many temporary string objects (O(n) vs O(n²) complexity) + parts = [model_group] for k, v in litellm_params.items(): if isinstance(k, str): - concat_str += k + parts.append(k) elif isinstance(k, dict): - concat_str += json.dumps(k) + parts.append(json.dumps(k)) else: - concat_str += str(k) + parts.append(str(k)) if isinstance(v, str): - concat_str += v + parts.append(v) elif isinstance(v, dict): - concat_str += json.dumps(v) + parts.append(json.dumps(v)) else: - concat_str += str(v) + parts.append(str(v)) + concat_str = "".join(parts) hash_object = hashlib.sha256(concat_str.encode()) return hash_object.hexdigest() From 97ed3d01ec359288cd12ea7923a58ca090dff492 Mon Sep 17 00:00:00 2001 From: AlexsanderHamir Date: Wed, 15 Oct 2025 15:58:08 -0700 Subject: [PATCH 2/2] test(router): update error message assertion after string concat optimization Update test_generate_model_id_with_deployment_model_name to accept the new error message format that results from the list+join optimization. The function still correctly rejects None values with a TypeError, but the error message changed from 'unsupported operand type(s) for +=' to 'expected str instance, NoneType found' due to the implementation change from string concatenation to list joining. --- tests/router_unit_tests/test_router_helper_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/router_unit_tests/test_router_helper_utils.py b/tests/router_unit_tests/test_router_helper_utils.py index a31c4d8210..c2339d9eec 100644 --- a/tests/router_unit_tests/test_router_helper_utils.py +++ b/tests/router_unit_tests/test_router_helper_utils.py @@ -1411,7 +1411,8 @@ def test_generate_model_id_with_deployment_model_name(model_list): "Expected TypeError when model_group is None - this confirms our fix is needed" ) except TypeError as e: - assert "unsupported operand type(s) for +=" in str(e) + # After optimization, error message changed but still fails appropriately on None + assert "unsupported operand type(s) for +=" in str(e) or "expected str instance, NoneType found" in str(e) print(f"✓ Correctly failed with None model_group (as expected): {e}") except Exception as e: pytest.fail(f"Unexpected error with None model_group: {e}")