Cache compiled known-module patterns across Config instances
I maintainer di solito rispondono entro 2 giorni
@Yin123-ybh ci sta già lavorando.
Dal 23/11/2026.
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di PyCQA/isort
-
separate_packages splits imports from the same explicitly configured packageForse già presa @rahul05ranjan l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 2 giorni
-
force_sort_within_sections does not sort lazy imports by module nameForse già presa @rahul05ranjan l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 2 giorni
-
length_sort with native sort order puts a longer import firstForse già presa @simpleqt l’ha presa 24 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
PyCQA/isort#2698 · 2 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)Forse già presa @Tanvi-007 l’ha presa 6 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
PyCQA/isort#2799 · 2 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single importForse già presa @beduldul l’ha presa 29 giorni fa. Aperta
Difficoltà 1/5 1-3 ore Idoneità per principianti 15/100
PyCQA/isort#2752 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
TigreGotico/text2spoken#626 ·
I maintainer di solito rispondono entro 1 giorno
-
oncoref MappingProxyType in proteome.layers breaks deepcopy in reference_coverage._inventoryForse già presa @iskandr l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
pipecat-ai/pipecat#7014 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
NL-BioImaging/biomero#136 ·
I maintainer di solito rispondono entro 1 giorno
-
Installing from source leaves out all subpackages (fastai.vision, fastai.text, ...)Forse già presa @lejrn l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100