Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 13, 2025

📄 239% (2.39x) speedup for _is_ui_route in litellm/proxy/auth/auth_checks.py

⏱️ Runtime : 189 milliseconds 55.9 milliseconds (best of 70 runs)

📝 Explanation and details

The optimization achieves a 238% speedup by eliminating the primary performance bottleneck: repeated regex compilation in route pattern matching.

Key Optimizations:

  1. Regex Compilation Caching: The original code compiled the same regex patterns repeatedly via re.sub() and re.match() on every call to _route_matches_pattern(). The optimized version introduces @lru_cache(maxsize=128) on _get_compiled_route_regex() to cache compiled regex objects, avoiding expensive recompilation for repeated patterns.

  2. Single-Pass Route Checking: The original _is_ui_route() used two separate any() loops - first checking startswith() matches, then pattern matches. The optimization combines both checks into a single loop with short-circuit logic, reducing iterations over allowed_routes from 2N to N operations.

Performance Impact:

  • Line profiler shows _route_matches_pattern() time dropped from 713ms to 206ms (71% reduction)
  • The regex compilation lines (originally consuming 81.6% of function time) now benefit from caching
  • Single-loop optimization reduces overall _is_ui_route() execution time by 42%

Workload Benefits:
Based on function_references, _is_ui_route() is called from _is_allowed_route() for every UI token validation request. This optimization is particularly valuable for:

  • High-frequency UI authentication where the same route patterns are validated repeatedly
  • Large-scale deployments with many concurrent UI requests (test results show 236-241% speedup on large-scale scenarios)
  • Applications with complex route patterns containing parameters like /threads/{thread_id} where regex compilation overhead is significant

The caching approach is especially effective since UI routes typically follow predictable patterns that repeat across requests, making the LRU cache highly efficient.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4955 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 83.3%
🌀 Generated Regression Tests and Runtime
import re

# imports
import pytest
from litellm.proxy.auth.auth_checks import _is_ui_route

# --- Mocks for dependencies ---

# Mock of LiteLLMRoutes.ui_routes.value
class MockLiteLLMRoutes:
    class ui_routes:
        # Example UI routes for testing
        value = [
            "/ui",
            "/dashboard",
            "/admin/settings",
            "/threads/{thread_id}",
            "/key/{token_id}/regenerate",
            "/users/{user_id}/profile",
            "/api/ui/{section}/info",
            "/static/assets",
            "/healthz"
        ]

# Mock of LiteLLM_UserTable (not used in logic, but required for signature)
class MockLiteLLM_UserTable:
    pass
from litellm.proxy.auth.auth_checks import _is_ui_route

# --- Unit Tests ---

# 1. Basic Test Cases

@pytest.mark.parametrize(
    "route,expected",
    [
        # Exact matches with allowed routes
        ("/ui", True),  # exact
        ("/dashboard", True),  # exact
        ("/admin/settings", True),  # exact
        ("/static/assets", True),  # exact
        ("/healthz", True),  # exact

        # Startswith matches (prefixes)
        ("/ui/settings", True),
        ("/dashboard/stats", True),
        ("/admin/settings/users", True),
        ("/static/assets/logo.png", True),

        # Pattern matches (with parameters)
        ("/threads/thread_ABC123", True),
        ("/key/123456/regenerate", True),
        ("/users/u_999/profile", True),
        ("/api/ui/overview/info", True),

        # Not matching any allowed route
        ("/api/v1/model", False),
        ("/api/ui", False),  # not in allowed_routes, only "/api/ui/{section}/info" is
        ("/key/regenerate/82akk800000000jjsk", False),  # wrong pattern order
        ("/users/u_999", False),  # missing /profile
        ("/dashboard2", False),  # not a prefix
    ]
)
def test_basic_is_ui_route(route, expected):
    """Test basic and pattern matching cases for _is_ui_route."""
    codeflash_output = _is_ui_route(route) # 781μs -> 259μs (201% faster)

# 2. Edge Test Cases

@pytest.mark.parametrize(
    "route,expected",
    [
        # None or non-string input
        (None, False),
        (123, False),
        (b"/ui", False),
        (["/ui"], False),
        ({'route': '/ui'}, False),

        # Empty string
        ("", False),

        # Only slash
        ("/", False),

        # Trailing slash (should not match unless allowed_routes has it)
        ("/ui/", True),  # "/ui" is a prefix, so "/ui/" should match
        ("/dashboard/", True),
        ("/admin/settings/", True),

        # Case sensitivity (should be case sensitive)
        ("/UI", False),
        ("/Dashboard", False),

        # Route is substring but not prefix
        ("ui/dashboard", False),
        ("settings/admin", False),

        # Route matches pattern with extra path segments
        ("/threads/thread_ABC123/extra", False),
        ("/users/u_999/profile/edit", False),

        # Route matches pattern with missing path segments
        ("/threads/", False),
        ("/users//profile", False),

        # Route has URL-encoded characters
        ("/threads/thread_ABC%20123", True),  # still matches pattern
        ("/users/u_999%2Fprofile", False),  # encoded slash breaks pattern

        # Route with query parameters (should not match)
        ("/ui?foo=bar", False),
        ("/threads/thread_ABC123?foo=bar", False),
        ("/dashboard/stats?user=1", False),

        # Route with fragment
        ("/ui#section", False),
        ("/dashboard#main", False),
    ]
)
def test_edge_is_ui_route(route, expected):
    """Test edge cases for _is_ui_route."""
    codeflash_output = _is_ui_route(route) # 941μs -> 297μs (216% faster)

# 3. Large Scale Test Cases

def test_large_scale_many_allowed_routes_and_queries():
    """
    Test _is_ui_route with a large number of allowed routes and many input routes.
    """
    # Save original allowed routes
    orig_routes = MockLiteLLMRoutes.ui_routes.value.copy()

    # Add 900 more allowed routes
    for i in range(10, 910):
        MockLiteLLMRoutes.ui_routes.value.append(f"/ui/section{i}")

    # Test that all new allowed routes match exactly
    for i in range(10, 910):
        codeflash_output = _is_ui_route(f"/ui/section{i}") # 34.3ms -> 10.1ms (240% faster)

    # Test that variants with trailing slash also match (prefix logic)
    for i in range(10, 910):
        codeflash_output = _is_ui_route(f"/ui/section{i}/") # 34.4ms -> 10.1ms (241% faster)

    # Test that non-matching routes do not match
    for i in range(10, 910):
        codeflash_output = _is_ui_route(f"/api/section{i}") # 34.5ms -> 10.1ms (241% faster)

    # Test pattern matching with large number of patterns
    # Add 50 more patterns with parameters
    for i in range(910, 960):
        MockLiteLLMRoutes.ui_routes.value.append(f"/dynamic/{i}/{{param}}")

    # Test that routes matching those patterns return True
    for i in range(910, 960):
        codeflash_output = _is_ui_route(f"/dynamic/{i}/foobar") # 1.91ms -> 561μs (240% faster)
        codeflash_output = _is_ui_route(f"/dynamic/{i}/12345")

    # Test that routes with extra segments do not match
    for i in range(910, 960):
        codeflash_output = _is_ui_route(f"/dynamic/{i}/foobar/extra") # 1.91ms -> 557μs (243% faster)

    # Restore original allowed routes after test
    MockLiteLLMRoutes.ui_routes.value = orig_routes

def test_large_scale_performance():
    """
    Test performance does not degrade for 1000 queries.
    """
    # Add 990 more allowed routes for stress testing
    orig_routes = MockLiteLLMRoutes.ui_routes.value.copy()
    for i in range(10, 1000):
        MockLiteLLMRoutes.ui_routes.value.append(f"/ui/scale{i}")

    # Test 1000 queries
    for i in range(1000):
        if i < 10:
            route = orig_routes[i] if i < len(orig_routes) else f"/ui/scale{i}"
        else:
            route = f"/ui/scale{i}"
        codeflash_output = _is_ui_route(route) # 38.2ms -> 11.4ms (236% faster)

    # Test 1000 negative queries
    for i in range(1000):
        route = f"/notui/scale{i}"
        codeflash_output = _is_ui_route(route) # 38.2ms -> 11.3ms (239% faster)

    # Restore original allowed routes
    MockLiteLLMRoutes.ui_routes.value = orig_routes

# 4. Miscellaneous/Regression Cases

def test_regression_similar_prefixes():
    """
    Regression: route that starts with a similar prefix but not an allowed route.
    """
    codeflash_output = _is_ui_route("/uistuff") # 49.5μs -> 17.0μs (191% faster)
    codeflash_output = _is_ui_route("/dashboarding") # 39.2μs -> 12.0μs (227% faster)
    codeflash_output = _is_ui_route("/admin/settingspanel") # 39.4μs -> 11.7μs (237% faster)

def test_regression_pattern_edge_cases():
    """
    Regression: pattern matching with special characters in route.
    """
    # Route with dashes and underscores
    codeflash_output = _is_ui_route("/threads/thread-XYZ_123") # 44.0μs -> 14.4μs (204% faster)
    # Route with numbers in param
    codeflash_output = _is_ui_route("/users/12345/profile") # 39.6μs -> 11.9μs (234% faster)
    # Route with mixed alphanum in param
    codeflash_output = _is_ui_route("/users/user_ABC123/profile") # 38.5μs -> 11.4μs (238% faster)

def test_user_obj_ignored():
    """
    Test that user_obj parameter does not affect result.
    """
    codeflash_output = _is_ui_route("/ui", user_obj=MockLiteLLM_UserTable()) # 44.3μs -> 14.1μs (214% faster)
    codeflash_output = _is_ui_route("/api/v1/model", user_obj=MockLiteLLM_UserTable()) # 39.8μs -> 12.3μs (223% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import re

# imports
import pytest
from litellm.proxy.auth.auth_checks import _is_ui_route

# --- Mocks and Stubs for External Dependencies ---

# Minimal mock of LiteLLMRoutes with ui_routes.value as required by _is_ui_route
class LiteLLMRoutes:
    class ui_routes:
        # Some real UI routes, including static and parameterized ones
        value = [
            "/ui",
            "/ui/",
            "/ui/settings",
            "/ui/users",
            "/ui/users/{user_id}",
            "/ui/threads/{thread_id}",
            "/ui/keys",
            "/ui/keys/{key_id}/edit",
            "/ui/key/{token_id}/regenerate",
            "/ui/very/long/route/" + "x" * 900,  # For large scale
        ]

# Minimal mock of LiteLLM_UserTable (not used in logic but required by signature)
class LiteLLM_UserTable:
    pass
from litellm.proxy.auth.auth_checks import _is_ui_route

# ------------------------
# Basic Test Cases
# ------------------------

def test_exact_static_route():
    # Should return True for exact static UI route
    codeflash_output = _is_ui_route("/ui") # 44.3μs -> 13.6μs (227% faster)
    codeflash_output = _is_ui_route("/ui/") # 39.2μs -> 11.5μs (240% faster)
    codeflash_output = _is_ui_route("/ui/settings") # 39.0μs -> 11.4μs (243% faster)
    codeflash_output = _is_ui_route("/ui/keys") # 38.6μs -> 10.6μs (263% faster)

def test_exact_parameterized_route():
    # Should return True for parameterized UI routes with valid values
    codeflash_output = _is_ui_route("/ui/users/123") # 44.2μs -> 14.0μs (215% faster)
    codeflash_output = _is_ui_route("/ui/threads/thread_ABC123") # 39.5μs -> 12.0μs (229% faster)
    codeflash_output = _is_ui_route("/ui/keys/KEYID123/edit") # 39.0μs -> 11.4μs (241% faster)
    codeflash_output = _is_ui_route("/ui/key/token_456/regenerate") # 38.5μs -> 11.5μs (235% faster)

def test_route_with_extra_path():
    # Should return True if the route starts with a static allowed route
    codeflash_output = _is_ui_route("/ui/settings/extra") # 43.8μs -> 14.3μs (206% faster)
    codeflash_output = _is_ui_route("/ui/users/123/profile") # 39.5μs -> 11.8μs (234% faster)

def test_route_with_similar_prefix_but_not_ui():
    # Should return False for similar but not allowed routes
    codeflash_output = _is_ui_route("/uix") # 43.7μs -> 13.6μs (220% faster)
    codeflash_output = _is_ui_route("/uii/settings") # 39.3μs -> 12.0μs (227% faster)
    codeflash_output = _is_ui_route("/uiusers/123") # 38.7μs -> 11.5μs (237% faster)

def test_non_ui_routes():
    # Should return False for completely unrelated routes
    codeflash_output = _is_ui_route("/api/v1/chat") # 44.0μs -> 14.2μs (210% faster)
    codeflash_output = _is_ui_route("/admin") # 39.1μs -> 11.6μs (238% faster)
    codeflash_output = _is_ui_route("/login") # 1.92μs -> 1.46μs (31.4% faster)

# ------------------------
# Edge Test Cases
# ------------------------

def test_none_and_non_string_inputs():
    # Should return False for None or non-string inputs
    codeflash_output = _is_ui_route(None) # 6.36μs -> 1.33μs (377% faster)
    codeflash_output = _is_ui_route(12345) # 4.90μs -> 778ns (530% faster)
    codeflash_output = _is_ui_route(["/ui"]) # 4.47μs -> 513ns (771% faster)
    codeflash_output = _is_ui_route({"route": "/ui"}) # 4.58μs -> 538ns (751% faster)

def test_empty_string():
    # Should return False for empty string
    codeflash_output = _is_ui_route("") # 44.2μs -> 14.1μs (215% faster)

def test_trailing_and_leading_slashes():
    # Should handle slashes correctly
    codeflash_output = _is_ui_route("/ui/") # 44.1μs -> 14.0μs (215% faster)
    codeflash_output = _is_ui_route("ui/") # 39.0μs -> 11.5μs (241% faster)
    codeflash_output = _is_ui_route("/ui") # 38.5μs -> 11.1μs (247% faster)
    codeflash_output = _is_ui_route("/ui//") # 38.5μs -> 11.2μs (244% faster)

def test_parameterized_route_wrong_format():
    # Should return False for parameterized route with missing segments
    codeflash_output = _is_ui_route("/ui/users/") # 44.8μs -> 14.5μs (209% faster)
    codeflash_output = _is_ui_route("/ui/threads/") # 39.8μs -> 11.9μs (234% faster)
    codeflash_output = _is_ui_route("/ui/keys//edit") # 38.9μs -> 11.6μs (237% faster)

def test_parameterized_route_extra_segments():
    # Should return False if extra segments make it not match any pattern
    codeflash_output = _is_ui_route("/ui/users/123/extra") # 43.8μs -> 14.4μs (204% faster)
    codeflash_output = _is_ui_route("/ui/keys/KEYID123/edit/extra") # 38.9μs -> 11.8μs (231% faster)
    # But if the extra segment is before the parameter, should be False
    codeflash_output = _is_ui_route("/ui/users/extra/123") # 39.1μs -> 11.5μs (239% faster)

def test_case_sensitivity():
    # Should be case-sensitive
    codeflash_output = _is_ui_route("/UI") # 43.3μs -> 13.6μs (219% faster)
    codeflash_output = _is_ui_route("/Ui/settings") # 39.7μs -> 12.1μs (228% faster)
    codeflash_output = _is_ui_route("/ui/Settings") # 38.8μs -> 11.1μs (251% faster)

def test_partial_match():
    # Should not match partial routes
    codeflash_output = _is_ui_route("/u") # 44.1μs -> 13.9μs (217% faster)
    codeflash_output = _is_ui_route("/uiuser") # 39.6μs -> 11.7μs (237% faster)

def test_route_with_special_characters():
    # Parameterized values may contain dashes or underscores
    codeflash_output = _is_ui_route("/ui/users/user-123_ABC") # 44.2μs -> 14.4μs (207% faster)
    codeflash_output = _is_ui_route("/ui/threads/thread-XYZ_789") # 39.4μs -> 12.0μs (227% faster)

def test_route_with_query_string():
    # Should not match if query string is present (since pattern anchors to end)
    codeflash_output = _is_ui_route("/ui/settings?tab=general") # 44.2μs -> 14.4μs (207% faster)
    codeflash_output = _is_ui_route("/ui/users/123?active=true") # 39.5μs -> 11.7μs (238% faster)

