Cache compiled known-module patterns across Config instances

Aperta
#2,805 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@Yin123-ybh ci sta già lavorando.

Dal 23/11/2026.

  • #2808 di @Yin123-ybh — aperta
  • #2810 di @Burakceliks — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
30/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
performance

Direzione di ricerca

Start at the known_patterns property in isort/settings.py and read how each pattern is expanded, compiled and paired with its placement. The proposal is a module-level functools.lru_cache(maxsize=128) helper keyed by the tuple of expanded pattern strings plus placement, returning an immutable tuple that each Config extends, with directory expansion left outside the cache. Benchmark with isort.code(source, profile="black") and run the unit and regression suites. Note the reporter already has this implemented on a branch and has offered a PR.

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

Descrizione

Config.known_patterns rebuilds its whole list on every new Config: for each known module pattern it does the wildcard substitution, calls re.compile, and allocates a (pattern, placement) pair. The default stdlib list alone is several hundred patterns. Python's re cache avoids some of the compile cost, but there are still hundreds of Python-level calls and allocations per config.

This shows up when using the API, since isort.code(source, profile="black") creates a fresh Config per call. Profiling an ordinary 8-import input put roughly 40% of the time in this property.

A possible fix: move the compilation into a module-level functools.lru_cache(maxsize=128) keyed by the tuple of expanded pattern strings plus the placement, returning an immutable tuple of compiled pairs. Each Config still gets its own list (extend from the cached tuple), directory expansion stays outside the cache so new local packages are still picked up, and invalid regexes still raise the same error.

Numbers for isort.code(..., profile="black") on mypyc builds, median over 15 alternating rounds of 250 calls each:

Input Size Before After Speedup
small 102 B 0.513 ms 0.273 ms 1.88x
isort/comments.py 1.7 KB 0.490 ms 0.206 ms 2.38x
isort/files.py 1.6 KB 0.666 ms 0.412 ms 1.62x
isort/io.py 2.0 KB 0.780 ms 0.515 ms 1.52x
isort/api.py 24.8 KB 1.141 ms 0.881 ms 1.29x

The gain is per Config, so it doesn't help the CLI startup or callers that already reuse a Config. It mostly helps editor integrations and services that call the API per file.

I have this on a branch with regression tests and a changelog entry (full test suite passes, output identical): https://github.com/TheNetherWatcher/isort/tree/perf/cache-known-patterns

Happy to open a PR with this change if it seems useful.

Lingua principale
Python
Stelle
7k
Fork
838
Merge medio
2g 6h
PR unite (30g)
19

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

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 PyCQA/isort

Tutte le issue di PyCQA/isort

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.