Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

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

Aberta
#160,027 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
54/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
backend, performance

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
Python
Estrelas
77.7k
Forks
38.3k
Merge médio
14h 29min
PRs com merge (30d)
699

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.