perf: speed up discovery, plans, the run loop, --diff, and the language server
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
What
Makes complexipy faster without changing any score or plan: one parallel walk selects the files, refactor plans are measured on their own function, each file is read once per run, --diff reads the ref through one git cat-file --batch process, the language server analyzes off its message loop, and the Python API releases the GIL.
Closes #274. Related to #256: analysis now runs on a thread of its own, so a workspace scan would not block hover or inlay hint replies.
Why
A default run walked and read the tree twice and --report-ignored three times. Plan measurement re-parsed and re-scored the whole file once per plan, so --suggest-refactors grew quadratically with the file size. --diff spawned about two git processes per file, one after the other. The language server analyzed every dirty document on its message loop, so a configuration change held every hover until all open documents were analyzed, and the Python API held the GIL for the whole analysis.
Changes
Discovery (helpers/exclude.rs)
- One
ignorewalk, run in parallel, replaces the gitignore walk plus the wax walk. It skips gitignored and hidden directories without reading them and prunes directories an exhaustive exclude pattern covers. Exclusion still compiles the same wax program and matches the same root-relative path, soExcludeMatcherand the agreement tests inhelpers/exclude/tests.rsare unchanged. The selected files are identical; failed paths now print in path order instead of directory-read order. ExcludeMatcherkeeps the combined pattern program when it compiles, sois_excludedruns one regex instead of one per pattern.
Refactor plans (cognitive_complexity.rs, rules/registry.rs, fix.rs)
- A plan is measured by splicing it into its function's own lines (inside a
class _:header for a method), parsing that text, and scoring it. When the spliced text no longer parses as exactly that one function, the whole file is measured as before, soestimated_reductionalways equals the whole-file number. Module-level plans of--check-scriptstill measure the whole file. - The file's line ending is computed once per file instead of once per plan.
AnalysisOptions.plans_abovebuilds plans only for functions over the limit. The CLI sets it for--failed,--fix, and--dry-rununless the console, JSON, SARIF, or GitLab output lists the plans of passing functions; the fix pass always uses it.- With plans off, the analysis no longer collects definition names or looks up a rule list per function, and a file without the text
complexipy(the same case-insensitive literal both directive patterns require) skips every ignore-comment lookup. file_complexity()andcode_complexity()takewith_plans=True;with_plans=Falseskips plans. Documented indocs/anddocs/es/. The wasm build passes no plans, since the web demo never reads them.- mimalloc is the global allocator of the CLI, the language server, and the Python extension on macOS, Windows, and x86_64/aarch64 Linux. Other targets keep the system allocator.
Run loop (runner.rs, run.rs, utils/)
runner::Analysisreads and parses each file once and collects the ignore-comment report and--report-ignoredlocations from the same text, with the same paths and order as the separate passes produced.- Path arguments are analyzed in parallel, and the invocation path is canonicalized once.
--fixanalyzes again only the files it wrote, and refreshes their ignore reports.--suggest-refactorsreads each file's lines once per file instead of once per function.- The colored fix diff parses the syntax only up to the last line it shows and paints only the shown lines; the terminal width is read once per diff.
- The analysis cache keeps the other targets' entries as raw text borrowed from the file, rewrites the file only when this run's entry changed, writes the new entry compactly, and keeps at most 32 MiB of entries next to the 64-entry limit, dropping the least recently updated first.
--diff and --staged (diff.rs)
- One
git cat-file --batchprocess reads every file at the ref, instead ofgit showper probe. Each file is looked up at its exact repository path, fromrev-parse --show-toplevel, instead of probing shorter suffixes and then the basename throughls-files. A file that moved since the ref now reads as new, and a run started in a subdirectory now finds each file's history. A blob over 32 MiB, by the size the batch header reports, is skipped without being read and reads as missing. Parsing the old versions runs in parallel. The ref and thegit diffcall that lists staged files are unchanged.
Language server (server.rs, analysis.rs, documents.rs)
- Analysis and plan building run on a worker thread with a 256 MiB stack (the default stack if the platform cannot reserve it). A request whose analysis is missing waits for it and is answered when it returns; a flush keeps one analysis queued at a time, so a request's analysis runs right after the one in progress.
- Each document has its own debounce deadline, so typing in one document no longer delays the diagnostics of another.
$/cancelRequestanswers a request that is still waiting withRequestCancelled.- A document's text is stored once as
Arc<str>and shared with its analysis, and requests no longer clone the text or the analysis. - Stdout still carries protocol frames only.
Python bindings (complexipy-python, classes.rs)
file_complexity,code_complexity,collect_all_ignored_locations,collect_removable_ignored_locations, andcompute_diffrelease the GIL while they run.functions,line_complexities, andrefactor_plansbuild their Python objects once and keep them in the instance__dict__; each access returns a new list over those objects instead of copying the data. The attribute types are unchanged.
CI (release.yml)
- The Windows and macOS legs build only the interpreter they install (
--interpreterwith the setup-python path) instead of--find-interpreter, which rebuilt every interpreter on the image in every leg. - sccache is off in the release workflow: a tag run writes to that tag's cache scope, which no later tag can read.
Measurements
Apple M4, 10 cores. Medians of 7 interleaved runs (9 for the discovery and mimalloc rows), each with 1 to 2 warmups. The machine was shared with other jobs: the first eleven rows started at a 1-min load average of 4 or less, the rest ran at the load shown, so the ratio is the headline and absolute times are under that load. Before is main at 07e49d0. After is this branch without mimalloc; the last three rows add mimalloc on top. Instructions retired come from /usr/bin/time -l and do not depend on load.
| Workload | Before | After | Ratio | Instructions | 1-min load |
|---|---|---|---|---|---|
| File discovery only, django copy (2,927 files) | 189 ms | 65 ms | 2.9x | 2.85G → 1.95G | 3.7 |
File discovery only, django plus gitignored .venv and node_modules | 435 ms | 69 ms | 6.3x | 6.93G → 2.02G | 3.7 |
complexipy . -q, django | 367 ms | 193 ms | 1.9x | 8.32G → 6.09G | 3.7 |
complexipy ., django | 660 ms | 203 ms | 3.3x | 12.70G → 6.38G | 3.6-3.8 |
complexipy . --report-ignored, django | 839 ms | 197 ms | 4.3x | 16.79G → 6.37G | 3.8-4.1 |
complexipy . -q, django plus gitignored trees | 652 ms | 186 ms | 3.5x | 12.98G → 6.15G | 3.7-3.8 |
-q --suggest-refactors, django | 411 ms | 194 ms | 2.1x | 9.59G → 6.35G | 3.8-4.9 |
--failed --suggest-refactors, django | 702 ms | 263 ms | 2.7x | 15.07G → 7.57G | 3.8 |
-q --suggest-refactors --output-format json (every plan), django | 435 ms | 226 ms | 1.9x | 9.99G → 7.24G | 3.8-4.2 |
| Every plan as JSON, 346 KB file of 2,280 small functions | 14.6 s | 54 ms | 272x | 270G → 0.84G | 3.7-4.9 |
--failed --suggest-refactors, same file | 18.6 s | 29 ms | 636x | 270G → 0.20G | 3.8-7.8 |
--suggest-refactors, 86 KB file of 500 functions with plans | 884 ms | 78 ms | 11.3x | 16.16G → 1.29G | 6.8 |
337 file arguments, -q | 62 ms | 25 ms | 2.5x | 0.72G → 0.56G | 6.3 |
2,929 file arguments, -q | 627 ms | 207 ms | 3.0x | 7.05G → 5.57G | 6.3-7.1 |
| 2,929 file arguments, default output | 691 ms | 203 ms | 3.4x | 8.54G → 5.87G | 7.1 |
One file, -q, first run on a 415 MB cache of 64 entries | 2,295 ms (1.5 GB RSS) | 900 ms (464 MB RSS) | 2.6x | 23.26G → 12.38G | 7.4-8.0 |
One file, -q, the run after that | 2,212 ms (2.4 GB RSS) | 52 ms (37 MB RSS) | 43x | 21.12G → 0.67G | 7.8 |
One file, -q, 30 MB cache with one 200k-function entry | 176 ms | 102 ms | 1.7x | 2.05G → 1.27G | 7.9 |
--fix, fresh django copy | 1,611 ms | 701 ms | 2.3x | 26.56G → 10.70G | 6.9-7.9 |
--dry-run -C yes, django | 1,143 ms | 542 ms | 2.1x | 19.23G → 9.92G | 5.8-6.2 |
--diff HEAD~200 -q, flask with full history (189 git processes before, 2 after) | 2,580 ms | 46 ms | 56x | 0.88G → 0.31G | 6.2 |
ExcludeMatcher::is_excluded, 1,000 patterns, per call | 76.7 µs | 0.18 µs | 426x | 6.2 | |
mimalloc on top: complexipy . -q, django | 212 ms | 178 ms | 1.19x | 6.11G → 5.18G | 6.2-7.4 |
| mimalloc on top: every plan as JSON, django | 268 ms | 212 ms | 1.26x | 7.19G → 6.06G | 7.4-8.8 |
mimalloc on top: complexipy ., django | 225 ms | 186 ms | 1.21x | 6.38G → 5.34G | 8.8-9.4 |
Run-loop items measured on their own, single rayon thread, against the build of the previous commit: rendering --suggest-refactors on the 500-function file costs 2.25G → 0.98G instructions, the color in --dry-run -C yes on django costs 3.56G → 1.85G, and 337 file arguments take 127 ms → 44 ms.
Language server, medians of 7 sessions per build, load 4.4 to 6.7:
| Scenario | Before | After |
|---|---|---|
| Hover on a small file right after a configuration change, with twelve 50k-line files open | 252 ms, after 13 of 13 publishes | 43 ms, after 3 of 13 publishes |
| Diagnostics of file A when A changed once and B was then edited every 100 ms for 2 s | 2,274 ms | 203 ms |
| Hover right after an edit, 20k-line file | 23.9 ms | 14.8 ms |
| First hover on a function, 20k-line file | 13.8 ms | 6.8 ms |
| Repeated hover, 20k-line file | 0.068 ms | 0.021 ms |
Python API, code_complexity on a 9.8k-line file and an indexed loop over the 517 functions of a django test module, medians of 7 processes per build, load 5.7 to 6.0:
| Measure | Before | After |
|---|---|---|
code_complexity(src), plans on | 76.8 ms | 3.1 ms |
code_complexity(src, with_plans=False) | 1.8 ms | |
| Longest stall of another Python thread during one call, share of the call | 0.92 | 0.00 |
| 8 calls on 4 threads | 627 ms | 11.8 ms |
for i in range(n): result.functions[i].complexity | 16.3 ms | 0.28 ms |
Scores and plans stay identical
--output-format json --suggest-refactors(every plan field of every function) and--plainoutput are byte-identical tomain, with and without--check-script, on django (30,770 functions, 1,314 plans), flask, requests,tests/src,tests/fixtures/refactor_plans, andcomplexipy/.tests/main.pytotals are unchanged.- 54 CLI scenarios produce byte-identical stdout, stderr, exit codes, report files, and rewritten trees on copies of flask and requests with ignore comments added and on
django/contrib: default output,--report-ignored,--suggest-refactorswith and without color,--fixand--dry-runwith and without color, all four output formats,--check-script,--no-ignore, file arguments, and mixed arguments. A tree with failing paths differs only in the order of its failed-path lines. - The new walk selects the same files as the old one on 430 cases: file and directory symlinks, dangling links, link loops, hidden and gitignored directories, a directory named
pkg.py, 11 spellings of the root, and every pattern inhelpers/exclude/tests.rsplus invalid ones, which fail with the same message. --diffoutput is identical atHEAD~20,HEAD~200, andHEAD~500from the repository root, for an invalid ref, and for--staged; it differs only for files that moved since the ref and for runs started in a subdirectory, as described above.
Checks
cargo test --workspace, uv run pytest, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check, ruff check, ruff format --check, ty check, cargo check -p complexipy-core --no-default-features, and cargo check -p complexipy-wasm --target wasm32-unknown-unknown pass locally. The release.yml changes only run on a tag, so this PR's CI does not exercise them.