Reuse the client cookie jar instead of copying it per request
Merged
KL
19d ago+8+8+0123456789-9-9-0123456789
Stop copying the client cookie jar on every request, as reported in #1282. _merge_cookies copied it, then Request.__init__ copied it again, so each request cost time in proportion to every stored cookie. Now the client jar is used directly unless per-request cookies= need merging, and redirects reuse it too.
build_request() with cookies spread over 500 domains:
| Cookies | Before | After |
|---|---|---|
| 100 | 410 µs | 260 µs |
| 1,000 | 2.4 ms | 1.3 ms |
| 5,000 | 6.3 ms | 2.2 ms |
The remaining cost is the stdlib CookieJar.add_cookie_header(), which still scans every domain and expiry on each request. The CodSpeed benchmark from #1283 tracks this case.
AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.