Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 10% (0.10x) speedup for check_allowlist in src/bokeh/server/util.py

⏱️ Runtime : 10.3 milliseconds 9.39 milliseconds (best of 163 runs)

📝 Explanation and details

The optimized code achieves a 9% speedup through several key optimizations:

1. Early exit for empty allowlist: Added a fast path that immediately returns False for empty allowlists, avoiding unnecessary string operations. This provides massive gains (318% faster) for empty allowlist cases.

2. Eliminated redundant string creation: The original code unconditionally modified the host parameter by adding :80, creating a new string object. The optimized version only creates host_with_port when needed, reducing memory allocations.

3. Replaced generator expression with explicit loop: Changed from any(match_host(host, pattern) for pattern in allowlist) to a regular for-loop with early return. This eliminates generator overhead and allows for immediate termination when a match is found.

4. Cached function reference: Stored match_host in a local variable match to avoid repeated attribute lookups during the loop iteration.

5. Added fast path for universal wildcards: In match_host, added early detection for patterns like '*' and '*:*' that match everything, avoiding expensive string splitting and comparison operations.

6. Micro-optimizations in matching logic: Reordered conditions in the port matching logic to check the wildcard case (p == '*') before exact match (h == p), as wildcards are common in allowlists.

These optimizations are particularly effective for:

  • Empty allowlists (318% faster)
  • Large allowlists with no matches (5-8% faster)
  • Wildcard patterns (15-25% faster)
  • Cases requiring pattern matching (10-20% faster)

The optimizations maintain exact functional compatibility while reducing both computational overhead and memory allocations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 32 Passed
🌀 Generated Regression Tests 3130 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
unit/bokeh/server/test_util.py::test_check_allowlist_accepts_all_on_star 24.5μs 20.2μs 21.0%✅
unit/bokeh/server/test_util.py::test_check_allowlist_accepts_implicit_port_80 699ns 735ns -4.90%⚠️
unit/bokeh/server/test_util.py::test_check_allowlist_accepts_name_port_match 432ns 509ns -15.1%⚠️
unit/bokeh/server/test_util.py::test_check_allowlist_rejects_name_mismatch 4.16μs 3.79μs 9.85%✅
unit/bokeh/server/test_util.py::test_check_allowlist_rejects_port_mismatch 2.49μs 2.08μs 19.7%✅
🌀 Generated Regression Tests and Runtime
from typing import Sequence

# imports
import pytest
from bokeh.server.util import check_allowlist

# unit tests

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

def test_exact_match_with_port():
    # Host matches exactly with allowlist entry including port
    codeflash_output = check_allowlist('example.com:80', ['example.com:80']) # 430ns -> 543ns (20.8% slower)

def test_exact_match_without_port():
    # Host without port should match allowlist entry with port 80
    codeflash_output = check_allowlist('example.com', ['example.com:80']) # 656ns -> 706ns (7.08% slower)

def test_exact_match_without_port_and_allowlist_without_port():
    # Host and allowlist both without port, implicit :80
    codeflash_output = check_allowlist('example.com', ['example.com']) # 3.41μs -> 2.67μs (28.0% faster)

def test_no_match():
    # Host does not match any entry in allowlist
    codeflash_output = check_allowlist('example.com', ['other.com']) # 3.15μs -> 2.59μs (21.4% faster)

def test_wildcard_match():
    # Allowlist with wildcard should match host
    codeflash_output = check_allowlist('foo.example.com', ['*.example.com']) # 3.59μs -> 2.94μs (22.4% faster)

def test_wildcard_ip_octet():
    # Allowlist with wildcard octet should match host IP
    codeflash_output = check_allowlist('192.168.1.1', ['192.168.*.*']) # 3.55μs -> 3.00μs (18.5% faster)

def test_wildcard_all():
    # '*' in allowlist should match any host
    codeflash_output = check_allowlist('anything:1234', ['*']) # 2.85μs -> 2.39μs (19.2% faster)

