Skip to content

Conversation

codeflash-ai[bot]
Copy link

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

📄 36% (0.36x) speedup for get_environ_proxies in src/requests/utils.py

⏱️ Runtime : 7.67 milliseconds 5.65 milliseconds (best of 92 runs)

📝 Explanation and details

The optimized code achieves a 35% speedup by addressing key performance bottlenecks in string processing and loop efficiency:

Key optimizations:

  1. Pre-compute string operations: Instead of repeatedly calling no_proxy.replace(" ", "").split(",") inside a generator expression for each loop iteration, the code now performs these operations once upfront and stores the result in no_proxy_hosts list.

  2. Cache hostname lookup: parsed.hostname is cached in a local variable hostname to avoid repeated attribute access throughout the function.

  3. Eliminate generator overhead: Replaced the generator expression (host for host in no_proxy.replace(" ", "").split(",") if host) with a list comprehension that filters empty hosts once, reducing iteration overhead.

  4. Optimize IP detection: The is_ipv4_address(hostname) result is cached in is_ip variable to avoid redundant calls.

  5. Streamline host_with_port construction: Instead of using += for string concatenation, uses f-string formatting which is more efficient.

Performance impact by test case type:

  • Large no_proxy lists: Show the most dramatic improvements (271% faster for matching, 49.9% for non-matching) because the string processing optimizations compound with list size
  • CIDR matching: 39.4% faster due to reduced overhead in the IP processing loop
  • Basic hostname matching: 10-18% faster from eliminating repeated string operations
  • Small lists: 6-15% improvements from reduced function call overhead

The optimizations are most effective when no_proxy contains many entries, as the upfront string processing cost is amortized across fewer loop iterations, while eliminating the repeated parsing that occurred in the original implementation.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 84 Passed
🌀 Generated Regression Tests 76 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_utils.py::TestGetEnvironProxies.test_bypass 200μs 181μs 10.5%✅
test_utils.py::TestGetEnvironProxies.test_bypass_no_proxy_keyword 87.1μs 78.2μs 11.4%✅
test_utils.py::TestGetEnvironProxies.test_not_bypass 330μs 302μs 9.23%✅
test_utils.py::TestGetEnvironProxies.test_not_bypass_no_proxy_keyword 593μs 510μs 16.3%✅
🌀 Generated Regression Tests and Runtime
import contextlib
import os
import socket
import struct
import sys
from urllib.parse import urlparse

# imports
import pytest
from requests.utils import get_environ_proxies


def getproxies():
    # Return all proxy-related environment variables as a dict
    proxies = {}
    for k, v in os.environ.items():
        if k.lower().endswith('_proxy') and v:
            proxies[k.lower()] = v
    return proxies
from requests.utils import get_environ_proxies

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

def test_no_proxies_set_returns_empty():
    # No proxies set in env, should return empty dict
    codeflash_output = get_environ_proxies("http://example.com") # 39.9μs -> 37.3μs (6.88% faster)

def test_http_proxy_set_returns_proxy():
    os.environ["http_proxy"] = "http://proxy.example.com:8080"
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 36.1μs -> 34.9μs (3.30% faster)

def test_https_proxy_set_returns_proxy():
    os.environ["https_proxy"] = "https://secureproxy.example.com:443"
    codeflash_output = get_environ_proxies("https://secure.com"); proxies = codeflash_output # 44.9μs -> 42.2μs (6.40% faster)

def test_uppercase_proxy_env_vars():
    os.environ["HTTP_PROXY"] = "http://proxy.example.com:8080"
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 45.5μs -> 42.5μs (7.02% faster)

def test_multiple_proxies_set():
    os.environ["http_proxy"] = "http://proxy1"
    os.environ["https_proxy"] = "http://proxy2"
    codeflash_output = get_environ_proxies("https://site.com"); proxies = codeflash_output # 47.8μs -> 42.7μs (12.2% faster)

def test_proxy_with_no_proxy_bypass():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "example.com"
    # Should bypass proxy for example.com
    codeflash_output = get_environ_proxies("http://example.com") # 11.9μs -> 10.4μs (14.2% faster)

def test_proxy_with_no_proxy_bypass_port():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "example.com:80"
    # Should bypass proxy for example.com:80
    codeflash_output = get_environ_proxies("http://example.com:80") # 17.3μs -> 15.8μs (9.40% faster)

def test_proxy_with_no_proxy_nonmatching():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "otherdomain.com"
    # Should NOT bypass proxy for example.com
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 52.3μs -> 48.7μs (7.54% faster)

def test_proxy_with_no_proxy_multiple_entries():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "other.com,example.com"
    # Should bypass proxy for example.com
    codeflash_output = get_environ_proxies("http://example.com") # 12.3μs -> 10.5μs (17.5% faster)

def test_proxy_with_no_proxy_spaces():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "  example.com  , other.com "
    codeflash_output = get_environ_proxies("http://example.com") # 11.7μs -> 10.4μs (12.8% faster)

def test_proxy_with_no_proxy_ip():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "192.168.1.1"
    codeflash_output = get_environ_proxies("http://192.168.1.1") # 15.0μs -> 13.8μs (8.29% faster)

def test_proxy_with_no_proxy_cidr():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "192.168.1.0/24"
    codeflash_output = get_environ_proxies("http://192.168.1.42") # 21.2μs -> 19.9μs (6.53% faster)

def test_proxy_with_no_proxy_invalid_cidr():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "192.168.1.0/33"
    codeflash_output = get_environ_proxies("http://192.168.1.42"); proxies = codeflash_output # 54.3μs -> 48.7μs (11.5% faster)

def test_proxy_with_no_proxy_invalid_ip():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "notanip"
    codeflash_output = get_environ_proxies("http://192.168.1.42"); proxies = codeflash_output # 46.2μs -> 47.3μs (2.48% slower)

def test_proxy_with_file_url():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # file:// URLs should always bypass proxies
    codeflash_output = get_environ_proxies("file:///tmp/file.txt") # 11.5μs -> 11.5μs (0.183% slower)

def test_proxy_with_no_proxy_none():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # no_proxy is not set, should use proxy
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 56.6μs -> 47.1μs (20.1% faster)

def test_proxy_with_explicit_no_proxy_arg():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # no_proxy passed as arg, should bypass
    codeflash_output = get_environ_proxies("http://example.com", no_proxy="example.com") # 11.7μs -> 10.0μs (16.5% faster)

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

def test_no_proxy_empty_string():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = ""
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 12.1μs -> 10.8μs (12.3% faster)

def test_no_proxy_comma_only():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = ",,,"
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 50.5μs -> 46.3μs (9.12% faster)

def test_no_proxy_hostname_subdomain():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "ample.com"
    # Should match 'example.com' ending
    codeflash_output = get_environ_proxies("http://example.com") # 11.4μs -> 9.99μs (13.7% faster)

def test_no_proxy_hostname_with_port():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "example.com:8080"
    # Should match 'example.com:8080'
    codeflash_output = get_environ_proxies("http://example.com:8080") # 18.2μs -> 16.6μs (9.55% faster)

def test_no_proxy_ipv4_address_not_matching():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "192.168.2.0/24"
    codeflash_output = get_environ_proxies("http://192.168.1.42"); proxies = codeflash_output # 63.3μs -> 59.6μs (6.34% faster)

def test_no_proxy_ipv4_address_exact():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "192.168.1.42"
    codeflash_output = get_environ_proxies("http://192.168.1.42") # 10.7μs -> 9.63μs (10.8% faster)

def test_no_proxy_ipv4_address_partial():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "192.168.1"
    codeflash_output = get_environ_proxies("http://192.168.1.42"); proxies = codeflash_output # 49.8μs -> 46.6μs (6.83% faster)

def test_no_proxy_invalid_host():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["no_proxy"] = "invalid_host"
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 52.4μs -> 49.8μs (5.22% faster)

def test_env_var_case_priority():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["NO_PROXY"] = "example.com"
    # Should match lowercase env var first
    codeflash_output = get_environ_proxies("http://example.com") # 53.0μs -> 47.5μs (11.7% faster)

def test_env_var_case_priority_upper_only():
    os.environ["http_proxy"] = "http://proxy.example.com"
    os.environ["NO_PROXY"] = "example.com"
    # Should work even if only uppercase is set
    codeflash_output = get_environ_proxies("http://example.com") # 50.3μs -> 46.1μs (9.24% faster)

def test_proxy_with_port_in_env():
    os.environ["http_proxy"] = "http://proxy.example.com:3128"
    codeflash_output = get_environ_proxies("http://example.com:8080"); proxies = codeflash_output # 51.3μs -> 48.2μs (6.51% faster)

def test_proxy_with_nonstandard_url_scheme():
    os.environ["http_proxy"] = "http://proxy.example.com"
    codeflash_output = get_environ_proxies("ftp://example.com"); proxies = codeflash_output # 61.0μs -> 53.5μs (14.1% faster)

def test_proxy_with_missing_hostname():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # URL with no hostname (e.g. file://)
    codeflash_output = get_environ_proxies("file:///etc/hosts") # 11.2μs -> 11.4μs (1.99% slower)

def test_proxy_with_invalid_url():
    os.environ["http_proxy"] = "http://proxy.example.com"
    codeflash_output = get_environ_proxies("not_a_url"); proxies = codeflash_output # 8.15μs -> 7.96μs (2.43% faster)

def test_proxy_with_ipv6_url():
    os.environ["http_proxy"] = "http://proxy.example.com"
    codeflash_output = get_environ_proxies("http://[2001:db8::1]"); proxies = codeflash_output # 76.3μs -> 71.5μs (6.75% faster)

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

def test_large_number_of_proxy_env_vars():
    # Set 500 proxy env vars
    for i in range(500):
        os.environ[f"http_proxy_{i}"] = f"http://proxy{i}.example.com"
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 197μs -> 192μs (2.72% faster)
    # Should contain all 500 proxies
    count = sum(1 for k in proxies if k.startswith("http_proxy_"))

def test_large_no_proxy_list():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # Create a no_proxy list with 500 entries, last entry matches
    no_proxy_list = ",".join([f"site{i}.com" for i in range(499)]) + ",example.com"
    os.environ["no_proxy"] = no_proxy_list
    codeflash_output = get_environ_proxies("http://example.com") # 237μs -> 64.1μs (271% faster)

def test_large_no_proxy_list_nonmatching():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # Create a no_proxy list with 500 entries, none match
    no_proxy_list = ",".join([f"site{i}.com" for i in range(500)])
    os.environ["no_proxy"] = no_proxy_list
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 519μs -> 346μs (49.9% faster)

def test_large_no_proxy_cidr_match():
    os.environ["http_proxy"] = "http://proxy.example.com"
    # 499 non-matching CIDRs, then one matching
    no_proxy_list = ",".join([f"10.{i}.0.0/16" for i in range(1, 500)]) + ",192.168.0.0/16"
    os.environ["no_proxy"] = no_proxy_list
    codeflash_output = get_environ_proxies("http://192.168.1.42") # 723μs -> 519μs (39.4% faster)

def test_large_scale_env_and_no_proxy():
    # Set 100 proxies and 100 no_proxy entries, last entry matches
    for i in range(100):
        os.environ[f"http_proxy_{i}"] = f"http://proxy{i}.example.com"
    no_proxy_list = ",".join([f"site{i}.com" for i in range(99)]) + ",example.com"
    os.environ["no_proxy"] = no_proxy_list
    codeflash_output = get_environ_proxies("http://example.com") # 58.3μs -> 22.4μs (161% faster)

def test_large_scale_env_and_no_proxy_nonmatching():
    # Set 100 proxies and 100 no_proxy entries, none match
    for i in range(100):
        os.environ[f"http_proxy_{i}"] = f"http://proxy{i}.example.com"
    no_proxy_list = ",".join([f"site{i}.com" for i in range(100)])
    os.environ["no_proxy"] = no_proxy_list
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 264μs -> 226μs (16.7% faster)
    count = sum(1 for k in proxies if k.startswith("http_proxy_"))
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import contextlib
import os
import socket
import struct
import sys
from urllib.parse import urlparse

# imports
import pytest
from requests.utils import get_environ_proxies


def proxy_bypass_environment(host):
    # This is a simplified version for testing purposes.
    no_proxy = os.environ.get("no_proxy") or os.environ.get("NO_PROXY")
    if not no_proxy or not host:
        return False
    host = host.lower()
    for entry in [h.strip().lower() for h in no_proxy.split(",") if h.strip()]:
        if entry.startswith("."):
            entry = entry[1:]
        if host.endswith(entry):
            return True
    return False

