Cache compiled known-module patterns across Config instances
Maintainers usually reply within 2 days
@Yin123-ybh is already working on this.
Since Nov 23, 2026.
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 30/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- performance
Research direction
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.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 7k
- Forks
- 838
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 19
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/isort
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
force_sort_within_sections does not sort lazy imports by module namePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
length_sort with native sort order puts a longer import firstPossibly taken @simpleqt claimed this 24 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
PyCQA/isort#2698 · 2 comments ·
Maintainers usually reply within 2 days
-
add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)Possibly taken @Tanvi-007 claimed this 6 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
PyCQA/isort#2799 · 2 comments ·
Maintainers usually reply within 2 days
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single importPossibly taken @beduldul claimed this 29 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 15/100
PyCQA/isort#2752 · 1 comment ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
TigreGotico/text2spoken#626 ·
Maintainers usually reply within 1 day