Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#160,027 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
699

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。