def getproxies():
    # For testing, just return the current environment proxy settings
    proxies = {}
    for proto in ["http", "https", "ftp"]:
        val = os.environ.get(proto + "_proxy") or os.environ.get(proto.upper() + "_PROXY")
        if val:
            proxies[proto] = val
    return proxies
from requests.utils import get_environ_proxies

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

def test_no_proxies_set_returns_empty(monkeypatch):
    # No proxies set, should return empty dict
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 52.4μs -> 48.9μs (7.03% faster)

def test_http_proxy_set(monkeypatch):
    # Set only http_proxy
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 41.9μs -> 41.7μs (0.455% faster)

def test_https_proxy_set(monkeypatch):
    # Set only https_proxy
    monkeypatch.setenv("https_proxy", "https://secureproxy.example.com:8443")
    codeflash_output = get_environ_proxies("https://secure.com"); proxies = codeflash_output # 41.9μs -> 43.1μs (2.68% slower)

def test_multiple_proxies(monkeypatch):
    # Set multiple proxies
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("https_proxy", "https://secureproxy.example.com:8443")
    monkeypatch.setenv("ftp_proxy", "ftp://ftpproxy.example.com:2121")
    codeflash_output = get_environ_proxies("ftp://ftp.example.com"); proxies = codeflash_output # 50.0μs -> 46.3μs (7.84% faster)

def test_uppercase_env(monkeypatch):
    # Set uppercase proxy environment variables
    monkeypatch.setenv("HTTP_PROXY", "http://proxy.example.com:8080")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 50.2μs -> 51.4μs (2.36% slower)

def test_priority_lowercase_over_uppercase(monkeypatch):
    # Lowercase should override uppercase
    monkeypatch.setenv("HTTP_PROXY", "http://proxy.example.com:8080")
    monkeypatch.setenv("http_proxy", "http://proxy2.example.com:8081")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 41.0μs -> 39.2μs (4.43% faster)

def test_no_proxy_bypass(monkeypatch):
    # Set http_proxy and no_proxy, should bypass for listed domain
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "example.com")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 12.0μs -> 10.4μs (15.5% faster)

def test_no_proxy_non_bypass(monkeypatch):
    # Set http_proxy and no_proxy, should NOT bypass for other domain
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "other.com")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 142μs -> 126μs (13.1% faster)

def test_no_proxy_multiple_domains(monkeypatch):
    # Multiple domains in no_proxy, should bypass for any matching
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "foo.com,example.com,bar.com")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 12.9μs -> 10.9μs (17.8% faster)

def test_no_proxy_with_spaces(monkeypatch):
    # no_proxy with spaces should be handled correctly
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", " foo.com , example.com , bar.com ")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 12.9μs -> 10.9μs (18.7% faster)

def test_no_proxy_with_port(monkeypatch):
    # no_proxy should match host:port
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "example.com:80")
    codeflash_output = get_environ_proxies("http://example.com:80"); proxies = codeflash_output # 18.6μs -> 16.7μs (11.4% faster)

def test_no_proxy_ip(monkeypatch):
    # no_proxy with IP address
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "192.168.1.1")
    codeflash_output = get_environ_proxies("http://192.168.1.1"); proxies = codeflash_output # 15.0μs -> 13.8μs (9.00% faster)

def test_no_proxy_cidr(monkeypatch):
    # no_proxy with CIDR range
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "192.168.1.0/24")
    codeflash_output = get_environ_proxies("http://192.168.1.42"); proxies = codeflash_output # 19.3μs -> 17.9μs (7.57% faster)

def test_no_proxy_invalid_cidr(monkeypatch):
    # no_proxy with invalid CIDR should NOT bypass
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "192.168.1.0/33")
    codeflash_output = get_environ_proxies("http://192.168.1.42"); proxies = codeflash_output # 49.4μs -> 47.6μs (3.68% faster)

def test_no_proxy_empty(monkeypatch):
    # no_proxy empty string should not bypass
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 12.7μs -> 11.3μs (12.6% faster)

def test_file_url(monkeypatch):
    # file:// URLs should always bypass proxies
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    codeflash_output = get_environ_proxies("file:///tmp/test.txt"); proxies = codeflash_output # 12.1μs -> 12.4μs (2.16% slower)

def test_no_proxy_none(monkeypatch):
    # no_proxy is None, should not bypass unless env matches
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    codeflash_output = get_environ_proxies("http://example.com", no_proxy=None); proxies = codeflash_output # 39.5μs -> 38.7μs (2.03% faster)

def test_no_proxy_argument(monkeypatch):
    # no_proxy passed as argument should override env
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "other.com")
    codeflash_output = get_environ_proxies("http://example.com", no_proxy="example.com"); proxies = codeflash_output # 11.2μs -> 10.2μs (10.1% faster)

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

def test_no_proxy_wildcard(monkeypatch):
    # no_proxy with wildcard should not bypass unless matched
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "*")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 34.4μs -> 31.6μs (8.65% faster)

def test_no_proxy_dot_prefix(monkeypatch):
    # no_proxy with .domain.com should match subdomains
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ".example.com")
    codeflash_output = get_environ_proxies("http://sub.example.com"); proxies = codeflash_output # 16.3μs -> 15.0μs (8.69% faster)

def test_no_proxy_partial_domain(monkeypatch):
    # no_proxy with partial domain should match suffix
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "ample.com")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 11.6μs -> 9.91μs (16.7% faster)

def test_no_proxy_port_only(monkeypatch):
    # no_proxy with only port should not match
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ":80")
    codeflash_output = get_environ_proxies("http://example.com:80"); proxies = codeflash_output # 13.9μs -> 12.4μs (12.5% faster)

def test_no_proxy_invalid_ip(monkeypatch):
    # no_proxy with invalid IP should not match
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "999.999.999.999")
    codeflash_output = get_environ_proxies("http://999.999.999.999"); proxies = codeflash_output # 16.0μs -> 14.6μs (9.70% faster)

def test_no_proxy_ipv6(monkeypatch):
    # no_proxy with IPv6 address should not match IPv4
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "2001:db8::1")
    codeflash_output = get_environ_proxies("http://192.168.1.1"); proxies = codeflash_output # 48.2μs -> 62.2μs (22.6% slower)

def test_no_proxy_ipv6_url(monkeypatch):
    # URL with IPv6 should not match IPv4 no_proxy
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", "192.168.1.1")
    codeflash_output = get_environ_proxies("http://[2001:db8::1]"); proxies = codeflash_output # 72.7μs -> 69.0μs (5.38% faster)

def test_no_proxy_empty_host(monkeypatch):
    # URL with no host should bypass
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    codeflash_output = get_environ_proxies("http:///"); proxies = codeflash_output # 11.7μs -> 11.7μs (0.017% slower)

def test_proxy_bypass_environment(monkeypatch):
    # Direct test of proxy_bypass_environment logic
    monkeypatch.setenv("no_proxy", "example.com")

def test_proxy_bypass_environment_subdomain(monkeypatch):
    # Subdomain should match .domain.com
    monkeypatch.setenv("no_proxy", ".example.com")

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

def test_large_number_of_proxies(monkeypatch):
    # Set many proxies, ensure all are present
    for i in range(100):
        proto = f"http{i}"
        monkeypatch.setenv(f"{proto}_proxy", f"http://proxy{i}.example.com:8080")
    # Only http, https, ftp are recognized by getproxies in our implementation
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("https_proxy", "https://secureproxy.example.com:8443")
    monkeypatch.setenv("ftp_proxy", "ftp://ftpproxy.example.com:2121")
    codeflash_output = get_environ_proxies("http://example.com"); proxies = codeflash_output # 228μs -> 227μs (0.763% faster)

def test_large_no_proxy_list(monkeypatch):
    # Large no_proxy list, should bypass for any matching
    domains = [f"site{i}.com" for i in range(100)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(domains))
    codeflash_output = get_environ_proxies("http://site42.com"); proxies = codeflash_output # 39.9μs -> 24.8μs (60.6% faster)

def test_large_non_matching_no_proxy(monkeypatch):
    # Large no_proxy list, should NOT bypass for non-matching
    domains = [f"site{i}.com" for i in range(100)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(domains))
    codeflash_output = get_environ_proxies("http://notlisted.com"); proxies = codeflash_output # 121μs -> 84.4μs (43.9% faster)

def test_large_no_proxy_cidr(monkeypatch):
    # Large CIDR list, should bypass for IP in range
    cidrs = [f"10.0.{i}.0/24" for i in range(50)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(cidrs))
    codeflash_output = get_environ_proxies("http://10.0.42.1"); proxies = codeflash_output # 104μs -> 85.3μs (22.6% faster)

def test_large_no_proxy_performance(monkeypatch):
    # Performance: large no_proxy list, non-matching
    domains = [f"site{i}.com" for i in range(999)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(domains))
    codeflash_output = get_environ_proxies("http://notlisted.com"); proxies = codeflash_output # 737μs -> 391μs (88.7% faster)

def test_large_no_proxy_performance_matching(monkeypatch):
    # Performance: large no_proxy list, matching at end
    domains = [f"site{i}.com" for i in range(999)] + ["finalsite.com"]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(domains))
    codeflash_output = get_environ_proxies("http://finalsite.com"); proxies = codeflash_output # 471μs -> 121μs (288% faster)

def test_large_no_proxy_with_ports(monkeypatch):
    # Large list with ports, should match host:port
    domains = [f"site{i}.com:8080" for i in range(500)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(domains))
    codeflash_output = get_environ_proxies("http://site42.com:8080"); proxies = codeflash_output # 51.1μs -> 41.2μs (24.1% faster)

def test_large_no_proxy_with_ips(monkeypatch):
    # Large list of IPs, should match correct IP
    ips = [f"10.0.0.{i}" for i in range(500)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(ips))
    codeflash_output = get_environ_proxies("http://10.0.0.42"); proxies = codeflash_output # 45.5μs -> 37.5μs (21.2% faster)

def test_large_mixed_no_proxy(monkeypatch):
    # Large mixed list of domains, IPs, CIDRs, ports
    entries = [f"site{i}.com" for i in range(300)] + \
              [f"10.0.1.{i}" for i in range(100)] + \
              [f"10.0.{i}.0/24" for i in range(50)] + \
              [f"site{i}.com:8080" for i in range(50)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(entries))
    codeflash_output = get_environ_proxies("http://10.0.42.1"); proxies = codeflash_output # 291μs -> 133μs (119% faster)

def test_large_no_proxy_non_matching(monkeypatch):
    # Large mixed list, non-matching host
    entries = [f"site{i}.com" for i in range(300)] + \
              [f"10.0.1.{i}" for i in range(100)] + \
              [f"10.0.{i}.0/24" for i in range(50)] + \
              [f"site{i}.com:8080" for i in range(50)]
    monkeypatch.setenv("http_proxy", "http://proxy.example.com:8080")
    monkeypatch.setenv("no_proxy", ",".join(entries))
    codeflash_output = get_environ_proxies("http://notlisted.com"); proxies = codeflash_output # 392μs -> 202μs (93.7% 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_environ_proxies-mgpwusy9 and push.

Codeflash

The optimized code achieves a 35% speedup by addressing key performance bottlenecks in string processing and loop efficiency:

**Key optimizations:**

1. **Pre-compute string operations**: Instead of repeatedly calling `no_proxy.replace(" ", "").split(",")` inside a generator expression for each loop iteration, the code now performs these operations once upfront and stores the result in `no_proxy_hosts` list.

2. **Cache hostname lookup**: `parsed.hostname` is cached in a local variable `hostname` to avoid repeated attribute access throughout the function.

3. **Eliminate generator overhead**: Replaced the generator expression `(host for host in no_proxy.replace(" ", "").split(",") if host)` with a list comprehension that filters empty hosts once, reducing iteration overhead.

4. **Optimize IP detection**: The `is_ipv4_address(hostname)` result is cached in `is_ip` variable to avoid redundant calls.

5. **Streamline host_with_port construction**: Instead of using `+=` for string concatenation, uses f-string formatting which is more efficient.

**Performance impact by test case type:**

- **Large no_proxy lists**: Show the most dramatic improvements (271% faster for matching, 49.9% for non-matching) because the string processing optimizations compound with list size
- **CIDR matching**: 39.4% faster due to reduced overhead in the IP processing loop  
- **Basic hostname matching**: 10-18% faster from eliminating repeated string operations
- **Small lists**: 6-15% improvements from reduced function call overhead

The optimizations are most effective when `no_proxy` contains many entries, as the upfront string processing cost is amortized across fewer loop iterations, while eliminating the repeated parsing that occurred in the original implementation.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 14, 2025 01:56
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 14, 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.

0 participants