Avoid expensive cookie truthiness checks#953
Conversation
f41df8e to
d100c10
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
d100c10 to
cb1bf24
Compare
|
Local validation on updated head
I also replaced the custom-jar fallback test with a real |
|
Follow-up coverage fix is on Local validation under Python 3.11:
Hosted checks are now green for Python 3.10, 3.11, 3.12, 3.13, 3.14, |
|
Merged current upstream Conflict resolution was limited to Validation after the merge:
|
Merging this PR will not alter performance
Comparing Footnotes
|
Summary
Fixes #783 by avoiding expensive cookie truthiness checks in the request cookie merge path.
Cookies.__bool__use theCookieJarinternal cookie store when available instead of iterating throughdeepvalues_cookiesstoreRoot Cause
CookieJar.__iter__walks the nested cookie store viahttp.cookiejar.deepvalues. BothBaseClient._merge_cookies()and request construction can hit cookie truthiness checks on hot paths, so checking for cookie presence should avoid that traversal when the jar exposes its internal store.Validation
uv run --frozen python -m pytest -q tests/httpx2/models/test_cookies.py::test_cookies_bool_does_not_iterate_cookie_jar tests/httpx2/models/test_cookies.py::test_cookies_bool_iterates_custom_cookie_jar_without_cookie_store tests/httpx2/client/test_cookies.py::test_set_per_request_cookie_merges_with_client_cookiesuv run --frozen python -m pytest -q tests/httpx2/models/test_cookies.py tests/httpx2/client/test_cookies.pyscripts/checkuv run --frozen python -m pytest -q tests/httpx2(1422 passed, 1 skipped)python -m compileall -q src/httpx2/httpx2/_client.py src/httpx2/httpx2/_models.py tests/httpx2/client/test_cookies.py tests/httpx2/models/test_cookies.pygit diff --check origin/main...HEAD && git diff --checkUpstream CI is also green for Python 3.10 through 3.14, the check job, and security analysis.
Microbenchmark on this branch, 1,000 cookies and 200,000 truthiness checks: