perf: speed up discovery, plans, the run loop, --diff, and the language server

Merged
DS
1mo ago
+2764-761

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 ignore walk, 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, so ExcludeMatcher and the agreement tests in helpers/exclude/tests.rs are unchanged. The selected files are identical; failed paths now print in path order instead of directory-read order.
  • ExcludeMatcher keeps the combined pattern program when it compiles, so is_excluded runs 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, so estimated_reduction always equals the whole-file number. Module-level plans of --check-script still measure the whole file.
  • The file's line ending is computed once per file instead of once per plan.
  • AnalysisOptions.plans_above builds plans only for functions over the limit. The CLI sets it for --failed, --fix, and --dry-run unless 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() and code_complexity() take with_plans=True; with_plans=False skips plans. Documented in docs/ and docs/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::Analysis reads and parses each file once and collects the ignore-comment report and --report-ignored locations 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.
  • --fix analyzes again only the files it wrote, and refreshes their ignore reports.
  • --suggest-refactors reads 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 --batch process reads every file at the ref, instead of git show per probe. Each file is looked up at its exact repository path, from rev-parse --show-toplevel, instead of probing shorter suffixes and then the basename through ls-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 the git diff call 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.
  • $/cancelRequest answers a request that is still waiting with RequestCancelled.
  • 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, and compute_diff release the GIL while they run.
  • functions, line_complexities, and refactor_plans build 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 (--interpreter with 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.

WorkloadBeforeAfterRatioInstructions1-min load
File discovery only, django copy (2,927 files)189 ms65 ms2.9x2.85G → 1.95G3.7
File discovery only, django plus gitignored .venv and node_modules435 ms69 ms6.3x6.93G → 2.02G3.7
complexipy . -q, django367 ms193 ms1.9x8.32G → 6.09G3.7
complexipy ., django660 ms203 ms3.3x12.70G → 6.38G3.6-3.8
complexipy . --report-ignored, django839 ms197 ms4.3x16.79G → 6.37G3.8-4.1
complexipy . -q, django plus gitignored trees652 ms186 ms3.5x12.98G → 6.15G3.7-3.8
-q --suggest-refactors, django411 ms194 ms2.1x9.59G → 6.35G3.8-4.9
--failed --suggest-refactors, django702 ms263 ms2.7x15.07G → 7.57G3.8
-q --suggest-refactors --output-format json (every plan), django435 ms226 ms1.9x9.99G → 7.24G3.8-4.2
Every plan as JSON, 346 KB file of 2,280 small functions14.6 s54 ms272x270G → 0.84G3.7-4.9
--failed --suggest-refactors, same file18.6 s29 ms636x270G → 0.20G3.8-7.8
--suggest-refactors, 86 KB file of 500 functions with plans884 ms78 ms11.3x16.16G → 1.29G6.8
337 file arguments, -q62 ms25 ms2.5x0.72G → 0.56G6.3
2,929 file arguments, -q627 ms207 ms3.0x7.05G → 5.57G6.3-7.1
2,929 file arguments, default output691 ms203 ms3.4x8.54G → 5.87G7.1
One file, -q, first run on a 415 MB cache of 64 entries2,295 ms (1.5 GB RSS)900 ms (464 MB RSS)2.6x23.26G → 12.38G7.4-8.0
One file, -q, the run after that2,212 ms (2.4 GB RSS)52 ms (37 MB RSS)43x21.12G → 0.67G7.8
One file, -q, 30 MB cache with one 200k-function entry176 ms102 ms1.7x2.05G → 1.27G7.9
--fix, fresh django copy1,611 ms701 ms2.3x26.56G → 10.70G6.9-7.9
--dry-run -C yes, django1,143 ms542 ms2.1x19.23G → 9.92G5.8-6.2
--diff HEAD~200 -q, flask with full history (189 git processes before, 2 after)2,580 ms46 ms56x0.88G → 0.31G6.2
ExcludeMatcher::is_excluded, 1,000 patterns, per call76.7 µs0.18 µs426x6.2
mimalloc on top: complexipy . -q, django212 ms178 ms1.19x6.11G → 5.18G6.2-7.4
mimalloc on top: every plan as JSON, django268 ms212 ms1.26x7.19G → 6.06G7.4-8.8
mimalloc on top: complexipy ., django225 ms186 ms1.21x6.38G → 5.34G8.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:

ScenarioBeforeAfter
Hover on a small file right after a configuration change, with twelve 50k-line files open252 ms, after 13 of 13 publishes43 ms, after 3 of 13 publishes
Diagnostics of file A when A changed once and B was then edited every 100 ms for 2 s2,274 ms203 ms
Hover right after an edit, 20k-line file23.9 ms14.8 ms
First hover on a function, 20k-line file13.8 ms6.8 ms
Repeated hover, 20k-line file0.068 ms0.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:

MeasureBeforeAfter
code_complexity(src), plans on76.8 ms3.1 ms
code_complexity(src, with_plans=False)1.8 ms
Longest stall of another Python thread during one call, share of the call0.920.00
8 calls on 4 threads627 ms11.8 ms
for i in range(n): result.functions[i].complexity16.3 ms0.28 ms

Scores and plans stay identical

  • --output-format json --suggest-refactors (every plan field of every function) and --plain output are byte-identical to main, with and without --check-script, on django (30,770 functions, 1,314 plans), flask, requests, tests/src, tests/fixtures/refactor_plans, and complexipy/. tests/main.py totals 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-refactors with and without color, --fix and --dry-run with 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 in helpers/exclude/tests.rs plus invalid ones, which fail with the same message.
  • --diff output is identical at HEAD~20, HEAD~200, and HEAD~500 from 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.