Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 33% (0.33x) speedup for BrotliMiddleware._is_compressible_file_type in gradio/brotli_middleware.py

⏱️ Runtime : 2.10 milliseconds 1.58 milliseconds (best of 19 runs)

📝 Explanation and details

The optimization achieves a 33% speedup through two key changes that eliminate redundant operations in the hot path:

1. Move set creation to initialization: The original code recreated the compressible_extensions set on every method call (line profiler shows 21.5% of time spent here). The optimized version moves this to __init__, converting it from a per-call allocation to a one-time instance variable. This eliminates thousands of set creations when the method is called frequently.

2. Replace split() with rpartition(): The original code used path.split(".")[-1] which creates an entire list of all path segments, then discards everything except the last element. The optimized version uses path.rpartition(".")[-1] which directly finds the last dot and returns only the final segment without creating intermediate data structures.

Performance impact by test type:

  • Files with extensions: 24-45% faster (most common case in web servers)
  • Files without extensions: 57-112% faster (avoids set creation entirely)
  • Bulk operations: 28-57% faster (critical for high-throughput scenarios)

Why this matters: The _is_compressible_file_type method is called in the middleware's __call__ method for every HTTP request to determine if response compression should be applied. In web applications serving many static assets (HTML, CSS, JS, JSON), this method executes thousands of times per second. The eliminated allocations reduce both CPU cycles and garbage collection pressure, making the middleware more efficient under load.

The optimization maintains identical behavior while removing computational waste from the request processing pipeline.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4831 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
from starlette.datastructures import Headers
from starlette.middleware.gzip import GZipResponder
from starlette.types import ASGIApp, Receive, Scope, Send

# unit tests

# Helper for creating a dummy scope
def make_scope(path):
    return {"path": path}

# 1. Basic Test Cases

def test_html_extension():
    """Should return True for .html files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/index.html")) # 2.98μs -> 2.40μs (24.0% faster)

def test_htm_extension():
    """Should return True for .htm files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/page.HTM")) # 2.41μs -> 1.78μs (35.7% faster)

def test_js_extension():
    """Should return True for .js files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/static/script.js")) # 2.40μs -> 1.90μs (26.3% faster)

def test_css_extension():
    """Should return True for .css files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/assets/styles.CSS")) # 2.29μs -> 1.75μs (31.1% faster)

def test_json_extension():
    """Should return True for .json files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/data/config.json")) # 2.35μs -> 1.81μs (30.0% faster)

def test_md_extension():
    """Should return True for .md files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/README.md")) # 2.32μs -> 1.67μs (39.0% faster)

def test_txt_extension():
    """Should return True for .txt files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/notes.txt")) # 2.33μs -> 1.71μs (36.1% faster)

def test_csv_extension():
    """Should return True for .csv files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/table.csv")) # 2.38μs -> 1.64μs (45.0% faster)

def test_tsv_extension():
    """Should return True for .tsv files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/spreadsheet.tsv")) # 2.37μs -> 1.87μs (26.9% faster)

def test_xml_extension():
    """Should return True for .xml files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/feed.xml")) # 2.25μs -> 1.73μs (29.8% faster)

def test_svg_extension():
    """Should return True for .svg files."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/vector.svg")) # 2.35μs -> 1.64μs (43.1% faster)

def test_non_compressible_extension_png():
    """Should return False for .png files (not in whitelist)."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/image.png")) # 2.31μs -> 1.55μs (49.2% faster)

def test_non_compressible_extension_zip():
    """Should return False for .zip files (not in whitelist)."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/archive.zip")) # 2.19μs -> 1.64μs (33.8% faster)

def test_non_compressible_extension_woff2():
    """Should return False for .woff2 files (already compressed)."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/font.woff2")) # 2.27μs -> 1.55μs (46.8% faster)

def test_no_extension():
    """Should return False for files with no extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/noextension")) # 1.40μs -> 660ns (112% faster)

def test_empty_path():
    """Should return False for empty path."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("")) # 1.24μs -> 616ns (102% faster)

def test_path_is_none():
    """Should return False if path is missing from scope."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type({}) # 1.41μs -> 717ns (96.7% faster)

# 2. Edge Test Cases

def test_multiple_dots_in_filename():
    """Should use the last extension after the last dot."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/archive.tar.gz")) # 2.36μs -> 1.65μs (43.3% faster)
    codeflash_output = mw._is_compressible_file_type(make_scope("/data.backup.JSON")) # 1.19μs -> 1.02μs (16.3% faster)

def test_dot_at_start_of_filename():
    """Should handle filenames starting with a dot."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/.hidden.txt")) # 2.36μs -> 1.64μs (44.0% faster)
    codeflash_output = mw._is_compressible_file_type(make_scope("/.env")) # 904ns -> 680ns (32.9% faster)

def test_uppercase_extension():
    """Should be case-insensitive for extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/UPPERCASE.CSS")) # 2.17μs -> 1.66μs (30.2% faster)
    codeflash_output = mw._is_compressible_file_type(make_scope("/UPPERCASE.PNG")) # 753ns -> 573ns (31.4% faster)

def test_trailing_dot_in_path():
    """Should return False for trailing dot (no extension)."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file.")) # 1.96μs -> 1.55μs (26.0% faster)

def test_path_with_query_string():
    """Should ignore query string and compress based on extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file.txt?version=1")) # 2.28μs -> 1.73μs (32.1% faster)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file.png?version=1")) # 739ns -> 570ns (29.6% faster)

def test_path_with_fragment():
    """Should ignore fragment and compress based on extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file.css#section")) # 1.99μs -> 1.71μs (15.9% faster)

def test_path_with_multiple_slashes():
    """Should handle paths with multiple slashes."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/static//file.js")) # 2.30μs -> 1.76μs (30.5% faster)

def test_path_with_spaces():
    """Should handle spaces in file names."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/my file.txt")) # 2.46μs -> 1.73μs (42.3% faster)

def test_path_with_special_characters():
    """Should handle special characters in file names."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file@2x.json")) # 2.33μs -> 1.71μs (36.4% faster)

def test_path_with_extension_in_middle():
    """Should not match extension in the middle of the filename."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/foo.html.bar")) # 2.21μs -> 1.57μs (40.3% faster)

def test_extension_with_mixed_case():
    """Should handle mixed case extensions."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file.JsOn")) # 2.29μs -> 1.74μs (31.9% faster)

def test_path_with_dot_but_no_extension():
    """Should return False if dot is present but not followed by an extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/foo.")) # 2.27μs -> 1.58μs (44.2% faster)

def test_path_with_dot_in_directory():
    """Should only consider the last dot for extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/dir.with.dot/file.txt")) # 2.44μs -> 1.83μs (33.2% faster)

def test_path_with_multiple_extensions():
    """Should only consider the last extension."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope("/file.csv.zip")) # 2.12μs -> 1.63μs (29.9% faster)

def test_path_with_leading_and_trailing_spaces():
    """Should handle leading and trailing spaces in path."""
    mw = BrotliMiddleware(app=None)
    codeflash_output = mw._is_compressible_file_type(make_scope(" /file.csv ")) # 2.27μs -> 1.62μs (40.5% faster)

# 3. Large Scale Test Cases

def test_bulk_compressible_extensions():
    """Should correctly identify compressible extensions in a large batch."""
    mw = BrotliMiddleware(app=None)
    compressible_exts = [
        ".html", ".htm", ".js", ".css", ".json", ".md", ".txt", ".csv", ".tsv", ".xml", ".svg"
    ]
    for ext in compressible_exts:
        for i in range(100):
            path = f"/file_{i}{ext}"
            codeflash_output = mw._is_compressible_file_type(make_scope(path))

def test_bulk_non_compressible_extensions():
    """Should correctly identify non-compressible extensions in a large batch."""
    mw = BrotliMiddleware(app=None)
    non_compressible_exts = [
        ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".exe", ".bin", ".zip", ".tar", ".gz", ".pdf", ".woff2"
    ]
    for ext in non_compressible_exts:
        for i in range(100):
            path = f"/file_{i}{ext}"
            codeflash_output = mw._is_compressible_file_type(make_scope(path))

def test_bulk_mixed_case_extensions():
    """Should correctly identify compressible extensions with mixed case in a large batch."""
    mw = BrotliMiddleware(app=None)
    compressible_exts = [
        ".Html", ".HTM", ".Js", ".Css", ".Json", ".Md", ".TXT", ".Csv", ".TSV", ".Xml", ".SVG"
    ]
    for ext in compressible_exts:
        for i in range(100):
            path = f"/file_{i}{ext}"
            codeflash_output = mw._is_compressible_file_type(make_scope(path))

def test_large_number_of_non_extension_paths():
    """Should return False for a large number of paths without extensions."""
    mw = BrotliMiddleware(app=None)
    for i in range(500):
        path = f"/folder/file_{i}"
        codeflash_output = mw._is_compressible_file_type(make_scope(path)) # 145μs -> 92.3μs (57.6% faster)

def test_large_number_of_paths_with_dots_but_no_extension():
    """Should return False for a large number of paths with dots but no valid extension."""
    mw = BrotliMiddleware(app=None)
    for i in range(500):
        path = f"/folder/file_{i}."
        codeflash_output = mw._is_compressible_file_type(make_scope(path)) # 199μs -> 148μs (34.2% faster)

def test_large_number_of_paths_with_query_and_fragment():
    """Should correctly identify compressible extensions even with query and fragment."""
    mw = BrotliMiddleware(app=None)
    for i in range(100):
        path = f"/file_{i}.json?foo=bar#{i}"
        codeflash_output = mw._is_compressible_file_type(make_scope(path)) # 50.1μs -> 38.9μs (28.7% faster)

def test_large_number_of_paths_with_multiple_dots():
    """Should only consider the last extension in large batch."""
    mw = BrotliMiddleware(app=None)
    for i in range(100):
        path = f"/archive_{i}.tar.gz"
        codeflash_output = mw._is_compressible_file_type(make_scope(path)) # 49.7μs -> 36.7μs (35.5% faster)
        path2 = f"/data_{i}.backup.JSON"
        codeflash_output = mw._is_compressible_file_type(make_scope(path2))
# 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  # used for our unit tests
from gradio.brotli_middleware import BrotliMiddleware

# unit tests

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

@pytest.mark.parametrize(
    "path,expected",
    [
        # HTML files
        ("/index.html", True),
        ("/about.htm", True),
        # JS, CSS
        ("/static/app.js", True),
        ("/static/styles/main.css", True),
        # JSON
        ("/api/data.json", True),
        # Markdown, TXT
        ("/docs/readme.md", True),
        ("/notes/todo.txt", True),
        # CSV, TSV
        ("/data/table.csv", True),
        ("/data/table.tsv", True),
        # XML, SVG
        ("/feed/rss.xml", True),
        ("/images/vector.svg", True),
        # WOFF2 (should NOT compress)
        ("/fonts/font.woff2", False),
        # PNG, JPG, GIF (should NOT compress)
        ("/images/photo.png", False),
        ("/images/photo.jpg", False),
        ("/images/photo.jpeg", False),
        ("/images/photo.gif", False),
        # No extension
        ("/api/endpoint", False),
        # Hidden file with compressible extension
        ("/.hidden.md", True),
        # Uppercase extension
        ("/file.HTML", True),
        ("/file.JS", True),
        ("/file.CSV", True),
        # Mixed case extension
        ("/file.JsOn", True),
        # Extension in query string (should ignore)
        ("/file.txt?download=1", True),
        ("/file.png?download=1", False),
        # Double extension (should use last)
        ("/archive.tar.gz", False),
        ("/report.final.csv", True),
        # Path with multiple dots
        ("/v1.2.3/app.js", True),
        ("/v1.2.3/app.min.js", True),
        # Extension at end of folder name (should not match)
        ("/folder.csv/file", False),
    ],
)
def test_basic_compressible_file_types(path, expected):
    """Test basic compressible and non-compressible file types."""
    bm = BrotliMiddleware()
    scope = {"path": path}
    codeflash_output = bm._is_compressible_file_type(scope)

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

@pytest.mark.parametrize(
    "path,expected",
    [
        # Empty path
        ("", False),
        # Path is None
        (None, False),
        # Path is just a dot
        (".", False),
        # Path ends with a dot (no extension)
        ("/file.", False),
        # Path with only extension
        (".html", True),
        (".png", False),
        # Path with extension in middle but not at end
        ("/file.html/other", False),
        # Path with extension and trailing slash
        ("/docs/readme.md/", False),
        # Path with extension and trailing dot
        ("/docs/readme.md.", False),
        # Path with extension and extra dot at end
        ("/docs/readme.md..", False),
        # Path with multiple dots but no valid extension
        ("/weird..file..", False),
        # Path with compressible extension but uppercase
        ("/UPPERCASE.TXT", True),
        # Path with compressible extension and spaces
        ("/docs/space file .txt", True),
        # Path with dot but no extension
        ("/file.", False),
        # Path with extension and query string with dot
        ("/file.txt?foo=bar.js", True),
        # Path with extension and fragment
        ("/file.css#section", True),
        # Path with extension and query+fragment
        ("/file.json?foo=bar#frag", True),
        # Path with extension and extra slash
        ("/file.html/", False),
        # Path with extension and trailing whitespace
        ("/file.js ", True),
        # Path with extension and leading whitespace
        (" /file.js", True),
        # Path with extension and unicode
        ("/файл.txt", True),
        # Path with compressible extension and emoji
        ("/smile😀.md", True),
        # Path with compressible extension and special chars
        ("/weird@file!.csv", True),
        # Path with extension in folder name
        ("/csv.folder/file", False),
        # Path with multiple compressible extensions (should use last)
        ("/file.txt.csv", True),
        ("/file.csv.txt", True),
        # Path with extension in query string only (should not match)
        ("/file?type=txt", False),
    ],
)
def test_edge_compressible_file_types(path, expected):
    """Test edge cases for compressible file type detection."""
    bm = BrotliMiddleware()
    scope = {"path": path} if path is not None else {}
    codeflash_output = bm._is_compressible_file_type(scope)

def test_scope_missing_path_key():
    """Test when 'path' key is missing from scope dict."""
    bm = BrotliMiddleware()
    scope = {}
    codeflash_output = bm._is_compressible_file_type(scope)

def test_path_is_not_string():
    """Test when path is not a string (e.g., int, list, dict)."""
    bm = BrotliMiddleware()
    for bad_path in [123, 3.14, [], {}, True, False]:
        scope = {"path": bad_path}
        # Should not raise, just return False
        codeflash_output = bm._is_compressible_file_type(scope)

def test_path_is_bytes():
    """Test when path is bytes (should not crash, should return False)."""
    bm = BrotliMiddleware()
    scope = {"path": b"/file.txt"}
    codeflash_output = bm._is_compressible_file_type(scope)

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


To edit these changes git checkout codeflash/optimize-BrotliMiddleware._is_compressible_file_type-mhwqj1ma and push.

Codeflash Static Badge

The optimization achieves a **33% speedup** through two key changes that eliminate redundant operations in the hot path:

**1. Move set creation to initialization**: The original code recreated the `compressible_extensions` set on every method call (line profiler shows 21.5% of time spent here). The optimized version moves this to `__init__`, converting it from a per-call allocation to a one-time instance variable. This eliminates thousands of set creations when the method is called frequently.

**2. Replace `split()` with `rpartition()`**: The original code used `path.split(".")[-1]` which creates an entire list of all path segments, then discards everything except the last element. The optimized version uses `path.rpartition(".")[-1]` which directly finds the last dot and returns only the final segment without creating intermediate data structures.

**Performance impact by test type**:
- **Files with extensions**: 24-45% faster (most common case in web servers)
- **Files without extensions**: 57-112% faster (avoids set creation entirely)
- **Bulk operations**: 28-57% faster (critical for high-throughput scenarios)

**Why this matters**: The `_is_compressible_file_type` method is called in the middleware's `__call__` method for every HTTP request to determine if response compression should be applied. In web applications serving many static assets (HTML, CSS, JS, JSON), this method executes thousands of times per second. The eliminated allocations reduce both CPU cycles and garbage collection pressure, making the middleware more efficient under load.

The optimization maintains identical behavior while removing computational waste from the request processing pipeline.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 13, 2025 01:13
@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