def test_wildcard_port():
    # '*:80' in allowlist should match any host on port 80
    codeflash_output = check_allowlist('foo:80', ['*:80']) # 3.13μs -> 2.71μs (15.4% faster)
    codeflash_output = check_allowlist('bar', ['*:80']) # 1.67μs -> 1.49μs (12.3% faster)

def test_no_port_in_allowlist_but_host_has_port():
    # Host with port matches allowlist without port (should match)
    codeflash_output = check_allowlist('foo:80', ['foo']) # 2.59μs -> 2.24μs (15.5% faster)

def test_no_port_in_host_but_allowlist_has_port():
    # Host without port, allowlist with port (should match if port is 80)
    codeflash_output = check_allowlist('foo', ['foo:80']) # 670ns -> 668ns (0.299% faster)

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

def test_empty_allowlist():
    # Empty allowlist should never match
    codeflash_output = check_allowlist('foo', []) # 1.25μs -> 300ns (318% faster)

def test_empty_host():
    # Empty host should not match any non-empty allowlist
    codeflash_output = check_allowlist('', ['foo']) # 3.20μs -> 2.77μs (15.5% faster)
    # But should match if allowlist has empty string or wildcard
    codeflash_output = check_allowlist('', ['']) # 1.78μs -> 1.30μs (36.2% faster)
    codeflash_output = check_allowlist('', ['*']) # 1.14μs -> 883ns (29.6% faster)

def test_host_with_port_implicit_80():
    # Host without port should be treated as :80
    codeflash_output = check_allowlist('foo', ['foo:80']) # 671ns -> 698ns (3.87% slower)
    # Host with explicit :80 should match allowlist without port
    codeflash_output = check_allowlist('foo:80', ['foo']) # 2.60μs -> 2.14μs (21.5% faster)

def test_host_with_different_port():
    # Host with port not matching allowlist port should not match
    codeflash_output = check_allowlist('foo:8080', ['foo:80']) # 2.05μs -> 1.69μs (20.8% faster)
    codeflash_output = check_allowlist('foo:8080', ['foo']) # 1.89μs -> 1.50μs (26.1% faster)

def test_allowlist_with_wildcard_port():
    # Allowlist with wildcard port should match any port
    codeflash_output = check_allowlist('foo:1234', ['foo:*']) # 2.75μs -> 2.42μs (13.7% faster)
    codeflash_output = check_allowlist('bar:9999', ['bar:*']) # 1.33μs -> 1.13μs (18.1% faster)

def test_allowlist_with_partial_wildcard():
    # Allowlist with partial wildcard in subdomain
    codeflash_output = check_allowlist('a.b.c.com', ['*.b.c.com']) # 3.32μs -> 2.90μs (14.6% faster)
    codeflash_output = check_allowlist('b.c.com', ['*.b.c.com']) # 1.62μs -> 1.36μs (19.4% faster)

def test_ipv6_address():
    # IPv6 addresses (should treat as string, not special)
    codeflash_output = check_allowlist('::1', ['::1']) # 426ns -> 466ns (8.58% slower)
    codeflash_output = check_allowlist('::1', ['*']) # 2.53μs -> 2.07μs (22.0% faster)
    codeflash_output = check_allowlist('::1:80', ['::1']) # 1.10μs -> 875ns (25.9% faster)
    codeflash_output = check_allowlist('::1:8080', ['::1:80']) # 775ns -> 635ns (22.0% faster)

def test_host_with_multiple_colons():
    # Host with multiple colons (IPv6 with port)
    codeflash_output = check_allowlist('[::1]:80', ['[::1]:80']) # 423ns -> 491ns (13.8% slower)
    codeflash_output = check_allowlist('[::1]:80', ['[::1]']) # 1.76μs -> 1.45μs (21.3% faster)

def test_pattern_longer_than_host():
    # Pattern has more parts than host, should not match
    codeflash_output = check_allowlist('foo.com', ['bar.foo.com']) # 2.38μs -> 2.22μs (7.26% faster)
    codeflash_output = check_allowlist('foo.com', ['*.foo.com']) # 1.28μs -> 1.07μs (19.3% faster)

