Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 47% (0.47x) speedup for BrotliMiddleware._is_handler_excluded in gradio/brotli_middleware.py

⏱️ Runtime : 348 microseconds 238 microseconds (best of 41 runs)

📝 Explanation and details

The optimization improves the _is_handler_excluded method by replacing the any() generator expression with an explicit early-return loop structure and adding a short-circuit check for empty excluded handlers.

Key optimizations applied:

  1. Early return for empty handlers: Added if not self.excluded_handlers: return False to immediately return when no patterns exist, avoiding unnecessary iteration setup.

  2. Explicit loop with early termination: Replaced any(pattern.search(handler) for pattern in self.excluded_handlers) with a direct for-loop that returns True immediately upon finding the first match, eliminating the generator overhead and function call overhead of any().

Why this is faster:

  • Eliminates generator overhead: The any() function with a generator expression creates additional Python object overhead for each iteration, while the explicit loop operates directly on the iterable.
  • Reduces function call overhead: any() adds an extra function call layer that's eliminated in the optimized version.
  • Short-circuits empty cases: When excluded_handlers is empty (common case), the optimization immediately returns without any iteration setup.

Performance impact based on test results:

  • Empty handlers case: Shows dramatic improvements (121-197% faster) as the short-circuit completely avoids iteration overhead.
  • Pattern matching cases: Consistent 30-90% improvements across all scenarios, with early matches benefiting most from immediate returns.
  • Large-scale scenarios: With 500-1000 patterns, still achieves 28-32% speedup despite the overhead of checking many patterns.

The optimization is particularly valuable since _is_handler_excluded is called for every HTTP request in the ASGI middleware pipeline, making even small per-call improvements significant for high-traffic applications.

Correctness verification report:

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

# imports
import pytest
from gradio.brotli_middleware import BrotliMiddleware

# unit tests

# ----------- BASIC TEST CASES -----------

def test_basic_no_excluded_handlers_returns_false():
    """If excluded_handlers is empty, should always return False."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[])
    codeflash_output = mw._is_handler_excluded({"path": "/test"}) # 2.33μs -> 961ns (143% faster)
    codeflash_output = mw._is_handler_excluded({"path": ""}) # 558ns -> 252ns (121% faster)

def test_basic_exact_match_returns_true():
    """If the handler matches exactly one of the excluded patterns, return True."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"^/api/private$"])
    codeflash_output = mw._is_handler_excluded({"path": "/api/private"}) # 3.90μs -> 2.02μs (92.7% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/api/public"}) # 1.00μs -> 524ns (91.0% faster)

def test_basic_partial_match_returns_true():
    """If the handler matches a substring pattern, return True."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"private"])
    codeflash_output = mw._is_handler_excluded({"path": "/api/private"}) # 2.70μs -> 1.45μs (87.1% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/api/public"}) # 961ns -> 641ns (49.9% faster)

def test_basic_multiple_patterns():
    """If any pattern matches, return True; otherwise, return False."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"private", r"admin"])
    codeflash_output = mw._is_handler_excluded({"path": "/api/private"}) # 2.56μs -> 1.42μs (80.6% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/admin/dashboard"}) # 1.11μs -> 768ns (44.4% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/user/home"}) # 938ns -> 601ns (56.1% faster)

# ----------- EDGE TEST CASES -----------

def test_edge_scope_without_path_key():
    """If scope has no 'path' key, should use empty string."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r".*"])
    codeflash_output = mw._is_handler_excluded({}) # 3.47μs -> 2.20μs (57.4% faster)
    mw2 = BrotliMiddleware(app=None, excluded_handlers=[r"nonmatch"])
    codeflash_output = mw2._is_handler_excluded({}) # 1.38μs -> 783ns (76.4% faster)

def test_edge_empty_path_and_empty_pattern():
    """Empty path and empty pattern: should match."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[""])
    codeflash_output = mw._is_handler_excluded({"path": ""}) # 3.06μs -> 1.69μs (81.4% faster)

def test_edge_path_is_none():
    """If path is None, should treat as empty string."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r".*"])
    codeflash_output = mw._is_handler_excluded({"path": None})
    mw2 = BrotliMiddleware(app=None, excluded_handlers=[r"nonmatch"])
    codeflash_output = mw2._is_handler_excluded({"path": None})

def test_edge_pattern_is_complex_regex():
    """Test with complex regex patterns."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"^/api/(private|secret)/\d+$"])
    codeflash_output = mw._is_handler_excluded({"path": "/api/private/123"}) # 4.14μs -> 2.77μs (49.6% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/api/secret/999"}) # 1.34μs -> 865ns (54.6% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/api/public/123"}) # 1.25μs -> 826ns (51.9% faster)

def test_edge_pattern_is_case_sensitive():
    """Regex is case sensitive by default."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"/PRIVATE"])
    codeflash_output = mw._is_handler_excluded({"path": "/PRIVATE"}) # 2.64μs -> 1.40μs (88.6% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/private"}) # 1.09μs -> 663ns (64.6% faster)

def test_edge_pattern_with_special_chars():
    """Pattern with special regex chars."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"\.json$"])
    codeflash_output = mw._is_handler_excluded({"path": "/data/file.json"}) # 3.35μs -> 1.92μs (74.8% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/data/file.txt"}) # 1.09μs -> 719ns (52.0% faster)

def test_edge_empty_excluded_handlers_is_none():
    """excluded_handlers=None should behave same as empty list."""
    mw = BrotliMiddleware(app=None, excluded_handlers=None)
    codeflash_output = mw._is_handler_excluded({"path": "/anything"}) # 1.54μs -> 635ns (143% faster)

def test_edge_path_is_non_string():
    """If path is not a string, but e.g. int, should convert to str for regex."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"^123$"])
    # The implementation does not convert to str, so this will not match unless path is str or None
    # Let's test what happens
    codeflash_output = mw._is_handler_excluded({"path": 123})  # Should not match, since 123 is not a string

def test_edge_pattern_matches_empty_string_only():
    """Pattern that matches only empty string."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"^$"])
    codeflash_output = mw._is_handler_excluded({"path": ""}) # 3.57μs -> 2.00μs (78.9% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/notempty"}) # 1.50μs -> 977ns (53.0% faster)

# ----------- LARGE SCALE TEST CASES -----------

def test_large_many_excluded_patterns():
    """Test with a large number of patterns, only one matches."""
    patterns = [rf"/api/endpoint{i}" for i in range(500)]
    mw = BrotliMiddleware(app=None, excluded_handlers=patterns)
    # Should match the last pattern
    codeflash_output = mw._is_handler_excluded({"path": "/api/endpoint499"}) # 5.34μs -> 2.81μs (90.0% faster)
    # Should not match any pattern
    codeflash_output = mw._is_handler_excluded({"path": "/api/endpoint500"}) # 1.39μs -> 1.02μs (36.7% faster)

def test_large_long_path_string():
    """Test with a very long path string."""
    long_path = "/api/" + "a" * 900
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"/api/a{900}$"])
    codeflash_output = mw._is_handler_excluded({"path": long_path}) # 4.53μs -> 2.90μs (56.3% faster)

def test_large_many_non_matching_patterns():
    """Test with many patterns, none of which match."""
    patterns = [rf"/api/endpoint{i}" for i in range(500)]
    mw = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = mw._is_handler_excluded({"path": "/api/other"}) # 45.3μs -> 35.4μs (28.0% faster)

def test_large_all_patterns_match():
    """If multiple patterns all match, should still return True."""
    patterns = [r"/api", r"/api/.*", r"api"]
    mw = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = mw._is_handler_excluded({"path": "/api/anything"}) # 2.82μs -> 1.38μs (104% faster)

def test_large_first_pattern_matches():
    """If only the first pattern matches, should return True (short-circuit)."""
    patterns = [r"/match", r"/notmatch1", r"/notmatch2"]
    mw = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = mw._is_handler_excluded({"path": "/match"}) # 2.75μs -> 1.46μs (88.6% faster)

def test_large_last_pattern_matches():
    """If only the last pattern matches, should return True."""
    patterns = [r"/notmatch1", r"/notmatch2", r"/match$"]
    mw = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = mw._is_handler_excluded({"path": "/match"}) # 3.69μs -> 2.38μs (54.8% faster)

def test_large_pattern_with_wildcards():
    """Test with a pattern containing wildcards."""
    mw = BrotliMiddleware(app=None, excluded_handlers=[r"/api/.*"])
    codeflash_output = mw._is_handler_excluded({"path": "/api/anything"}) # 3.54μs -> 2.00μs (76.9% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/api/" + "x" * 100}) # 1.33μs -> 821ns (62.2% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/other"}) # 974ns -> 558ns (74.6% faster)

def test_large_pattern_with_numbers():
    """Test with patterns containing numbers."""
    patterns = [rf"/item/{i}" for i in range(100)]
    mw = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = mw._is_handler_excluded({"path": "/item/99"}) # 4.22μs -> 2.54μs (66.3% faster)
    codeflash_output = mw._is_handler_excluded({"path": "/item/100"}) # 1.17μs -> 677ns (73.1% 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 gradio.brotli_middleware import BrotliMiddleware

# unit tests

# Basic Test Cases

def test_no_excluded_handlers_returns_false():
    # No excluded handlers, should always return False
    middleware = BrotliMiddleware(app=None, excluded_handlers=None)
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 1.62μs -> 695ns (133% faster)
    codeflash_output = middleware._is_handler_excluded({"path": ""}) # 634ns -> 233ns (172% faster)

def test_exact_match_in_excluded_handlers_returns_true():
    # Exact match in excluded_handlers should return True
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"^/api/data$"])
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 3.38μs -> 1.99μs (69.5% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data/"}) # 1.54μs -> 1.11μs (38.8% faster)

def test_partial_match_in_excluded_handlers_returns_true():
    # Regex partial match should return True
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/api"])
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 2.71μs -> 1.46μs (85.5% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/api"}) # 948ns -> 527ns (79.9% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/other"}) # 839ns -> 527ns (59.2% faster)

def test_multiple_patterns_one_matches_returns_true():
    # Multiple patterns, one matches
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/foo", r"/bar"])
    codeflash_output = middleware._is_handler_excluded({"path": "/foo"}) # 2.61μs -> 1.37μs (90.4% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/bar"}) # 1.13μs -> 763ns (48.6% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/baz"}) # 809ns -> 505ns (60.2% faster)

def test_multiple_patterns_none_match_returns_false():
    # Multiple patterns, none match
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/foo", r"/bar"])
    codeflash_output = middleware._is_handler_excluded({"path": "/baz"}) # 2.46μs -> 1.42μs (72.8% faster)

# Edge Test Cases

def test_empty_path_returns_false():
    # Empty path should not match any pattern unless pattern matches empty string
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/api"])
    codeflash_output = middleware._is_handler_excluded({"path": ""}) # 2.06μs -> 1.13μs (82.1% faster)

def test_pattern_matching_empty_string():
    # Pattern that matches empty string
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"^$"])
    codeflash_output = middleware._is_handler_excluded({"path": ""}) # 3.15μs -> 1.92μs (63.8% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/api"}) # 1.24μs -> 898ns (38.0% faster)

def test_path_key_missing_in_scope():
    # Scope without 'path' key should default to empty string
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"^$"])
    codeflash_output = middleware._is_handler_excluded({}) # 3.26μs -> 1.80μs (81.5% faster)

def test_pattern_with_special_regex_characters():
    # Patterns with regex special characters
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/api/\d+", r"/foo\w+"])
    codeflash_output = middleware._is_handler_excluded({"path": "/api/123"}) # 3.77μs -> 2.35μs (60.6% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/fooabc"}) # 1.72μs -> 1.18μs (46.1% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/bar"}) # 881ns -> 489ns (80.2% faster)

def test_pattern_with_wildcard():
    # Pattern with wildcard
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/static/.*"])
    codeflash_output = middleware._is_handler_excluded({"path": "/static/file.txt"}) # 3.27μs -> 2.05μs (59.0% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/static/"}) # 1.07μs -> 688ns (55.4% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/dynamic/file.txt"}) # 960ns -> 533ns (80.1% faster)

def test_unicode_path_and_pattern():
    # Unicode in path and pattern
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/привет"])
    codeflash_output = middleware._is_handler_excluded({"path": "/привет"}) # 2.85μs -> 1.64μs (74.2% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/hello"}) # 1.07μs -> 523ns (105% faster)

def test_pattern_matches_substring_not_start():
    # Pattern matches substring not at start
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"data"])
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 2.74μs -> 1.45μs (88.5% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/api/metadata"}) # 988ns -> 509ns (94.1% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/api/info"}) # 881ns -> 485ns (81.6% faster)

def test_pattern_with_anchors():
    # Pattern with start/end anchors
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"^/api/data$"])
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 3.08μs -> 1.86μs (65.8% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data/"}) # 1.67μs -> 1.08μs (53.9% faster)

def test_excluded_handlers_is_empty_list():
    # Excluded handlers is empty list, should always return False
    middleware = BrotliMiddleware(app=None, excluded_handlers=[])
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 1.56μs -> 526ns (197% faster)

# Large Scale Test Cases

def test_large_number_of_patterns_performance_and_correctness():
    # Large number of patterns, only one matches
    patterns = [fr"/foo{i}" for i in range(500)] + [r"/target"]
    middleware = BrotliMiddleware(app=None, excluded_handlers=patterns)
    # Should match only /target
    codeflash_output = middleware._is_handler_excluded({"path": "/target"}) # 41.4μs -> 31.5μs (31.5% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/foo499"}) # 1.38μs -> 860ns (59.9% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/bar"}) # 33.2μs -> 25.3μs (31.2% faster)

def test_large_number_of_patterns_none_match():
    # Large number of patterns, none match
    patterns = [fr"/foo{i}" for i in range(1000)]
    middleware = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = middleware._is_handler_excluded({"path": "/bar"}) # 75.0μs -> 56.7μs (32.2% faster)

def test_large_path_string_with_matching_pattern():
    # Large path string, pattern matches somewhere in the middle
    long_path = "/a" * 100 + "/target"
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/target"])
    codeflash_output = middleware._is_handler_excluded({"path": long_path}) # 3.54μs -> 1.95μs (82.1% faster)

def test_large_path_string_with_no_matching_pattern():
    # Large path string, no pattern matches
    long_path = "/a" * 100
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"/target"])
    codeflash_output = middleware._is_handler_excluded({"path": long_path}) # 2.48μs -> 1.65μs (50.1% faster)

def test_large_pattern_matches_large_path():
    # Large pattern matches large path
    pattern = r"/a" * 100
    long_path = "/a" * 100
    middleware = BrotliMiddleware(app=None, excluded_handlers=[pattern])
    codeflash_output = middleware._is_handler_excluded({"path": long_path}) # 3.00μs -> 1.75μs (71.8% faster)

def test_all_patterns_match():
    # All patterns match the path
    patterns = [r"/api", r"api", r"/api/data"]
    middleware = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = middleware._is_handler_excluded({"path": "/api/data"}) # 2.72μs -> 1.40μs (94.8% faster)

def test_some_patterns_match_some_do_not():
    # Some patterns match, some do not
    patterns = [r"/foo", r"/bar", r"/baz"]
    middleware = BrotliMiddleware(app=None, excluded_handlers=patterns)
    codeflash_output = middleware._is_handler_excluded({"path": "/bar"}) # 3.03μs -> 1.72μs (75.8% faster)
    codeflash_output = middleware._is_handler_excluded({"path": "/qux"}) # 1.18μs -> 635ns (86.0% faster)

def test_pattern_is_empty_string():
    # Pattern is empty string, matches everything
    middleware = BrotliMiddleware(app=None, excluded_handlers=[""])
    codeflash_output = middleware._is_handler_excluded({"path": "/anything"}) # 3.17μs -> 1.79μs (76.9% faster)
    codeflash_output = middleware._is_handler_excluded({"path": ""}) # 978ns -> 599ns (63.3% faster)

def test_path_is_none():
    # Path is None, should default to empty string
    middleware = BrotliMiddleware(app=None, excluded_handlers=[r"^$"])
    codeflash_output = middleware._is_handler_excluded({"path": None})

def test_pattern_is_none_should_raise():
    # If pattern is None, re.compile should raise TypeError
    with pytest.raises(TypeError):
        BrotliMiddleware(app=None, excluded_handlers=[None])
# 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-BrotliMiddleware._is_handler_excluded-mhwqarci and push.

Codeflash Static Badge

The optimization improves the `_is_handler_excluded` method by replacing the `any()` generator expression with an explicit early-return loop structure and adding a short-circuit check for empty excluded handlers.

**Key optimizations applied:**

1. **Early return for empty handlers**: Added `if not self.excluded_handlers: return False` to immediately return when no patterns exist, avoiding unnecessary iteration setup.

2. **Explicit loop with early termination**: Replaced `any(pattern.search(handler) for pattern in self.excluded_handlers)` with a direct for-loop that returns `True` immediately upon finding the first match, eliminating the generator overhead and function call overhead of `any()`.

**Why this is faster:**

- **Eliminates generator overhead**: The `any()` function with a generator expression creates additional Python object overhead for each iteration, while the explicit loop operates directly on the iterable.
- **Reduces function call overhead**: `any()` adds an extra function call layer that's eliminated in the optimized version.
- **Short-circuits empty cases**: When `excluded_handlers` is empty (common case), the optimization immediately returns without any iteration setup.

**Performance impact based on test results:**

- **Empty handlers case**: Shows dramatic improvements (121-197% faster) as the short-circuit completely avoids iteration overhead.
- **Pattern matching cases**: Consistent 30-90% improvements across all scenarios, with early matches benefiting most from immediate returns.
- **Large-scale scenarios**: With 500-1000 patterns, still achieves 28-32% speedup despite the overhead of checking many patterns.

The optimization is particularly valuable since `_is_handler_excluded` is called for every HTTP request in the ASGI middleware pipeline, making even small per-call improvements significant for high-traffic applications.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 13, 2025 01:06
@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