Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

CookieJar.add_cookie_header() is linear in the number of stored domains

未關閉
#160,027 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
54/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
python

研究方向

Start with http.cookiejar.CookieJar.add_cookie_header(), DefaultCookiePolicy.domain_return_ok(), and clear_expired_cookies() in the standard library, then run the test_http_cookiejar tests. The issue identifies three possible optimization paths, from caching request host parsing to changing domain lookup and expiry tracking. Done means the chosen change reduces work as the number of stored domains grows while the cookie jar tests continue to pass.

由索引模型根據 Issue 內容生成。

描述

performance stdlib type-feature

Feature or enhancement

CookieJar.add_cookie_header() gets slower with every stored domain, even when only one cookie matches the request:

import timeit, urllib.request
from http.cookiejar import CookieJar, Cookie

def cookie(name, domain):
    return Cookie(0, name, "v", None, False, domain, True, False, "/", True, False, None, True, None, None, {})

for n in (10, 100, 1000, 10000):
    jar = CookieJar()
    for i in range(n):
        jar.set_cookie(cookie(f"c{i}", f"site{i}.example"))
    req = urllib.request.Request("https://site0.example/")
    t = min(timeit.repeat(lambda: jar.add_cookie_header(req), number=200, repeat=3)) / 200
    print(f"{n:>6} domains: {t*1e6:8.1f} us")
    10 domains:     37.9 us
   100 domains:    320.8 us
  1000 domains:   3086.9 us
 10000 domains:  30450.6 us

Each call:

  1. Runs DefaultCookiePolicy.domain_return_ok() for every stored domain, and each call re-parses the request URL via eff_request_host().
  2. Runs clear_expired_cookies(), which iterates every stored cookie.

Possible improvements, in increasing size:

  1. Compute eff_request_host() once per request instead of once per domain.
  2. With DefaultCookiePolicy, look up only the domain keys that can match the request host (its dot-suffixes) instead of iterating all domains.
  3. Track the earliest expiry in set_cookie() and skip the sweep until it has passed.

HTTPX2 ships 2 and 3 as a CookieJar subclass, which passes test_http_cookiejar: https://github.com/pydantic/httpx2/pull/1285

I'm happy to send a PR.

主要語言
Python
星號
77.7k
分支
38.3k
平均合併
14 小時 29 分鐘
30 天內合併 PR
699

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。