def test_case_sensitivity():
    # Hostname matching is case-sensitive
    codeflash_output = check_allowlist('FOO.com', ['foo.com']) # 2.88μs -> 2.68μs (7.15% faster)

def test_pattern_with_star_and_port():
    # Pattern with star and port
    codeflash_output = check_allowlist('foo:80', ['*:80']) # 3.17μs -> 2.65μs (19.6% faster)
    codeflash_output = check_allowlist('foo:8080', ['*:80']) # 1.15μs -> 969ns (18.8% faster)

def test_pattern_with_star_and_star_port():
    # Pattern with both star host and star port
    codeflash_output = check_allowlist('foo:9999', ['*:*']) # 3.01μs -> 2.42μs (24.6% faster)
    codeflash_output = check_allowlist('bar', ['*:*']) # 1.66μs -> 1.38μs (20.1% faster)

def test_port_zero():
    # Port zero is valid string, should be matched if specified
    codeflash_output = check_allowlist('foo:0', ['foo:0']) # 393ns -> 473ns (16.9% slower)
    codeflash_output = check_allowlist('foo:0', ['foo:1']) # 1.75μs -> 1.43μs (22.7% faster)

def test_pattern_with_trailing_dot():
    # Trailing dot in pattern or host should be treated as literal
    codeflash_output = check_allowlist('foo.com.', ['foo.com.']) # 3.44μs -> 2.89μs (19.1% faster)
    codeflash_output = check_allowlist('foo.com', ['foo.com.']) # 1.61μs -> 1.34μs (20.1% faster)

def test_pattern_with_internal_wildcards():
    # Wildcards in the middle of the domain
    codeflash_output = check_allowlist('a.b.c', ['a.*.c']) # 3.14μs -> 2.76μs (13.8% faster)
    codeflash_output = check_allowlist('a.b.c', ['*.b.*']) # 1.58μs -> 1.30μs (21.4% faster)

def test_pattern_with_multiple_wildcards():
    # Multiple wildcards in pattern
    codeflash_output = check_allowlist('1.2.3.4', ['*.*.*.*']) # 3.05μs -> 2.68μs (14.0% faster)
    codeflash_output = check_allowlist('1.2.3.4', ['1.*.*.4']) # 1.53μs -> 1.30μs (17.5% faster)

def test_pattern_with_extra_port_colon():
    # Host with extra colon (malformed), should not crash
    codeflash_output = check_allowlist('foo:80:90', ['foo:80:90']) # 405ns -> 469ns (13.6% slower)
    codeflash_output = check_allowlist('foo:80:90', ['foo:80']) # 1.77μs -> 1.46μs (21.4% faster)

def test_pattern_with_empty_string():
    # Allowlist with empty string matches only empty host
    codeflash_output = check_allowlist('', ['']) # 2.85μs -> 2.41μs (18.4% faster)
    codeflash_output = check_allowlist('foo', ['']) # 1.86μs -> 1.54μs (20.9% faster)

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

def test_large_allowlist_no_match():
    # Large allowlist with no match
    allowlist = [f'host{i}.com' for i in range(1000)]
    codeflash_output = check_allowlist('notfound.com', allowlist) # 397μs -> 371μs (7.00% faster)

def test_large_allowlist_with_match():
    # Large allowlist with a match in the middle
    allowlist = [f'host{i}.com' for i in range(500)]
    allowlist.append('target.com')
    allowlist.extend([f'host{i}.com' for i in range(501,1000)])
    codeflash_output = check_allowlist('target.com', allowlist) # 201μs -> 190μs (6.06% faster)

def test_large_allowlist_with_wildcard_match():
    # Large allowlist with a wildcard matching the host
    allowlist = [f'host{i}.com' for i in range(999)] + ['*']
    codeflash_output = check_allowlist('anything.com', allowlist) # 396μs -> 372μs (6.67% faster)

