Skip to content

Avoid copying the cookie jar on every request - #3897

Open
FullStraing wants to merge 3 commits into
encode:masterfrom
FullStraing:fix/cookie-jar-copy-2875
Open

FullStraing wants to merge 3 commits into
encode:masterfrom
FullStraing:fix/cookie-jar-copy-2875

Conversation

@FullStraing

Copy link
Copy Markdown

Summary

Fixes #2875

Problem

When a client holds cookies, building each request copied the entire
cookie jar twice:

  1. BaseClient._merge_cookies() built Cookies(self.cookies) (one
    jar.set_cookie() per cookie) plus an update() — even when there
    were no per-request cookies to merge in.
  2. Request.__init__ then built another throwaway Cookies(cookies)
    copy just to call set_cookie_header() on it.

Measured on current main (build_request × 300, trust_env=False):

client cookies before after
0 0.010s 0.009s
10 0.061s 0.047s
100 0.300s 0.208s
500 1.334s 0.955s

A cProfile run at 500 cookies showed 300,000 CookieJar.set_cookie
calls for 300 requests — the two copies — before this change.

Fix

  • _merge_cookies(): when there are no per-request cookies, return the
    client'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 a Cookies
    instance, call set_cookie_header() on it directly instead of
    copying the jar first.

One deliberate, documented side effect: set_cookie_header() ends in
stdlib CookieJar.clear_expired_cookies(), which now applies to the
caller'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.py 10 passed (7 existing + 3 new),
tests/models/test_cookies.py 7 passed, tests/models/test_requests.py
24 passed. With the source changes stashed, the two reuse tests fail and
the rest pass, confirming they pin the new behavior.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Slow performance when merging cookies

1 participant