Skip to content

gh-160027: Use urlsplit() instead of urlparse() in http.cookiejar.request_host() - #160680

Open
eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:gh-160027-cookiejar-request-host
Open

eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:gh-160027-cookiejar-request-host

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

request_host() only needs the netloc, which is the same for urlsplit and urlparse. urlsplit() is cached while urlparse() is not and faster.

Benchmarks:

stored domains main PR
10 88 µs 41 µs
100 928 µs 326 µs
1000 13.0 ms 3.2 ms
10000 85.6 ms 32.7 ms

(benchmark script from the corresponding issue)

With this change the code is still linear in the number of stored domains.

request_host() only needs the netloc, which is the same for both, and
urlsplit() is cached while urlparse() is not.  It is called once for
every stored domain on each CookieJar.add_cookie_header() call, which is
now about 2.5x faster.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant