From d432b17f9de7f84df78a1cc20190ac438891ed49 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Sun, 22 Dec 2024 17:07:50 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`s?= =?UTF-8?q?elect=5Fproxy`=20by=2022%=20Sure!=20Here=20is=20a=20more=20opti?= =?UTF-8?q?mized=20version=20of=20the=20program.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Changes Made. 1. Import `urlparse` from `urllib.parse` instead of `src.requests.compat` for a more standard approach. 2. Removed the reassignment of `proxies` to itself with a default empty dictionary since we handle the `if not proxies` check earlier. 3. Instead of constructing a list and iterating over potentially multiple keys in a loop, use the `or` operator to check for each proxy key in a single statement, reducing overhead and making it faster. 4. Simplified by storing `urlparts.hostname` into `hostname`. These changes help in reducing the complexity of the function and should result in a better overall performance. --- src/requests/utils.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/requests/utils.py b/src/requests/utils.py index 699683e5d9..770e65e4b7 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -58,6 +58,7 @@ UnrewindableBodyError, ) from .structures import CaseInsensitiveDict +from urllib.parse import urlparse NETRC_FILES = (".netrc", "_netrc") @@ -844,22 +845,19 @@ def select_proxy(url, proxies): :param url: The url being for the request :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs """ - proxies = proxies or {} + if not proxies: + return None + urlparts = urlparse(url) - if urlparts.hostname is None: + hostname = urlparts.hostname + + if not hostname: return proxies.get(urlparts.scheme, proxies.get("all")) - proxy_keys = [ - urlparts.scheme + "://" + urlparts.hostname, - urlparts.scheme, - "all://" + urlparts.hostname, - "all", - ] - proxy = None - for proxy_key in proxy_keys: - if proxy_key in proxies: - proxy = proxies[proxy_key] - break + proxy = (proxies.get(f"{urlparts.scheme}://{hostname}") or + proxies.get(urlparts.scheme) or + proxies.get(f"all://{hostname}") or + proxies.get("all")) return proxy