logging: Logger.setLevel() and logging.disable() are O(number of loggers)

Aperta
#160,936 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Start with Manager._clear_cache() and Logger.isEnabledFor() in CPython’s logging implementation, then inspect the logging tests that cover cache invalidation. The issue includes a benchmark reproducing quadratic behavior across thousands of loggers. Done means cache clearing scales with loggers that have cached values while preserving correct isEnabledFor() results after level changes.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

performance stdlib type-bug
Bug report

Every Logger.setLevel() and logging.disable() call goes through Manager._clear_cache(), which walks the whole loggerDict and clears each logger's isEnabledFor cache, even the ones that never cached anything. So setting the level on N loggers one by one is quadratic. That shows up in apps or test suites with thousands of loggers (one per module/plugin/connection) that get configured individually.

import logging, time

for n in (1000, 2000, 4000, 8000):
    logging.root.manager.loggerDict.clear()
    loggers = [logging.getLogger(f"app.mod{i}") for i in range(n)]
    for lg in loggers:
        lg.isEnabledFor(logging.DEBUG)
    t = time.perf_counter()
    for lg in loggers:
        lg.setLevel(logging.INFO)
    print(f"{n:5d} loggers: {time.perf_counter() - t:.3f} s")

On 3.14.7 this gives about 0.23 s at 2000 loggers, 0.89 s at 4000 and 4.4 s at 8000 (each doubling is about 4x).

A fix is to have the manager remember which loggers actually have something in their cache (a WeakSet filled on the isEnabledFor cache-miss path, which already holds the lock) and only clear those. The cache-hit path doesn't change. I have a patch ready and can open a PR.

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-160937
Lingua principale
Python
Stelle
77.8k
Fork
37.7k
Merge medio
15h 24m
PR unite (30g)
728

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.