def test_route_with_fragment():
    # Should not match if fragment is present
    codeflash_output = _is_ui_route("/ui/settings#section") # 43.6μs -> 14.2μs (208% faster)

# ------------------------
# Large Scale Test Cases
# ------------------------


def test_large_route_string():
    # Test with a very long route string (close to 1000 chars)
    long_id = "x" * 900
    route = f"/ui/very/long/route/{long_id}"
    codeflash_output = _is_ui_route(route) # 51.2μs -> 18.2μs (181% faster)
    # Should not match if one character is missing
    codeflash_output = _is_ui_route(f"/ui/very/long/route/{long_id[:-1]}") # 39.7μs -> 11.9μs (233% faster)


def test_mutation_guard_prefix_vs_pattern():
    # If startswith logic is removed, these would fail
    codeflash_output = _is_ui_route("/ui/settings/advanced") # 50.2μs -> 18.1μs (176% faster)
    # If pattern logic is removed, these would fail
    codeflash_output = _is_ui_route("/ui/users/abc123") # 39.8μs -> 11.8μs (236% faster)
    # If both are removed, everything would fail

def test_mutation_guard_pattern_substitution():
    # If the regex pattern is not correctly substituting {param}, these would fail
    codeflash_output = _is_ui_route("/ui/keys/KEYID123/edit") # 45.2μs -> 14.6μs (210% faster)
    codeflash_output = _is_ui_route("/ui/threads/thread_ABC123") # 39.7μs -> 11.8μs (238% faster)

def test_mutation_guard_type_checking():
    # If type checking is removed, these would fail
    codeflash_output = _is_ui_route(123) # 6.56μs -> 1.43μs (359% faster)
    codeflash_output = _is_ui_route(None) # 4.79μs -> 632ns (657% faster)

def test_mutation_guard_exact_match_vs_partial():
    # Should not match partials
    codeflash_output = _is_ui_route("/ui/keys/KEYID123") # 45.9μs -> 14.8μs (210% faster)
    codeflash_output = _is_ui_route("/ui/keys/KEYID123/ed") # 39.8μs -> 11.7μs (240% faster)
    codeflash_output = _is_ui_route("/ui/keys/KEYID123/edit") # 39.0μs -> 11.2μs (247% faster)
    codeflash_output = _is_ui_route("/ui/keys/KEYID123/edit/extra") # 38.5μs -> 11.5μs (235% faster)
    codeflash_output = _is_ui_route("/ui/keys/") # 38.5μs -> 11.3μs (241% faster)
# 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-_is_ui_route-mhwtfox3 and push.

Codeflash Static Badge

The optimization achieves a **238% speedup** by eliminating the primary performance bottleneck: repeated regex compilation in route pattern matching.

**Key Optimizations:**

1. **Regex Compilation Caching**: The original code compiled the same regex patterns repeatedly via `re.sub()` and `re.match()` on every call to `_route_matches_pattern()`. The optimized version introduces `@lru_cache(maxsize=128)` on `_get_compiled_route_regex()` to cache compiled regex objects, avoiding expensive recompilation for repeated patterns.

2. **Single-Pass Route Checking**: The original `_is_ui_route()` used two separate `any()` loops - first checking `startswith()` matches, then pattern matches. The optimization combines both checks into a single loop with short-circuit logic, reducing iterations over `allowed_routes` from 2N to N operations.

**Performance Impact:**
- Line profiler shows `_route_matches_pattern()` time dropped from 713ms to 206ms (71% reduction)
- The regex compilation lines (originally consuming 81.6% of function time) now benefit from caching
- Single-loop optimization reduces overall `_is_ui_route()` execution time by 42%

**Workload Benefits:**
Based on `function_references`, `_is_ui_route()` is called from `_is_allowed_route()` for every UI token validation request. This optimization is particularly valuable for:
- **High-frequency UI authentication** where the same route patterns are validated repeatedly
- **Large-scale deployments** with many concurrent UI requests (test results show 236-241% speedup on large-scale scenarios)
- **Applications with complex route patterns** containing parameters like `/threads/{thread_id}` where regex compilation overhead is significant

The caching approach is especially effective since UI routes typically follow predictable patterns that repeat across requests, making the LRU cache highly efficient.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 13, 2025 02:34
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Nov 13, 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