Cache compiled known-module patterns across Config instances

Open
#2,805 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@Yin123-ybh is already working on this.

Since Nov 23, 2026.

  • #2808 by @Yin123-ybh — open
  • #2810 by @Burakceliks — open

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyCQA/isort

All issues in PyCQA/isort

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.