def test_large_number_of_hosts():
    # Test many hosts against a small wildcard allowlist
    allowlist = ['*.example.com']
    for i in range(1000):
        codeflash_output = check_allowlist(f'foo{i}.example.com', allowlist) # 879μs -> 697μs (26.2% faster)

def test_large_number_of_patterns_with_partial_wildcard():
    # Allowlist with many patterns and one partial wildcard
    allowlist = [f'prefix{i}.domain.com' for i in range(999)] + ['*.domain.com']
    codeflash_output = check_allowlist('special.domain.com', allowlist) # 412μs -> 389μs (5.96% faster)

def test_large_ip_range_wildcard():
    # Allowlist with wildcard for a large IP range
    allowlist = ['10.0.*.*']
    for i in range(100):
        for j in range(10):
            codeflash_output = check_allowlist(f'10.0.{i}.{j}', allowlist)

def test_large_scale_pattern_with_ports():
    # Allowlist with many port-specific patterns
    allowlist = [f'foo.com:{i}' for i in range(1000)]
    codeflash_output = check_allowlist('foo.com:999', allowlist) # 5.73μs -> 5.75μs (0.382% slower)
    codeflash_output = check_allowlist('foo.com:1001', allowlist) # 203μs -> 188μs (7.82% faster)

def test_large_scale_pattern_with_star_port():
    # Allowlist with star port for many hosts
    allowlist = [f'host{i}.com:*' for i in range(1000)]
    for i in range(0, 1000, 100):
        codeflash_output = check_allowlist(f'host{i}.com:1234', allowlist) # 2.14ms -> 2.04ms (4.75% faster)

def test_large_scale_pattern_with_star_and_specific_port():
    # Allowlist with star and specific port for many hosts
    allowlist = [f'host{i}.com:80' for i in range(1000)] + ['*:443']
    codeflash_output = check_allowlist('host500.com:80', allowlist) # 3.07μs -> 3.10μs (0.968% slower)
    codeflash_output = check_allowlist('anyhost.com:443', allowlist) # 207μs -> 189μs (9.00% faster)
    codeflash_output = check_allowlist('anyhost.com:444', allowlist) # 203μs -> 188μs (7.99% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from __future__ import annotations

from typing import Sequence

# imports
import pytest  # used for our unit tests
from bokeh.server.util import check_allowlist

# unit tests

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

def test_exact_match_with_port():
    # Host matches exactly with allowlist entry (with port)
    codeflash_output = check_allowlist('example.com:80', ['example.com:80']) # 465ns -> 524ns (11.3% slower)

def test_exact_match_without_port():
    # Host matches exactly with allowlist entry (without port, implicit :80)
    codeflash_output = check_allowlist('example.com', ['example.com:80']) # 682ns -> 696ns (2.01% slower)

def test_no_match():
    # Host does not match any allowlist entry
    codeflash_output = check_allowlist('foo.com', ['bar.com:80']) # 3.67μs -> 3.12μs (17.7% faster)

def test_allowlist_multiple_entries():
    # Host matches one of several allowlist entries
    allowlist = ['foo.com:80', 'bar.com:80', 'baz.com:80']
    codeflash_output = check_allowlist('bar.com', allowlist) # 698ns -> 733ns (4.77% slower)
    codeflash_output = check_allowlist('baz.com', allowlist) # 350ns -> 315ns (11.1% faster)
    codeflash_output = check_allowlist('notfound.com', allowlist) # 4.31μs -> 3.86μs (11.6% faster)

def test_wildcard_match():
    # Wildcard in allowlist matches any host
    codeflash_output = check_allowlist('anything.com', ['*']) # 3.30μs -> 2.68μs (22.9% faster)
    codeflash_output = check_allowlist('anything.com:8080', ['*']) # 1.54μs -> 1.26μs (22.6% faster)

def test_wildcard_with_port():
    # Wildcard with port matches any host with that port
    codeflash_output = check_allowlist('foo.com:80', ['*:80']) # 3.03μs -> 2.69μs (12.7% faster)
    codeflash_output = check_allowlist('foo.com:8080', ['*:80']) # 1.09μs -> 925ns (17.4% faster)

def test_ip_exact_and_wildcard():
    # IP address exact and wildcard matches
    codeflash_output = check_allowlist('192.168.1.1', ['192.168.1.1:80']) # 598ns -> 705ns (15.2% slower)
    codeflash_output = check_allowlist('192.168.1.2', ['192.168.1.*']) # 3.10μs -> 2.68μs (15.6% faster)
    codeflash_output = check_allowlist('192.168.2.2', ['192.168.1.*']) # 1.54μs -> 1.37μs (12.0% faster)

def test_hostname_wildcard():
    # Hostname wildcard matches
    codeflash_output = check_allowlist('alice', ['*']) # 2.84μs -> 2.48μs (14.5% faster)
    codeflash_output = check_allowlist('bob', ['*']) # 1.40μs -> 1.26μs (11.3% faster)

def test_hostname_mismatch():
    # Hostname does not match
    codeflash_output = check_allowlist('alice', ['bob']) # 2.69μs -> 2.39μs (12.3% faster)

def test_port_wildcard_in_pattern():
    # Pattern port is wildcard, should match any port
    codeflash_output = check_allowlist('alice:80', ['alice:*']) # 2.92μs -> 2.47μs (18.4% faster)
    codeflash_output = check_allowlist('alice:8080', ['alice:*']) # 1.40μs -> 1.13μs (23.7% faster)

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

def test_empty_allowlist():
    # Empty allowlist should never match
    codeflash_output = check_allowlist('foo.com', []) # 1.13μs -> 304ns (273% faster)

def test_empty_host():
    # Empty host string with non-empty allowlist
    codeflash_output = check_allowlist('', ['*']) # 3.10μs -> 2.55μs (21.7% faster)
    codeflash_output = check_allowlist('', ['']) # 1.38μs -> 1.20μs (14.5% faster)

def test_host_without_port_and_pattern_without_port():
    # Both host and pattern without port (implicit :80)
    codeflash_output = check_allowlist('foo.com', ['foo.com']) # 3.00μs -> 2.68μs (11.9% faster)

def test_host_with_port_and_pattern_without_port():
    # Host with port, pattern without port (should match if port is 80)
    codeflash_output = check_allowlist('foo.com:80', ['foo.com']) # 2.99μs -> 2.44μs (22.3% faster)
    codeflash_output = check_allowlist('foo.com:8080', ['foo.com']) # 1.39μs -> 1.18μs (18.3% faster)

def test_pattern_longer_than_host():
    # Pattern with more octets than host should not match
    codeflash_output = check_allowlist('foo.com', ['bar.foo.com']) # 2.52μs -> 2.11μs (19.3% faster)
    codeflash_output = check_allowlist('192.168.1.1', ['192.168.1.1.1']) # 1.51μs -> 1.33μs (13.4% faster)

def test_pattern_shorter_than_host():
    # Pattern with fewer octets than host, but matches prefix
    codeflash_output = check_allowlist('foo.bar.com', ['foo']) # 3.07μs -> 2.64μs (16.0% faster)
    codeflash_output = check_allowlist('192.168.1.1', ['192']) # 1.50μs -> 1.21μs (24.6% faster)

def test_pattern_with_multiple_wildcards():
    # Pattern with wildcards in multiple positions
    codeflash_output = check_allowlist('192.168.1.1', ['192.*.*.*']) # 3.25μs -> 2.88μs (13.0% faster)
    codeflash_output = check_allowlist('10.0.0.5', ['10.*.*.*']) # 1.89μs -> 1.64μs (14.9% faster)
    codeflash_output = check_allowlist('10.0.0.5', ['192.*.*.*']) # 1.37μs -> 1.17μs (16.8% faster)

def test_pattern_with_wildcard_port():
    # Pattern with wildcard port should match any port
    codeflash_output = check_allowlist('foo.com:1234', ['foo.com:*']) # 3.06μs -> 2.58μs (18.9% faster)
    codeflash_output = check_allowlist('foo.com:5678', ['foo.com:*']) # 1.42μs -> 1.23μs (15.4% faster)

def test_pattern_with_port_and_host_without_port():
    # Pattern with port, host without port (should match if port is 80)
    codeflash_output = check_allowlist('foo.com', ['foo.com:80']) # 654ns -> 689ns (5.08% slower)
    codeflash_output = check_allowlist('foo.com', ['foo.com:8080']) # 1.81μs -> 1.40μs (29.2% faster)

def test_pattern_with_wildcard_and_port():
    # Pattern '*' with port should match any host with that port
    codeflash_output = check_allowlist('bar.com:80', ['*:80']) # 3.21μs -> 2.60μs (23.5% faster)
    codeflash_output = check_allowlist('bar.com:8080', ['*:80']) # 1.16μs -> 904ns (28.1% faster)

def test_pattern_with_only_port():
    # Pattern is just a port (should not match)
    codeflash_output = check_allowlist('foo.com:80', [':80']) # 3.06μs -> 2.63μs (16.1% faster)

def test_pattern_with_trailing_dot():
    # Host or pattern with trailing dot
    codeflash_output = check_allowlist('foo.com.', ['foo.com']) # 3.15μs -> 2.76μs (14.2% faster)
    codeflash_output = check_allowlist('foo.com', ['foo.com.']) # 1.61μs -> 1.30μs (24.2% faster)

def test_ipv6_like_strings():
    # IPv6-like hostnames (should be treated as normal strings)
    codeflash_output = check_allowlist('::1', ['*']) # 2.81μs -> 2.32μs (21.5% faster)
    codeflash_output = check_allowlist('::1', ['::1']) # 302ns -> 299ns (1.00% faster)
    codeflash_output = check_allowlist('::1', ['::2']) # 1.15μs -> 942ns (22.0% faster)

def test_case_sensitivity():
    # Hostname matching is case sensitive
    codeflash_output = check_allowlist('FOO.com', ['foo.com']) # 2.90μs -> 2.61μs (11.3% faster)
    codeflash_output = check_allowlist('foo.com', ['FOO.com']) # 1.33μs -> 1.17μs (13.7% faster)

def test_host_with_multiple_colons():
    # Host with multiple colons (e.g., badly formed IPv6)
    codeflash_output = check_allowlist('foo:bar:baz', ['foo:bar:baz']) # 409ns -> 470ns (13.0% slower)
    codeflash_output = check_allowlist('foo:bar:baz', ['foo:bar']) # 1.78μs -> 1.52μs (16.8% faster)

def test_pattern_with_multiple_colons():
    # Pattern with multiple colons
    codeflash_output = check_allowlist('foo:bar:baz', ['foo:bar:baz']) # 382ns -> 454ns (15.9% slower)
    codeflash_output = check_allowlist('foo:bar:baz', ['foo:*:baz']) # 2.80μs -> 2.37μs (18.1% faster)

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

def test_large_allowlist_exact_match():
    # Large allowlist, host matches one entry
    allowlist = [f'host{i}.com:80' for i in range(1000)]
    codeflash_output = check_allowlist('host500.com', allowlist) # 3.23μs -> 3.26μs (0.950% slower)
    codeflash_output = check_allowlist('host999.com', allowlist) # 5.48μs -> 5.49μs (0.055% slower)
    codeflash_output = check_allowlist('notfound.com', allowlist) # 480μs -> 456μs (5.25% faster)

def test_large_allowlist_wildcard_match():
    # Large allowlist with one wildcard entry at the end
    allowlist = [f'host{i}.com:80' for i in range(999)] + ['*']
    codeflash_output = check_allowlist('anything.com', allowlist) # 480μs -> 454μs (5.84% faster)
    codeflash_output = check_allowlist('host500.com', allowlist) # 3.00μs -> 3.01μs (0.299% slower)

def test_large_allowlist_no_match():
    # Large allowlist, host does not match any entry
    allowlist = [f'host{i}.com:80' for i in range(1000)]
    codeflash_output = check_allowlist('nope.com', allowlist) # 478μs -> 451μs (5.90% faster)

def test_large_number_of_patterns_with_wildcards():
    # Allowlist with many wildcard patterns
    allowlist = [f'192.168.{i}.*' for i in range(1000)]
    codeflash_output = check_allowlist('192.168.500.42', allowlist) # 239μs -> 227μs (5.15% faster)
    codeflash_output = check_allowlist('192.169.1.1', allowlist) # 433μs -> 410μs (5.46% faster)

def test_large_number_of_hosts():
    # Test many hosts against a small allowlist
    allowlist = ['foo.com:80', 'bar.com:80', '*:8080']
    for i in range(1000):
        host = f'foo.com:{80 if i % 2 == 0 else 8080}'
        codeflash_output = check_allowlist(host, allowlist) # 774μs -> 663μs (16.8% faster)
    # Also test a host that should not match
    codeflash_output = check_allowlist('notfound.com:1234', allowlist) # 1.61μs -> 1.33μs (21.3% faster)

def test_large_allowlist_with_varied_patterns():
    # Large allowlist with mixed patterns, including wildcards and exacts
    allowlist = [f'host{i}.com:80' for i in range(500)]
    allowlist += [f'192.168.{i}.*' for i in range(500)]
    allowlist += ['*.wild.com', '*:1234']
    codeflash_output = check_allowlist('host123.com', allowlist) # 1.29μs -> 1.34μs (3.59% slower)
    codeflash_output = check_allowlist('192.168.250.1', allowlist) # 366μs -> 348μs (5.24% faster)
    codeflash_output = check_allowlist('foo.wild.com', allowlist) # 381μs -> 360μs (5.79% faster)
    codeflash_output = check_allowlist('bar.com:1234', allowlist) # 235μs -> 219μs (7.45% faster)
    codeflash_output = check_allowlist('notfound.com:5678', allowlist) # 237μs -> 217μs (8.82% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from bokeh.server.util import check_allowlist

def test_check_allowlist():
    check_allowlist('', ())

def test_check_allowlist_2():
    check_allowlist(':', ':')
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_5f34sbte/tmpbv_bfu_7/test_concolic_coverage.py::test_check_allowlist 1.14μs 370ns 208%✅
codeflash_concolic_5f34sbte/tmpbv_bfu_7/test_concolic_coverage.py::test_check_allowlist_2 421ns 464ns -9.27%⚠️

To edit these changes git checkout codeflash/optimize-check_allowlist-mhbl4xef and push.

Codeflash

The optimized code achieves a **9% speedup** through several key optimizations:

**1. Early exit for empty allowlist**: Added a fast path that immediately returns `False` for empty allowlists, avoiding unnecessary string operations. This provides massive gains (318% faster) for empty allowlist cases.

**2. Eliminated redundant string creation**: The original code unconditionally modified the `host` parameter by adding `:80`, creating a new string object. The optimized version only creates `host_with_port` when needed, reducing memory allocations.

**3. Replaced generator expression with explicit loop**: Changed from `any(match_host(host, pattern) for pattern in allowlist)` to a regular for-loop with early return. This eliminates generator overhead and allows for immediate termination when a match is found.

**4. Cached function reference**: Stored `match_host` in a local variable `match` to avoid repeated attribute lookups during the loop iteration.

**5. Added fast path for universal wildcards**: In `match_host`, added early detection for patterns like `'*'` and `'*:*'` that match everything, avoiding expensive string splitting and comparison operations.

**6. Micro-optimizations in matching logic**: Reordered conditions in the port matching logic to check the wildcard case (`p == '*'`) before exact match (`h == p`), as wildcards are common in allowlists.

These optimizations are particularly effective for:
- **Empty allowlists** (318% faster)
- **Large allowlists with no matches** (5-8% faster) 
- **Wildcard patterns** (15-25% faster)
- **Cases requiring pattern matching** (10-20% faster)

The optimizations maintain exact functional compatibility while reducing both computational overhead and memory allocations.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 29, 2025 05:59
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Oct 29, 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