Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 30, 2025

📄 6% (0.06x) speedup for _safe_set_request_parsed_body in litellm/proxy/common_utils/http_parsing_utils.py

⏱️ Runtime : 58.5 microseconds 55.1 microseconds (best of 528 runs)

📝 Explanation and details

The optimization achieves a 6% speedup through two key changes:

1. Early exit optimization: Moving the if request is None check outside the try-except block eliminates unnecessary exception handling setup for the common case where request is None. This avoids the overhead of entering a try block when no actual work needs to be done.

2. f-string replacement: Replacing .format() with f-string formatting (f"...{e}") for the debug log message is faster since f-strings are compiled at parse time rather than evaluated at runtime.

The line profiler results show the early exit optimization is particularly effective - the None check now takes 25.5% of execution time but runs more efficiently, while the try block setup is reduced from 12.3% to 8.8% of total time. The f-string change reduces string formatting time from 6.9% to 3.2%.

These optimizations are most beneficial for:

  • High-frequency calls with None requests (17.3% faster in test_basic_none_request)
  • Exception cases where debug logging occurs (18.9% faster in test_edge_non_dict_parsed_body)
  • Simple valid requests where the main path executes without exceptions

The optimizations maintain identical behavior while reducing Python interpreter overhead through more efficient control flow and string operations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 41 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from typing import Optional

# imports
import pytest  # used for our unit tests
from fastapi import Request
from litellm.proxy.common_utils.http_parsing_utils import \
    _safe_set_request_parsed_body
from starlette.datastructures import Headers
from starlette.types import Scope


# Helper to create a minimal FastAPI Request object with a mutable scope
class DummyReceive:
    async def __call__(self):
        return {}

def make_request(scope_overrides=None):
    """
    Helper to create a minimal Request object for testing.
    Allows overriding scope fields.
    """
    base_scope = {
        "type": "http",
        "method": "GET",
        "path": "/",
        "headers": [],
        "query_string": b"",
    }
    if scope_overrides:
        base_scope.update(scope_overrides)
    return Request(base_scope, DummyReceive())

# unit tests

# 1. Basic Test Cases

def test_basic_set_parsed_body_simple_dict():
    """Test setting a simple parsed_body dict on a Request."""
    req = make_request()
    body = {"foo": 1, "bar": "baz"}
    _safe_set_request_parsed_body(req, body) # 1.16μs -> 1.09μs (6.03% faster)
    keys, val = req.scope["parsed_body"]

def test_basic_set_parsed_body_empty_dict():
    """Test setting an empty dict as parsed_body."""
    req = make_request()
    body = {}
    _safe_set_request_parsed_body(req, body) # 1.02μs -> 967ns (6.00% faster)
    keys, val = req.scope["parsed_body"]

def test_basic_request_none():
    """Test that passing None as request does not raise and does nothing."""
    body = {"foo": "bar"}
    # Should not raise
    _safe_set_request_parsed_body(None, body) # 341ns -> 329ns (3.65% faster)
    # Nothing to assert, just check no error

def test_basic_overwrite_existing_parsed_body():
    """Test overwriting an existing parsed_body in scope."""
    req = make_request()
    req.scope["parsed_body"] = (("old",), {"old": 123})
    body = {"new": 456}
    _safe_set_request_parsed_body(req, body) # 1.08μs -> 1.07μs (1.31% faster)
    keys, val = req.scope["parsed_body"]

# 2. Edge Test Cases

def test_edge_parsed_body_with_non_str_keys():
    """Test parsed_body dict with non-string keys."""
    req = make_request()
    body = {1: "one", (2, 3): "tuplekey"}
    _safe_set_request_parsed_body(req, body) # 1.13μs -> 1.14μs (0.616% slower)
    keys, val = req.scope["parsed_body"]

def test_edge_parsed_body_with_nested_dict():
    """Test parsed_body dict with nested dictionaries."""
    req = make_request()
    body = {"outer": {"inner": 42}, "simple": 1}
    _safe_set_request_parsed_body(req, body) # 1.07μs -> 1.06μs (1.32% faster)
    keys, val = req.scope["parsed_body"]

def test_edge_parsed_body_with_mutable_values():
    """Test parsed_body dict with lists and sets as values."""
    req = make_request()
    body = {"list": [1,2], "set": set([3,4])}
    _safe_set_request_parsed_body(req, body) # 1.02μs -> 1.04μs (1.93% slower)
    keys, val = req.scope["parsed_body"]

def test_edge_scope_missing_parsed_body_key():
    """Test that parsed_body is added if not present in scope."""
    req = make_request()
    body = {"x": 1}
    _safe_set_request_parsed_body(req, body) # 1.04μs -> 1.07μs (3.36% slower)

def test_edge_scope_is_unexpected_type():
    """Test that function does not crash if scope is not a dict (simulate error)."""
    class WeirdRequest:
        def __init__(self):
            self.scope = None
    req = WeirdRequest()
    try:
        _safe_set_request_parsed_body(req, {"a": 1})
    except Exception as e:
        pytest.fail(f"Should not raise, but got: {e}")

def test_edge_parsed_body_with_none_value():
    """Test parsed_body dict with None value."""
    req = make_request()
    body = {"key": None}
    _safe_set_request_parsed_body(req, body) # 1.04μs -> 1.03μs (0.774% faster)
    keys, val = req.scope["parsed_body"]

def test_edge_parsed_body_is_not_dict():
    """Test that function does not crash if parsed_body is not a dict."""
    req = make_request()
    # Try with a list
    try:
        _safe_set_request_parsed_body(req, ["not", "a", "dict"])
    except Exception as e:
        pytest.fail(f"Should not raise, but got: {e}")

def test_edge_request_scope_is_missing():
    """Test that function does not crash if request has no scope attribute."""
    class NoScopeRequest:
        pass
    req = NoScopeRequest()
    try:
        _safe_set_request_parsed_body(req, {"foo": "bar"})
    except Exception as e:
        pytest.fail(f"Should not raise, but got: {e}")

# 3. Large Scale Test Cases

def test_large_scale_parsed_body_many_keys():
    """Test parsed_body dict with 1000 keys."""
    req = make_request()
    body = {f"key{i}": i for i in range(1000)}
    _safe_set_request_parsed_body(req, body) # 4.59μs -> 4.61μs (0.304% slower)
    keys, val = req.scope["parsed_body"]

def test_large_scale_parsed_body_large_values():
    """Test parsed_body dict with large string values."""
    req = make_request()
    large_str = "x" * 1000
    body = {"big": large_str, "small": "y"}
    _safe_set_request_parsed_body(req, body) # 1.10μs -> 1.08μs (2.51% faster)
    keys, val = req.scope["parsed_body"]

def test_large_scale_multiple_calls():
    """Test multiple calls with different bodies do not interfere."""
    req = make_request()
    for i in range(10):
        body = {f"k{i}": i}
        _safe_set_request_parsed_body(req, body) # 3.62μs -> 3.63μs (0.331% slower)
        keys, val = req.scope["parsed_body"]

def test_large_scale_parsed_body_keys_are_ordered():
    """Test that the tuple of keys preserves insertion order (Python 3.7+)."""
    req = make_request()
    body = {f"key{i}": i for i in range(10)}
    _safe_set_request_parsed_body(req, body) # 1.11μs -> 1.07μs (3.75% faster)
    keys, val = req.scope["parsed_body"]
    expected_keys = tuple(f"key{i}" for i in range(10))
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from typing import Optional

# imports
import pytest  # used for our unit tests
from fastapi import Request
from litellm.proxy.common_utils.http_parsing_utils import \
    _safe_set_request_parsed_body
from starlette.datastructures import Headers
from starlette.types import Scope


class DummyLogger:
    def __init__(self):
        self.logged = []
    def debug(self, msg):
        self.logged.append(msg)

# Patch for litellm._logging.verbose_proxy_logger
verbose_proxy_logger = DummyLogger()
from litellm.proxy.common_utils.http_parsing_utils import \
    _safe_set_request_parsed_body


# Helper to create a minimal FastAPI Request object
def make_request_with_scope(scope=None):
    if scope is None:
        scope = {
            "type": "http",
            "method": "POST",
            "headers": [],
            "parsed_body": None,
        }
    # Use Starlette's Request for compatibility
    return Request(scope)

# ---------------------- UNIT TESTS ----------------------

# 1. BASIC TEST CASES

def test_basic_none_request():
    # Test that passing None as request does nothing and does not raise
    parsed_body = {"foo": "bar"}
    codeflash_output = _safe_set_request_parsed_body(None, parsed_body); result = codeflash_output # 379ns -> 323ns (17.3% faster)

def test_basic_simple_dict():
    # Test with a simple dict and a valid request
    request = make_request_with_scope()
    parsed_body = {"a": 1, "b": 2}
    _safe_set_request_parsed_body(request, parsed_body) # 978ns -> 954ns (2.52% faster)
    keys, body = request.scope["parsed_body"]

def test_basic_empty_dict():
    # Test with an empty dict
    request = make_request_with_scope()
    parsed_body = {}
    _safe_set_request_parsed_body(request, parsed_body) # 915ns -> 952ns (3.89% slower)
    keys, body = request.scope["parsed_body"]

def test_basic_single_key():
    # Test with a dict with one key
    request = make_request_with_scope()
    parsed_body = {"x": 42}
    _safe_set_request_parsed_body(request, parsed_body) # 892ns -> 978ns (8.79% slower)
    keys, body = request.scope["parsed_body"]

# 2. EDGE TEST CASES

def test_edge_non_dict_parsed_body():
    # Test with a non-dict parsed_body (should raise inside, but not propagate)
    request = make_request_with_scope()
    parsed_body = ["not", "a", "dict"]
    _safe_set_request_parsed_body(request, parsed_body) # 3.81μs -> 3.20μs (18.9% faster)

def test_edge_scope_missing_parsed_body():
    # Test when scope does not have 'parsed_body' initially
    scope = {
        "type": "http",
        "method": "GET",
        "headers": [],
    }
    request = make_request_with_scope(scope)
    parsed_body = {"z": 99}
    _safe_set_request_parsed_body(request, parsed_body) # 896ns -> 959ns (6.57% slower)
    keys, body = request.scope["parsed_body"]

def test_edge_scope_is_none():
    # Test with a request whose scope is None (simulate broken request)
    class BrokenRequest:
        def __init__(self):
            self.scope = None
    request = BrokenRequest()
    parsed_body = {"foo": "bar"}
    _safe_set_request_parsed_body(request, parsed_body) # 4.05μs -> 3.44μs (17.7% faster)

def test_edge_scope_is_not_dict():
    # Test with a request whose scope is not a dict
    class WeirdRequest:
        def __init__(self):
            self.scope = "not_a_dict"
    request = WeirdRequest()
    parsed_body = {"foo": "bar"}
    _safe_set_request_parsed_body(request, parsed_body) # 3.69μs -> 3.25μs (13.6% faster)

def test_edge_parsed_body_with_non_string_keys():
    # Test with dict with non-string keys
    request = make_request_with_scope()
    parsed_body = {1: "one", (2, 3): "tuple"}
    _safe_set_request_parsed_body(request, parsed_body) # 970ns -> 942ns (2.97% faster)
    keys, body = request.scope["parsed_body"]

def test_edge_parsed_body_with_none_value():
    # Test with dict with None value
    request = make_request_with_scope()
    parsed_body = {"foo": None}
    _safe_set_request_parsed_body(request, parsed_body) # 945ns -> 958ns (1.36% slower)
    keys, body = request.scope["parsed_body"]

def test_edge_parsed_body_with_mutable_value():
    # Test with dict containing a mutable value
    request = make_request_with_scope()
    parsed_body = {"foo": [1,2,3]}
    _safe_set_request_parsed_body(request, parsed_body) # 922ns -> 971ns (5.05% slower)
    keys, body = request.scope["parsed_body"]

def test_edge_parsed_body_with_nested_dict():
    # Test with dict containing a nested dict
    request = make_request_with_scope()
    parsed_body = {"outer": {"inner": "val"}}
    _safe_set_request_parsed_body(request, parsed_body) # 901ns -> 947ns (4.86% slower)
    keys, body = request.scope["parsed_body"]

# 3. LARGE SCALE TEST CASES

def test_large_scale_many_keys():
    # Test with a dict with many keys (up to 1000)
    request = make_request_with_scope()
    parsed_body = {f"key_{i}": i for i in range(1000)}
    _safe_set_request_parsed_body(request, parsed_body) # 4.29μs -> 4.45μs (3.40% slower)
    keys, body = request.scope["parsed_body"]

def test_large_scale_large_values():
    # Test with a dict with large values (each value is a big string)
    request = make_request_with_scope()
    big_str = "x" * 1000
    parsed_body = {f"key_{i}": big_str for i in range(10)}
    _safe_set_request_parsed_body(request, parsed_body) # 1.03μs -> 1.04μs (1.63% slower)
    keys, body = request.scope["parsed_body"]
    for v in body.values():
        pass

def test_large_scale_nested_dicts():
    # Test with a dict with many nested dicts
    request = make_request_with_scope()
    parsed_body = {f"outer_{i}": {f"inner_{j}": j for j in range(10)} for i in range(50)}
    _safe_set_request_parsed_body(request, parsed_body) # 1.16μs -> 1.23μs (5.70% slower)
    keys, body = request.scope["parsed_body"]
    for k in keys:
        pass

def test_large_scale_keys_are_in_order():
    # Test that keys are in the same order as dict insertion (Python 3.7+ guarantees this)
    request = make_request_with_scope()
    keys_order = [f"k{i}" for i in range(100)]
    parsed_body = {k: i for i, k in enumerate(keys_order)}
    _safe_set_request_parsed_body(request, parsed_body) # 1.41μs -> 1.49μs (5.23% slower)
    keys, body = request.scope["parsed_body"]
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-_safe_set_request_parsed_body-mhdfh2f3 and push.

Codeflash Static Badge

The optimization achieves a 6% speedup through two key changes:

**1. Early exit optimization:** Moving the `if request is None` check outside the try-except block eliminates unnecessary exception handling setup for the common case where request is None. This avoids the overhead of entering a try block when no actual work needs to be done.

**2. f-string replacement:** Replacing `.format()` with f-string formatting (`f"...{e}"`) for the debug log message is faster since f-strings are compiled at parse time rather than evaluated at runtime.

The line profiler results show the early exit optimization is particularly effective - the None check now takes 25.5% of execution time but runs more efficiently, while the try block setup is reduced from 12.3% to 8.8% of total time. The f-string change reduces string formatting time from 6.9% to 3.2%.

These optimizations are most beneficial for:
- **High-frequency calls with None requests** (17.3% faster in test_basic_none_request)
- **Exception cases** where debug logging occurs (18.9% faster in test_edge_non_dict_parsed_body)
- **Simple valid requests** where the main path executes without exceptions

The optimizations maintain identical behavior while reducing Python interpreter overhead through more efficient control flow and string operations.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 12:56
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant