Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 28% (0.28x) speedup for _get_url in sentry_sdk/integrations/_asgi_common.py

⏱️ Runtime : 6.71 milliseconds 5.23 milliseconds (best of 43 runs)

📝 Explanation and details

The optimization achieves a 28% speedup through two key performance improvements:

1. Dictionary lookup optimization for default ports:

  • Original: Creates a new dictionary {"http": 80, "https": 443, "ws": 80, "wss": 443} on every call and performs .get(scheme)
  • Optimized: Uses conditional expressions 80 if scheme in ("http", "ws") else 443 if scheme in ("https", "wss") else None
  • Impact: Eliminates dictionary object creation overhead (9.5% of original runtime per line profiler) and replaces with faster tuple membership tests

2. String formatting optimization:

  • Original: Uses % formatting: "%s://%s%s" % (scheme, host, path)
  • Optimized: Uses f-strings: f"{scheme}://{host}{path}"
  • Impact: F-strings are faster in Python 3.6+ as they compile to more efficient bytecode operations

3. Minor cleanup:

  • Removed redundant None argument from asgi_scope.get("server", None) since None is the default

Performance characteristics from test results:

  • Best gains on cases requiring port logic (25-47% faster) where dictionary creation was most expensive
  • Consistent improvements across all URL construction paths (17-35% typical range)
  • Minimal overhead on simple path-only returns (some cases 5-13% slower due to f-string preparation, but these are rare edge cases)
  • Scales well with high-volume scenarios (large scale tests show 25-47% improvements)

The optimization is most effective for typical ASGI applications that frequently construct URLs with server information and non-standard ports.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 10072 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest  # used for our unit tests
from sentry_sdk.integrations._asgi_common import _get_url

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

def test_basic_http_with_host():
    # Basic HTTP with host provided
    scope = {"scheme": "http", "server": ("example.com", 80), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", "myhost.com") # 1.54μs -> 1.20μs (28.5% faster)

def test_basic_https_with_host():
    # Basic HTTPS with host provided
    scope = {"scheme": "https", "server": ("example.com", 443), "path": "/bar"}
    codeflash_output = _get_url(scope, "http", "securehost.com") # 1.28μs -> 1.09μs (17.9% faster)

def test_basic_ws_with_host():
    # Basic WebSocket with host provided
    scope = {"scheme": "ws", "server": ("ws.example.com", 80), "path": "/ws"}
    codeflash_output = _get_url(scope, "ws", "websockhost.com") # 1.27μs -> 1.13μs (12.8% faster)

def test_basic_http_without_host_default_port():
    # Basic HTTP, no host, default port
    scope = {"scheme": "http", "server": ("example.com", 80), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 2.02μs -> 1.50μs (34.5% faster)

def test_basic_https_without_host_default_port():
    # Basic HTTPS, no host, default port
    scope = {"scheme": "https", "server": ("secure.com", 443), "path": "/secure"}
    codeflash_output = _get_url(scope, "https", None) # 2.00μs -> 1.54μs (29.9% faster)

def test_basic_ws_without_host_default_port():
    # Basic WS, no host, default port
    scope = {"scheme": "ws", "server": ("ws.com", 80), "path": "/ws"}
    codeflash_output = _get_url(scope, "ws", None) # 1.82μs -> 1.46μs (25.0% faster)

def test_basic_http_without_host_nondefault_port():
    # Basic HTTP, no host, non-default port
    scope = {"scheme": "http", "server": ("example.com", 8080), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 2.15μs -> 1.68μs (27.5% faster)

def test_basic_https_without_host_nondefault_port():
    # Basic HTTPS, no host, non-default port
    scope = {"scheme": "https", "server": ("secure.com", 8443), "path": "/secure"}
    codeflash_output = _get_url(scope, "https", None) # 2.11μs -> 1.76μs (20.2% faster)

def test_basic_ws_without_host_nondefault_port():
    # Basic WS, no host, non-default port
    scope = {"scheme": "ws", "server": ("ws.com", 81), "path": "/ws"}
    codeflash_output = _get_url(scope, "ws", None) # 2.13μs -> 1.62μs (31.4% faster)

def test_basic_wss_without_host_default_port():
    # Basic WSS, no host, default port
    scope = {"scheme": "wss", "server": ("wss.com", 443), "path": "/wss"}
    codeflash_output = _get_url(scope, "wss", None) # 1.83μs -> 1.60μs (14.2% faster)

def test_basic_wss_without_host_nondefault_port():
    # Basic WSS, no host, non-default port
    scope = {"scheme": "wss", "server": ("wss.com", 444), "path": "/wss"}
    codeflash_output = _get_url(scope, "wss", None) # 2.14μs -> 1.76μs (21.4% faster)

def test_basic_with_root_path():
    # Basic usage with root_path included
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": "/root", "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 1.97μs -> 1.59μs (23.9% faster)

def test_basic_with_empty_root_path():
    # Basic usage with empty root_path
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": "", "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 1.66μs -> 1.42μs (17.1% faster)

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

def test_edge_missing_scheme_uses_default():
    # Missing scheme, should use default_scheme
    scope = {"server": ("example.com", 80), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 1.88μs -> 1.45μs (29.2% faster)
    codeflash_output = _get_url(scope, "ws", None) # 1.23μs -> 899ns (36.5% faster)

def test_edge_missing_server_returns_path():
    # Missing server, should return path only
    scope = {"scheme": "http", "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 862ns -> 980ns (12.0% slower)

def test_edge_missing_path_returns_root_path_or_empty():
    # Missing path, should return root_path or empty string
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": "/root"}
    codeflash_output = _get_url(scope, "http", None) # 1.79μs -> 1.51μs (18.7% faster)
    scope2 = {"scheme": "http", "server": ("example.com", 80)}
    codeflash_output = _get_url(scope2, "http", None) # 1.27μs -> 934ns (36.3% faster)

def test_edge_missing_everything_returns_empty():
    # Missing everything, should return empty string
    scope = {}
    codeflash_output = _get_url(scope, "http", None) # 963ns -> 1.01μs (5.12% slower)

def test_edge_host_is_empty_string():
    # Host is empty string, should use server info
    scope = {"scheme": "http", "server": ("example.com", 80), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", "") # 1.80μs -> 1.45μs (24.3% faster)

def test_edge_host_is_falsey():
    # Host is False, should use server info
    scope = {"scheme": "http", "server": ("example.com", 80), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", False) # 1.86μs -> 1.39μs (34.4% faster)

def test_edge_server_port_is_none():
    # Server port is None, should not append port
    scope = {"scheme": "http", "server": ("example.com", None), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 2.24μs -> 1.75μs (27.9% faster)

def test_edge_server_is_none_and_host_is_none():
    # Both server and host are None, should return path (or root_path+path)
    scope = {"scheme": "http", "root_path": "/root", "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 1.03μs -> 1.08μs (4.63% slower)

def test_edge_server_tuple_incorrect_length():
    # Server tuple with incorrect length, should raise ValueError
    scope = {"scheme": "http", "server": ("example.com",), "path": "/foo"}
    with pytest.raises(ValueError):
        _get_url(scope, "http", None) # 2.86μs -> 2.96μs (3.31% slower)

def test_edge_path_is_empty_string():
    # Path is empty string, should return root_path or scheme://host
    scope = {"scheme": "http", "server": ("example.com", 80), "path": ""}
    codeflash_output = _get_url(scope, "http", None) # 2.05μs -> 1.62μs (26.0% faster)

def test_edge_root_path_and_path_are_empty():
    # Both root_path and path are empty
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": "", "path": ""}
    codeflash_output = _get_url(scope, "http", None) # 1.84μs -> 1.50μs (23.2% faster)

def test_edge_path_and_root_path_missing():
    # Both path and root_path missing
    scope = {"scheme": "http", "server": ("example.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 1.85μs -> 1.48μs (25.4% faster)

def test_edge_scheme_is_unknown():
    # Unknown scheme, should still work but not apply default port logic
    scope = {"scheme": "ftp", "server": ("ftp.example.com", 21), "path": "/file"}
    codeflash_output = _get_url(scope, "ftp", None) # 2.33μs -> 1.91μs (22.0% faster)

def test_edge_server_is_ipv6():
    # Server is IPv6 address
    scope = {"scheme": "http", "server": ("[2001:db8::1]", 80), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 1.87μs -> 1.44μs (30.0% faster)

def test_edge_server_is_ipv6_nondefault_port():
    # Server is IPv6, non-default port
    scope = {"scheme": "http", "server": ("[2001:db8::1]", 8080), "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 2.27μs -> 1.67μs (35.6% faster)

def test_edge_path_is_slash():
    # Path is just a slash
    scope = {"scheme": "http", "server": ("example.com", 80), "path": "/"}
    codeflash_output = _get_url(scope, "http", None) # 1.77μs -> 1.47μs (20.6% faster)

def test_edge_root_path_is_slash():
    # Root path is just a slash
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": "/", "path": "/foo"}
    codeflash_output = _get_url(scope, "http", None) # 1.99μs -> 1.54μs (29.0% faster)

def test_edge_root_path_and_path_slash():
    # Both root_path and path are slashes
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": "/", "path": "/"}
    codeflash_output = _get_url(scope, "http", None) # 1.90μs -> 1.48μs (28.2% faster)




def test_large_scale_many_hosts():
    # Test with up to 1000 different hosts
    for i in range(1, 1001):
        scope = {"scheme": "http", "server": (f"host{i}.com", 80), "path": f"/foo{i}"}
        codeflash_output = _get_url(scope, "http", None); result = codeflash_output # 825μs -> 559μs (47.6% faster)

def test_large_scale_long_path():
    # Test with a very long path (999 characters)
    long_path = "/" + "a" * 999
    scope = {"scheme": "http", "server": ("example.com", 80), "path": long_path}
    codeflash_output = _get_url(scope, "http", None); result = codeflash_output # 2.39μs -> 1.79μs (33.6% faster)

def test_large_scale_long_root_path_and_path():
    # Test with long root_path and path (each 500 chars)
    root_path = "/" + "r" * 500
    path = "/" + "p" * 499
    scope = {"scheme": "http", "server": ("example.com", 80), "root_path": root_path, "path": path}
    codeflash_output = _get_url(scope, "http", None); result = codeflash_output # 2.56μs -> 2.11μs (21.6% faster)

def test_large_scale_many_schemes_and_ports():
    # Test with 1000 different schemes and ports
    schemes = ["http", "https", "ws", "wss"]
    for i in range(1, 1001):
        scheme = schemes[i % 4]
        port = 8000 + i
        scope = {"scheme": scheme, "server": (f"host{i}.com", port), "path": f"/foo{i}"}
        codeflash_output = _get_url(scope, scheme, None); result = codeflash_output # 897μs -> 678μs (32.2% faster)
        # Only default ports should omit port in URL
        default_port = {"http": 80, "https": 443, "ws": 80, "wss": 443}[scheme]
        if port != default_port:
            pass
        else:
            pass

def test_large_scale_missing_server_returns_path():
    # Test with 1000 scopes missing server, should return path only
    for i in range(1, 1001):
        scope = {"scheme": "http", "path": f"/foo{i}"}
        codeflash_output = _get_url(scope, "http", None); result = codeflash_output # 442μs -> 411μs (7.41% faster)

def test_large_scale_all_empty():
    # Test with 1000 empty scopes, should return empty string
    for _ in range(1000):
        scope = {}
        codeflash_output = _get_url(scope, "http", None) # 433μs -> 404μs (7.12% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import pytest  # used for our unit tests
from sentry_sdk.integrations._asgi_common import _get_url

# unit tests

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

def test_basic_http_with_host():
    # Basic: HTTP scheme, host provided, no root_path
    scope = {"scheme": "http", "path": "/index", "server": ("example.com", 80)}
    codeflash_output = _get_url(scope, "http", "myhost.com") # 1.61μs -> 1.34μs (19.9% faster)

def test_basic_https_with_default_port():
    # Basic: HTTPS scheme, default port, host from server
    scope = {"scheme": "https", "path": "/secure", "server": ("secure.com", 443)}
    codeflash_output = _get_url(scope, "http", None) # 2.02μs -> 1.66μs (22.2% faster)

def test_basic_ws_with_non_default_port():
    # Basic: WS scheme, non-default port, host from server
    scope = {"scheme": "ws", "path": "/ws", "server": ("ws.example", 8080)}
    codeflash_output = _get_url(scope, "ws", None) # 2.33μs -> 1.77μs (31.6% faster)

def test_basic_root_path_and_path():
    # Basic: root_path and path concatenation
    scope = {"scheme": "http", "root_path": "/api", "path": "/v1", "server": ("api.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 2.02μs -> 1.61μs (25.8% faster)

def test_basic_host_priority():
    # Basic: host argument takes priority over server in scope
    scope = {"scheme": "http", "path": "/x", "server": ("ignored.com", 80)}
    codeflash_output = _get_url(scope, "http", "override.com") # 1.37μs -> 1.25μs (9.76% faster)

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

def test_edge_missing_scheme():
    # Edge: scheme missing, should use default_scheme
    scope = {"path": "/default", "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "ws", None) # 1.97μs -> 1.51μs (29.9% faster)

def test_edge_missing_server_and_host():
    # Edge: both server and host missing, should return just path
    scope = {"scheme": "http", "path": "/lonely"}
    codeflash_output = _get_url(scope, "http", None) # 933ns -> 1.00μs (6.79% slower)

def test_edge_missing_path_and_root_path():
    # Edge: path and root_path missing, should return empty string
    scope = {"scheme": "http", "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 1.93μs -> 1.47μs (31.7% faster)

def test_edge_empty_path_and_root_path():
    # Edge: path and root_path both empty
    scope = {"scheme": "http", "root_path": "", "path": "", "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 1.85μs -> 1.50μs (23.8% faster)

def test_edge_nonstandard_scheme_and_port():
    # Edge: unknown scheme, should not apply default port logic
    scope = {"scheme": "ftp", "path": "/file", "server": ("ftp.com", 2121)}
    codeflash_output = _get_url(scope, "http", None) # 2.35μs -> 1.88μs (25.3% faster)

def test_edge_default_port_for_nonstandard_scheme():
    # Edge: unknown scheme, port is 80, should include port
    scope = {"scheme": "ftp", "path": "/file", "server": ("ftp.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 2.24μs -> 1.78μs (25.8% faster)

def test_edge_host_is_empty_string():
    # Edge: host provided as empty string, should fallback to server
    scope = {"scheme": "http", "path": "/x", "server": ("real.com", 80)}
    codeflash_output = _get_url(scope, "http", "") # 1.90μs -> 1.52μs (25.0% faster)

def test_edge_server_is_none():
    # Edge: server is explicitly None, should return just path
    scope = {"scheme": "http", "path": "/y", "server": None}
    codeflash_output = _get_url(scope, "http", None) # 851ns -> 983ns (13.4% slower)



def test_edge_server_tuple_with_port_zero():
    # Edge: server tuple with port 0, should include port in URL
    scope = {"scheme": "http", "path": "/zero", "server": ("host.com", 0)}
    codeflash_output = _get_url(scope, "http", None) # 2.52μs -> 1.96μs (28.6% faster)

def test_edge_server_tuple_with_port_none():
    # Edge: server tuple with port None, should include 'None' as port
    scope = {"scheme": "http", "path": "/none", "server": ("host.com", None)}
    codeflash_output = _get_url(scope, "http", None) # 2.25μs -> 1.92μs (17.2% faster)

def test_edge_server_tuple_with_non_string_host():
    # Edge: server tuple with integer host, should convert to string
    scope = {"scheme": "http", "path": "/int", "server": (12345, 80)}
    codeflash_output = _get_url(scope, "http", None) # 2.14μs -> 1.71μs (25.1% faster)

def test_edge_path_with_special_characters():
    # Edge: path with special URL characters
    scope = {"scheme": "http", "path": "/a%20b?c=d", "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 1.83μs -> 1.45μs (25.9% faster)

def test_edge_root_path_and_path_with_slashes():
    # Edge: root_path ends and path starts with slash
    scope = {"scheme": "http", "root_path": "/api/", "path": "/v1", "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 1.98μs -> 1.64μs (20.7% faster)



def test_large_scale_many_paths():
    # Large Scale: Test with many different paths
    scope_base = {"scheme": "http", "server": ("host.com", 80)}
    for i in range(1000):
        scope = dict(scope_base)
        scope["path"] = "/resource%d" % i
        codeflash_output = _get_url(scope, "http", None) # 819μs -> 557μs (46.9% faster)

def test_large_scale_many_hosts():
    # Large Scale: Test with many different hosts
    scope_base = {"scheme": "http", "path": "/x"}
    for i in range(1000):
        host = "host%d.com" % i
        scope = dict(scope_base)
        scope["server"] = (host, 80)
        codeflash_output = _get_url(scope, "http", None) # 824μs -> 560μs (47.2% faster)

def test_large_scale_non_default_ports():
    # Large Scale: Test with many non-default ports
    scope_base = {"scheme": "http", "path": "/x"}
    for i in range(900, 1900):
        scope = dict(scope_base)
        scope["server"] = ("host.com", i)
        expected = "http://host.com:%d/x" % i if i != 80 else "http://host.com/x"
        codeflash_output = _get_url(scope, "http", None) # 892μs -> 648μs (37.5% faster)

def test_large_scale_long_path():
    # Large Scale: Very long path
    long_path = "/" + "a" * 999
    scope = {"scheme": "http", "path": long_path, "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 2.52μs -> 1.74μs (44.4% faster)

def test_large_scale_long_root_path():
    # Large Scale: Very long root_path
    long_root = "/" + "b" * 999
    scope = {"scheme": "http", "root_path": long_root, "path": "/c", "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 2.72μs -> 2.21μs (23.3% faster)

def test_large_scale_combined_long_root_and_path():
    # Large Scale: Very long root_path and path
    long_root = "/" + "r" * 500
    long_path = "/" + "p" * 499
    scope = {"scheme": "http", "root_path": long_root, "path": long_path, "server": ("host.com", 80)}
    codeflash_output = _get_url(scope, "http", None) # 2.57μs -> 2.12μs (21.4% faster)

def test_large_scale_host_argument():
    # Large Scale: Many host arguments, should always use host argument
    scope_base = {"scheme": "http", "path": "/x", "server": ("shouldnotuse.com", 80)}
    for i in range(1000):
        host = "override%d.com" % i
        scope = dict(scope_base)
        codeflash_output = _get_url(scope, "http", host) # 603μs -> 482μs (25.1% faster)

def test_large_scale_varied_schemes():
    # Large Scale: Test with all supported schemes and ports
    schemes = ["http", "https", "ws", "wss"]
    ports = [80, 443, 8080, 8443]
    for scheme in schemes:
        for port in ports:
            scope = {"scheme": scheme, "path": "/z", "server": ("host.com", port)}
            default_port = {"http": 80, "https": 443, "ws": 80, "wss": 443}[scheme]
            if port == default_port:
                expected = "%s://host.com/z" % scheme
            else:
                expected = "%s://host.com:%d/z" % (scheme, port)
            codeflash_output = _get_url(scope, scheme, None)

def test_large_scale_missing_keys():
    # Large Scale: Many scopes missing keys, should fallback to defaults
    for i in range(1000):
        scope = {"path": "/m%d" % i}
        codeflash_output = _get_url(scope, "http", None) # 424μs -> 406μs (4.34% faster)

def test_large_scale_server_is_none():
    # Large Scale: Many scopes with server=None
    for i in range(1000):
        scope = {"scheme": "http", "path": "/n%d" % i, "server": None}
        codeflash_output = _get_url(scope, "http", None) # 427μs -> 421μs (1.43% 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-_get_url-mg9g4mz4 and push.

Codeflash

The optimization achieves a **28% speedup** through two key performance improvements:

**1. Dictionary lookup optimization for default ports:**
- **Original:** Creates a new dictionary `{"http": 80, "https": 443, "ws": 80, "wss": 443}` on every call and performs `.get(scheme)`
- **Optimized:** Uses conditional expressions `80 if scheme in ("http", "ws") else 443 if scheme in ("https", "wss") else None`
- **Impact:** Eliminates dictionary object creation overhead (9.5% of original runtime per line profiler) and replaces with faster tuple membership tests

**2. String formatting optimization:**
- **Original:** Uses `%` formatting: `"%s://%s%s" % (scheme, host, path)`
- **Optimized:** Uses f-strings: `f"{scheme}://{host}{path}"`
- **Impact:** F-strings are faster in Python 3.6+ as they compile to more efficient bytecode operations

**3. Minor cleanup:**
- Removed redundant `None` argument from `asgi_scope.get("server", None)` since `None` is the default

**Performance characteristics from test results:**
- **Best gains** on cases requiring port logic (25-47% faster) where dictionary creation was most expensive
- **Consistent improvements** across all URL construction paths (17-35% typical range)
- **Minimal overhead** on simple path-only returns (some cases 5-13% slower due to f-string preparation, but these are rare edge cases)
- **Scales well** with high-volume scenarios (large scale tests show 25-47% improvements)

The optimization is most effective for typical ASGI applications that frequently construct URLs with server information and non-standard ports.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 2, 2025 13:23
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 2, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant