Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 7% (0.07x) speedup for _check_wildcard_routing in litellm/proxy/auth/model_checks.py

⏱️ Runtime : 760 microseconds 713 microseconds (best of 185 runs)

📝 Explanation and details

The optimization simplifies the function by eliminating the unnecessary conditional branching. Instead of using an if statement to check "*" in model and then returning True or False in separate branches, the optimized version directly returns the boolean result of "*" in model.

Key changes:

  • Removed the if "*" in model: conditional check and explicit return True/return False statements
  • Replaced with a single return "*" in model statement that directly returns the boolean evaluation

Why this improves performance:

  1. Reduced branching overhead: Eliminates the conditional jump instructions that the CPU must evaluate and potentially mispredict
  2. Fewer Python bytecode operations: The optimized version generates fewer bytecode instructions, reducing interpreter overhead
  3. Direct boolean evaluation: Python's in operator already returns a boolean, so wrapping it in conditional logic adds unnecessary work

The line profiler shows the optimization reduces total execution time from 2.21ms to 1.20ms (46% reduction in profiled time), with the per-hit cost dropping from ~250ns to ~242ns.

Test case performance patterns:

  • Best improvements (5-12% faster): Simple model names without wildcards, strings with special characters, and large-scale batch processing
  • Minimal impact (0-3%): Short strings with wildcards where the * is found early
  • Consistent gains: The optimization shows consistent improvements across edge cases, unicode strings, and long strings, making it universally beneficial regardless of input characteristics

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4956 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest  # used for our unit tests
from litellm.proxy.auth.model_checks import _check_wildcard_routing

# unit tests

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

def test_basic_provider_wildcard():
    # Should return True for provider wildcard
    codeflash_output = _check_wildcard_routing("anthropic/*") # 369ns -> 390ns (5.38% slower)
    codeflash_output = _check_wildcard_routing("openai/*") # 244ns -> 242ns (0.826% faster)

def test_basic_global_wildcard():
    # Should return True for global wildcard
    codeflash_output = _check_wildcard_routing("*") # 375ns -> 391ns (4.09% slower)

def test_basic_non_wildcard():
    # Should return False for regular model names
    codeflash_output = _check_wildcard_routing("anthropic/claude-2") # 425ns -> 407ns (4.42% faster)
    codeflash_output = _check_wildcard_routing("openai/gpt-4") # 287ns -> 316ns (9.18% slower)
    codeflash_output = _check_wildcard_routing("gpt-3.5-turbo") # 186ns -> 166ns (12.0% faster)

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

def test_edge_empty_string():
    # Should return False for empty string
    codeflash_output = _check_wildcard_routing("") # 360ns -> 393ns (8.40% slower)

def test_edge_wildcard_in_middle():
    # Should return True if * appears anywhere in the string
    codeflash_output = _check_wildcard_routing("foo*bar") # 399ns -> 396ns (0.758% faster)
    codeflash_output = _check_wildcard_routing("abc*") # 237ns -> 224ns (5.80% faster)
    codeflash_output = _check_wildcard_routing("*xyz") # 183ns -> 183ns (0.000% faster)

def test_edge_multiple_wildcards():
    # Should return True if multiple * present
    codeflash_output = _check_wildcard_routing("a*b*c") # 391ns -> 362ns (8.01% faster)
    codeflash_output = _check_wildcard_routing("**") # 246ns -> 245ns (0.408% faster)

def test_edge_only_slash():
    # Should return False for a string with only a slash
    codeflash_output = _check_wildcard_routing("/") # 361ns -> 377ns (4.24% slower)

def test_edge_spaces_and_wildcard():
    # Should return True if * is present, even with spaces
    codeflash_output = _check_wildcard_routing(" * ") # 335ns -> 331ns (1.21% faster)
    codeflash_output = _check_wildcard_routing("openai / *") # 270ns -> 275ns (1.82% slower)

def test_edge_unicode_characters():
    # Should return True if * is present, even with unicode
    codeflash_output = _check_wildcard_routing("模型*") # 457ns -> 467ns (2.14% slower)
    codeflash_output = _check_wildcard_routing("模型") # 297ns -> 277ns (7.22% faster)

def test_edge_wildcard_with_numbers():
    # Should return True if * is present with numbers
    codeflash_output = _check_wildcard_routing("123*456") # 337ns -> 347ns (2.88% slower)

def test_edge_wildcard_in_long_string():
    # Should return True if * is present in a long string
    long_string = "a" * 100 + "*" + "b" * 100
    codeflash_output = _check_wildcard_routing(long_string) # 459ns -> 450ns (2.00% faster)

def test_edge_wildcard_in_various_positions():
    # Should return True for wildcard at different positions
    codeflash_output = _check_wildcard_routing("*abc") # 358ns -> 368ns (2.72% slower)
    codeflash_output = _check_wildcard_routing("abc*") # 264ns -> 264ns (0.000% faster)
    codeflash_output = _check_wildcard_routing("ab*cd") # 173ns -> 169ns (2.37% faster)

def test_edge_wildcard_with_special_chars():
    # Should return True for wildcard with other special characters
    codeflash_output = _check_wildcard_routing("!@#*") # 347ns -> 335ns (3.58% faster)
    codeflash_output = _check_wildcard_routing("!@#") # 257ns -> 258ns (0.388% slower)

def test_edge_wildcard_with_newline():
    # Should return True if * is present with newline
    codeflash_output = _check_wildcard_routing("*\n") # 315ns -> 353ns (10.8% slower)
    codeflash_output = _check_wildcard_routing("\n*") # 262ns -> 238ns (10.1% faster)
    codeflash_output = _check_wildcard_routing("\n") # 200ns -> 162ns (23.5% faster)

def test_edge_wildcard_with_tab():
    # Should return True if * is present with tab
    codeflash_output = _check_wildcard_routing("\t*") # 354ns -> 352ns (0.568% faster)
    codeflash_output = _check_wildcard_routing("*\t") # 234ns -> 256ns (8.59% slower)
    codeflash_output = _check_wildcard_routing("\t") # 205ns -> 184ns (11.4% faster)

def test_edge_wildcard_with_multiple_slashes():
    # Should return True if * is present with multiple slashes
    codeflash_output = _check_wildcard_routing("openai///***") # 361ns -> 383ns (5.74% slower)
    codeflash_output = _check_wildcard_routing("openai///") # 278ns -> 262ns (6.11% faster)

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

def test_large_scale_all_non_wildcard():
    # Should return False for a large list of non-wildcard names
    for i in range(500):
        codeflash_output = _check_wildcard_routing(f"model_{i}") # 73.2μs -> 68.7μs (6.65% faster)

def test_large_scale_all_wildcard():
    # Should return True for a large list of wildcard names
    for i in range(500):
        codeflash_output = _check_wildcard_routing(f"provider_{i}/*") # 72.6μs -> 70.5μs (2.96% faster)

def test_large_scale_mixed():
    # Mix of wildcards and non-wildcards
    for i in range(250):
        codeflash_output = _check_wildcard_routing(f"provider_{i}/*") # 38.3μs -> 36.4μs (5.35% faster)
        codeflash_output = _check_wildcard_routing(f"model_{i}")

def test_large_scale_random_positions():
    # Test * in random positions in large strings
    for i in range(100):
        s = "a" * i + "*" + "b" * (100 - i)
        codeflash_output = _check_wildcard_routing(s) # 15.2μs -> 14.8μs (3.22% faster)

def test_large_scale_long_strings_without_wildcard():
    # Should return False for long strings without *
    for i in range(100):
        s = "a" * i + "b" * (100 - i)
        codeflash_output = _check_wildcard_routing(s) # 15.4μs -> 14.6μs (5.94% faster)

def test_large_scale_edge_cases():
    # Test strings with many special characters, with and without *
    for i in range(100):
        s = "!@#$%^&" * i
        codeflash_output = _check_wildcard_routing(s) # 17.9μs -> 16.7μs (7.21% faster)
        s = "!@#$%^&*" + ("!@#$%^&" * i)
        codeflash_output = _check_wildcard_routing(s)

# ----------------------------
# Determinism Test
# ----------------------------


#------------------------------------------------
import pytest  # used for our unit tests
from litellm.proxy.auth.model_checks import _check_wildcard_routing

# unit tests

# 1. Basic Test Cases

def test_star_only():
    # Test with only a star
    codeflash_output = _check_wildcard_routing("*") # 441ns -> 422ns (4.50% faster)

def test_provider_star():
    # Test with provider followed by slash and star
    codeflash_output = _check_wildcard_routing("anthropic/*") # 406ns -> 399ns (1.75% faster)
    codeflash_output = _check_wildcard_routing("openai/*") # 222ns -> 221ns (0.452% faster)

def test_no_wildcard():
    # Test with a regular model name (no wildcard)
    codeflash_output = _check_wildcard_routing("gpt-3.5-turbo") # 392ns -> 367ns (6.81% faster)
    codeflash_output = _check_wildcard_routing("anthropic/claude-v1") # 260ns -> 272ns (4.41% slower)

# 2. Edge Test Cases

def test_empty_string():
    # Test with an empty string
    codeflash_output = _check_wildcard_routing("") # 335ns -> 342ns (2.05% slower)

def test_star_in_middle():
    # Test with a star in the middle of the string (should still return True)
    codeflash_output = _check_wildcard_routing("gpt*3.5-turbo") # 388ns -> 423ns (8.27% slower)

def test_star_at_end():
    # Test with a star at the end (not after slash)
    codeflash_output = _check_wildcard_routing("gpt-3.5-turbo*") # 440ns -> 422ns (4.27% faster)

def test_multiple_stars():
    # Test with multiple stars in the string
    codeflash_output = _check_wildcard_routing("*/gpt*") # 425ns -> 401ns (5.99% faster)
    codeflash_output = _check_wildcard_routing("openai*/*") # 265ns -> 284ns (6.69% slower)

def test_star_with_spaces():
    # Test with spaces and a star
    codeflash_output = _check_wildcard_routing(" * ") # 406ns -> 365ns (11.2% faster)
    codeflash_output = _check_wildcard_routing("openai/ *") # 276ns -> 254ns (8.66% faster)
    codeflash_output = _check_wildcard_routing("openai/* ") # 206ns -> 172ns (19.8% faster)

def test_non_ascii_characters():
    # Test with non-ascii and a star
    codeflash_output = _check_wildcard_routing("模型*") # 472ns -> 444ns (6.31% faster)
    codeflash_output = _check_wildcard_routing("模型") # 273ns -> 262ns (4.20% faster)

def test_star_as_part_of_word():
    # Test with star embedded in a word
    codeflash_output = _check_wildcard_routing("star*fish") # 371ns -> 375ns (1.07% slower)
    codeflash_output = _check_wildcard_routing("fishstar") # 287ns -> 286ns (0.350% faster)

def test_only_slash():
    # Test with only a slash (no star)
    codeflash_output = _check_wildcard_routing("/") # 391ns -> 361ns (8.31% faster)

def test_only_provider_slash():
    # Test with only provider and slash
    codeflash_output = _check_wildcard_routing("openai/") # 393ns -> 391ns (0.512% faster)

def test_star_in_provider():
    # Test with star in provider name
    codeflash_output = _check_wildcard_routing("open*ai/gpt-3.5") # 373ns -> 369ns (1.08% faster)

def test_star_and_other_wildcards():
    # Test with other wildcards (should only care about *)
    codeflash_output = _check_wildcard_routing("openai/?") # 387ns -> 362ns (6.91% faster)
    codeflash_output = _check_wildcard_routing("openai/[a-z]") # 274ns -> 273ns (0.366% faster)

# 3. Large Scale Test Cases

def test_large_list_of_models_no_wildcard():
    # Test a large list of models, none with wildcard
    models = [f"model_{i}" for i in range(1000)]
    for model in models:
        codeflash_output = _check_wildcard_routing(model) # 150μs -> 138μs (8.49% faster)

def test_large_list_of_models_with_wildcards():
    # Test a large list of models, all with wildcard at different positions
    models = [f"provider_{i}/*" for i in range(500)] + [f"model_{i}*extra" for i in range(500)]
    for model in models:
        codeflash_output = _check_wildcard_routing(model) # 148μs -> 139μs (6.82% faster)

def test_mixed_large_list():
    # Test a mixed large list, some with, some without wildcards
    models = []
    expected = []
    for i in range(500):
        models.append(f"model_{i}")
        expected.append(False)
        models.append(f"provider_{i}/*")
        expected.append(True)
    for model, exp in zip(models, expected):
        codeflash_output = _check_wildcard_routing(model) # 154μs -> 144μs (7.31% faster)

def test_long_string_with_star():
    # Test with a very long string containing a star
    long_model = "a" * 500 + "*" + "b" * 499
    codeflash_output = _check_wildcard_routing(long_model) # 603ns -> 582ns (3.61% faster)

def test_long_string_without_star():
    # Test with a very long string without a star
    long_model = "a" * 1000
    codeflash_output = _check_wildcard_routing(long_model) # 464ns -> 447ns (3.80% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from litellm.proxy.auth.model_checks import _check_wildcard_routing

def test__check_wildcard_routing():
    _check_wildcard_routing('')

def test__check_wildcard_routing_2():
    _check_wildcard_routing('*')
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_zbim32de/tmpavie8szb/test_concolic_coverage.py::test__check_wildcard_routing 540ns 480ns 12.5%✅
codeflash_concolic_zbim32de/tmpavie8szb/test_concolic_coverage.py::test__check_wildcard_routing_2 414ns 402ns 2.99%✅

To edit these changes git checkout codeflash/optimize-_check_wildcard_routing-mhdz7ks4 and push.

Codeflash Static Badge

The optimization simplifies the function by eliminating the unnecessary conditional branching. Instead of using an `if` statement to check `"*" in model` and then returning `True` or `False` in separate branches, the optimized version directly returns the boolean result of `"*" in model`.

**Key changes:**
- Removed the `if "*" in model:` conditional check and explicit `return True`/`return False` statements
- Replaced with a single `return "*" in model` statement that directly returns the boolean evaluation

**Why this improves performance:**
1. **Reduced branching overhead**: Eliminates the conditional jump instructions that the CPU must evaluate and potentially mispredict
2. **Fewer Python bytecode operations**: The optimized version generates fewer bytecode instructions, reducing interpreter overhead
3. **Direct boolean evaluation**: Python's `in` operator already returns a boolean, so wrapping it in conditional logic adds unnecessary work

The line profiler shows the optimization reduces total execution time from 2.21ms to 1.20ms (46% reduction in profiled time), with the per-hit cost dropping from ~250ns to ~242ns.

**Test case performance patterns:**
- **Best improvements** (5-12% faster): Simple model names without wildcards, strings with special characters, and large-scale batch processing
- **Minimal impact** (0-3%): Short strings with wildcards where the `*` is found early
- **Consistent gains**: The optimization shows consistent improvements across edge cases, unicode strings, and long strings, making it universally beneficial regardless of input characteristics
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 22:08
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium 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: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant