Repository navigation
Avoid copying the cookie jar on every request - #3897
Open
FullStraing wants to merge 3 commits into
Open
FullStraing wants to merge 3 commits into
FullStraing wants to merge 3 commits into
Conversation
Refactor cookie merging logic to improve efficiency by avoiding unnecessary copying of the cookie jar.
Refactor cookie handling to avoid copying the entire jar when setting cookie headers.
Add regression tests for cookie merging behavior in httpx.Client.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2875
Problem
When a client holds cookies, building each request copied the entire
cookie jar twice:
BaseClient._merge_cookies()builtCookies(self.cookies)(onejar.set_cookie()per cookie) plus anupdate()— even when therewere no per-request cookies to merge in.
Request.__init__then built another throwawayCookies(cookies)copy just to call
set_cookie_header()on it.Measured on current
main(build_request× 300,trust_env=False):A cProfile run at 500 cookies showed 300,000
CookieJar.set_cookiecalls for 300 requests — the two copies — before this change.
Fix
_merge_cookies(): when there are no per-request cookies, return theclient's cookies as they are; when the client has no cookies, return
the per-request cookies as they are. The full merge (copy + update)
now only happens when both sides actually have cookies.
Request.__init__: when the argument already is aCookiesinstance, call
set_cookie_header()on it directly instead ofcopying the jar first.
One deliberate, documented side effect:
set_cookie_header()ends instdlib
CookieJar.clear_expired_cookies(), which now applies to thecaller's jar rather than to a throwaway copy. Expired cookies were
never sent either way; they are now also removed from the jar at
request-build time instead of lingering in it.
Tests
New tests in
tests/client/test_cookies.py:test_merge_cookies_without_request_cookies_reuses_client_cookies—fails without the fix (a copy was returned), passes with it; also
asserts the built request still carries the client cookies.
test_merge_cookies_without_client_cookies_reuses_request_cookies—same for the other branch.
test_merge_cookies_still_merges_client_and_request_cookies—merge semantics unchanged: per-request cookies override client
cookies, and the client's jar is not mutated by a merge.
Results:
tests/client/test_cookies.py10 passed (7 existing + 3 new),tests/models/test_cookies.py7 passed,tests/models/test_requests.py24 passed. With the source changes stashed, the two reuse tests fail and
the rest pass, confirming they pin the new behavior.
Checklist