A check a quarter faster: analysis 209 ms to 149 ms and the pass 527 ms to 395 ms on a large entry, findings unchanged, with a guard that sees work growing #139

Merged
sionzee merged 11 commits from speed-two into main 2026-11-22 21:39:00 +01:00
Owner

A check runs in an agent's edit loop, in the editor and in the lint plugin, so how long it takes is part of the product. #126 took the whole-program pass on an adopter's largest entry from 1,120 ms to 465 ms. Then ten pieces landed in one day, each reviewed at "about 1 to 3%", and together they made a check a fifth slower. This takes that back and more, with every finding unchanged.

On that entry, shipped profile, minimum of 25 interleaved runs:

Before After
Per-file analysis 209 ms 149 ms
Whole-program pass 527 ms 395 ms
Pass, one thread 1,020 ms 916 ms
Pass, all directories 2,230 ms 1,900 ms

My own measurement with the fast profile agrees: analysis 223 ms to 155 ms and the pass 567 ms to 431 ms, both now below where they stood before the ten pieces (191 ms and 489 ms).

Where the time had gone

There is no perf on the machine this was done on, and its load swings wall times by a third, so the attribution was made with callgrind: instruction counts on one thread, which do not depend on load. From the tree before the ten pieces to main: analysis +27%, the pass +18%. The three largest shares were the spelling normalisation's second scope analysis of every file, the per-function look of the newest rules, and the reference index and scan of files for unused-code.

Two things turned that work into wall time, and they are what was cut:

  • The module graph was analysed layer by layer, so analysis took the sum of each layer's slowest file. It is now analysed as files are found, and the breadth-first order is replayed afterwards so that findings and unresolved imports come out in the order they did.
  • The pass is mostly one thread, and 15% of that thread hashed memo keys. A Path hashes byte by byte through SipHash; resolve_callee alone is asked 233,547 times on that entry and nearly always hits its memo. The memos now hash with the crate's existing fast hasher, and path keys as their bytes.

The cuts

Nine commits, each with its measured effect in its message:

  • Feature packs loaded once per package.json, not once per directory (manifests were parsed 22 times).
  • The pass's memos hash with FxHash; path keys hashed as bytes.
  • The module graph analysed as it is found.
  • Whether a package re-exports a test runner is worked out by one thread per module while the others wait, instead of by each of them.
  • The scan for files that declare brand sinks, the Vite configs' imports and sentinel-error's 121 package files run beside other work instead of in front of it.
  • A type alias's body is copied out of its declaration only where it is followed.

Three attempted cuts showed no gain and removed no repeated work, and were dropped. On one thread the pass's work fell 10% and its serial part 32%; most of the gain in analysis is scheduling, and the per-file work of the newest rules is as it was.

Nothing changed in what is reported

Byte-identical output against main after every cut: 592 cases (the adopter's entries one by one and together, its directories, the three examples, all 129 fixture directories, with and without --idiomatic), each as text, as --json and as stderr with the exit code, with the new binary run twice so that parallel work cannot make order depend on timing. I repeated it independently on the adopter, the examples and 21 held-out projects (210 runs): identical. safe-ts emit, the rewrite list and the soundness corpus are unchanged.

So that it shows next time

The work guard counted repeated questions, and new rules brought new work with counters of their own. It gains four counts on its fixed project:

  • memo_questions: every question asked of the four big memos, hits included.
  • allocations: fresh allocations of a whole check on one thread, stable to 10 in 250,000 between runs; the test fails above 1% over its ceiling. A rule that takes one more look at every function moves it by several percent.
  • feature_sets_loaded and alias_bodies_copied.

Reverting the pack cut, reverting the alias cut, adding a second per-function look, and asking again what every call runs each make the test fail. The hashing and scheduling cuts cannot be seen by a count; their revert is a visible change of a type in a diff.

Found and left, each needing a decision

  • A lighter analysis for the package files sentinel-error reads: they need calls, imports and one summary, and get all forty collectors. About half of the pass's parallel work.
  • A cache on disk for the test-runner question: every process parses about 600 modules of one package's re-export tree (31 ms) before the largest file can go on.
  • One per-function index for the rules that each walk every function (unused-answer, misplaced-argument, unbalanced-update): about 5% of per-file work, a refactor across three rules.
  • The normalisation's second scope analysis: skipping unchanged files would save less than re-parsing the changed ones costs.

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and verification) in Claude Code.

A check runs in an agent's edit loop, in the editor and in the lint plugin, so how long it takes is part of the product. #126 took the whole-program pass on an adopter's largest entry from 1,120 ms to 465 ms. Then ten pieces landed in one day, each reviewed at "about 1 to 3%", and together they made a check a fifth slower. This takes that back and more, with every finding unchanged. On that entry, shipped profile, minimum of 25 interleaved runs: | | Before | After | |---|---|---| | Per-file analysis | 209 ms | **149 ms** | | Whole-program pass | 527 ms | **395 ms** | | Pass, one thread | 1,020 ms | 916 ms | | Pass, all directories | 2,230 ms | 1,900 ms | My own measurement with the fast profile agrees: analysis 223 ms to 155 ms and the pass 567 ms to 431 ms, both now below where they stood before the ten pieces (191 ms and 489 ms). ## Where the time had gone There is no `perf` on the machine this was done on, and its load swings wall times by a third, so the attribution was made with callgrind: instruction counts on one thread, which do not depend on load. From the tree before the ten pieces to `main`: analysis +27%, the pass +18%. The three largest shares were the spelling normalisation's second scope analysis of every file, the per-function look of the newest rules, and the reference index and scan of files for `unused-code`. Two things turned that work into wall time, and they are what was cut: - **The module graph was analysed layer by layer**, so analysis took the sum of each layer's slowest file. It is now analysed as files are found, and the breadth-first order is replayed afterwards so that findings and unresolved imports come out in the order they did. - **The pass is mostly one thread, and 15% of that thread hashed memo keys.** A `Path` hashes byte by byte through SipHash; `resolve_callee` alone is asked 233,547 times on that entry and nearly always hits its memo. The memos now hash with the crate's existing fast hasher, and path keys as their bytes. ## The cuts Nine commits, each with its measured effect in its message: - Feature packs loaded once per `package.json`, not once per directory (manifests were parsed 22 times). - The pass's memos hash with FxHash; path keys hashed as bytes. - The module graph analysed as it is found. - Whether a package re-exports a test runner is worked out by one thread per module while the others wait, instead of by each of them. - The scan for files that declare brand sinks, the Vite configs' imports and `sentinel-error`'s 121 package files run beside other work instead of in front of it. - A type alias's body is copied out of its declaration only where it is followed. Three attempted cuts showed no gain and removed no repeated work, and were dropped. On one thread the pass's work fell 10% and its serial part 32%; most of the gain in analysis is scheduling, and the per-file work of the newest rules is as it was. ## Nothing changed in what is reported Byte-identical output against `main` after every cut: 592 cases (the adopter's entries one by one and together, its directories, the three examples, all 129 fixture directories, with and without `--idiomatic`), each as text, as `--json` and as stderr with the exit code, with the new binary run twice so that parallel work cannot make order depend on timing. I repeated it independently on the adopter, the examples and 21 held-out projects (210 runs): identical. `safe-ts emit`, the rewrite list and the soundness corpus are unchanged. ## So that it shows next time The work guard counted repeated questions, and new rules brought new work with counters of their own. It gains four counts on its fixed project: - **`memo_questions`**: every question asked of the four big memos, hits included. - **`allocations`**: fresh allocations of a whole check on one thread, stable to 10 in 250,000 between runs; the test fails above 1% over its ceiling. A rule that takes one more look at every function moves it by several percent. - **`feature_sets_loaded`** and **`alias_bodies_copied`**. Reverting the pack cut, reverting the alias cut, adding a second per-function look, and asking again what every call runs each make the test fail. The hashing and scheduling cuts cannot be seen by a count; their revert is a visible change of a type in a diff. ## Found and left, each needing a decision - **A lighter analysis for the package files `sentinel-error` reads**: they need calls, imports and one summary, and get all forty collectors. About half of the pass's parallel work. - **A cache on disk for the test-runner question**: every process parses about 600 modules of one package's re-export tree (31 ms) before the largest file can go on. - **One per-function index for the rules that each walk every function** (`unused-answer`, `misplaced-argument`, `unbalanced-update`): about 5% of per-file work, a refactor across three rules. - The normalisation's second scope analysis: skipping unchanged files would save less than re-parsing the changed ones costs. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and verification) in Claude Code.
`Project::features_for` remembered its answer per directory, so `features::load` (every pack's
manifest parsed, and later each `FeatureSet`'s API maps) ran again for every source directory
under the same package.json, and threads analyzing files of a new directory at the same time
each ran it. The load is a function of the package.json alone: it now runs once per
package.json (`feature_configs`, a `OnceLock` per config, so racing threads wait for one load),
and every directory under it shares the result.

Measured on an adopter's largest entry (`--idiomatic`, fast profile, 15 interleaved runs,
min / median ms): analysis 249.7 / 261.6 -> 241.1 / 259.0, pass 602.6 / 623.9 -> 593.6 / 618.8.
Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Most of the whole-program pass is the memos' hits: every rule asks again what call a callee is,
which file a path is, what a name binds, and each lookup hashed its key with the standard
hasher. On an adopter's largest entry, one thread, SipHash took 1.35 G of the pass's 9.34 G
instructions, 0.6 G of it on `Path`s (hashed component by component); `Run::resolve_callee`
(233,547 calls) and `Run::file` (213,915 calls) spent 0.26 G and 0.24 G hashing alone.

`borrow.rs` already had a fast hasher for its ids and memo keys (`IdHasher`, FxHash); it moves to
`fast_hash.rs`, and `Run`'s maps keyed by a path, a function key or a callee use it (the files,
the callees, bindings, type lookups, slots, values, package effects, failure and propagation
memos). No answer depends on a map's order: the standard hasher's order already changed from
run to run.

Measured (fast profile, 15 interleaved runs, min / median ms): analysis 228.3 / 249.3 ->
227.6 / 243.2 (unchanged), pass 566.1 / 576.0 -> 551.9 / 565.0. Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Project::reexports_runner` follows a package's `export * from` tree (up to four levels, each
module parsed) to see whether it passes on `vitest` or `node:test`, and remembers the finished
answer. Files analyzed side by side that import the same package each found no answer yet and
each followed the whole tree: on an adopter's largest entry, 25 such walks parsed 642 modules
(192 M instructions on one thread), many of them at once on different threads. A lock per
module now lets one thread work the answer out while the others asking meanwhile wait for it.

Measured (fast profile, 11 interleaved runs, min / median ms): analysis 232.4 / 247.4 ->
219.4 / 237.0, pass 559.5 / 570.0 -> 555.7 / 567.2 (unchanged). Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`module_graph_noting` analyzed the graph breadth-first, each layer in parallel, and waited at the
end of each layer for its slowest file: the analysis took the sum of each layer's slowest
file. On an adopter's largest entry the layers' slowest files were 22, 3, 102, 49 and 38 ms (a
338 KB file in the third layer, then the slowest of 104 files, then of 48), so most threads
waited most of the time.

`analyze_reachable` now starts a file as soon as a file importing it is analyzed (one
`rayon::scope`, a task per file found), so the time is that of the slowest chain of imports.
Each file's imports are resolved there, as before (globs first, then each import in order), and
the breadth-first walk is replayed over what was found: the files reached, the order of the
unresolved imports and every finding are as they were, whichever analysis ends first.

It needs the previous commit: analyzed side by side, every file importing the same package
worked out its re-exports at once.

Measured (fast profile, 15 interleaved runs, min / median ms): analysis 224.0 / 235.2 ->
163.7 / 174.7, pass 552.3 / 567.7 -> 555.9 / 568.4 (unchanged). Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Project::brand_sink_files` lists the project's files that call `brandSinks` (what each file's
analysis needs to know which member names a sink may have). It read every source file of the
project one at a time, on the analysis of the first file, which every other file's analysis
waits for: 12 ms on an adopter's project. The files are now read in parallel and kept in the
order they were listed.

Measured (fast profile, 15 interleaved runs, min / median ms): analysis 168.9 / 174.5 ->
154.4 / 166.1, pass 556.3 / 583.8 -> 563.9 / 574.2 (unchanged). Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`check_entries_named` warns when a Vite config imports code that only works after the transform
(`vite_config_warning`): it analyzes the config and what it imports at run time, one file
after another, at the start of the whole-program pass: 22 ms of it on an adopter's project.
It now runs beside the module graph's analysis, where most threads wait for the program's
longest chain of imports, and the warnings are assembled from its answers as before (the
same order, taken at the same point: the resolver's tsconfig warnings are still read after
both).

Measured (fast profile, 15 interleaved runs, min / median ms): analysis 153.3 / 161.9 ->
159.2 / 166.8, pass 559.5 / 569.1 -> 532.4 / 545.3. Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first `sentinel-error` question of a whole-program pass analyzes, side by side, the files
outside the program that the rule will read for every file that asks (an adopter's largest
entry: 121 tests and scripts, about 50 ms with every thread busy). It came after the work every
file's findings wait for, which is one thread's: the member names, the brands' sinks, members set
by the whole program, `unused-code`'s index and the event rules (about 38 ms).

The list of those files is now taken at the start of the pass (`Run::sentinel_wanted`, the first
half of `sentinel_prefetch`, which then finds none left), and they are analyzed on the other
threads while that work (`Run::program_wide`, the same steps in the same order) runs.

Measured (fast profile, 15 interleaved runs, min / median ms): pass 532.4 / 545.3 ->
500.2 / 509.1, analysis unchanged. Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With FxHash in the previous commit, hashing paths was still a fifth of the whole-program pass's
instructions on one thread (819 M of 3,724 M on an adopter's largest entry): a `Path` hashes
component by component, in a loop over every byte, and `Run::file` (213,915 calls) and
`Run::resolve_callee` (233,547) look up a path on each call.

`fast_hash::PathKey` hashes and compares a path as its bytes (one write, eight bytes at a
time), and `PathMap<V>` is a map by it with the methods of `HashMap<PathBuf, V>` the pass uses
(`get`, `contains_key`, `insert`, `entry`, `values`, `into_keys`, indexing), so every use of
`Run`'s path-keyed maps (`files`, `callees`, bindings, type lookups, slots, package effects,
modules, ...) reads as before. Two spellings of one path are two keys there: the memo asks
again and finds the same answer.

Measured (fast profile, 15 interleaved runs, min / median ms): pass 496.9 / 510.8 ->
459.8 / 466.1, analysis unchanged. Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`json::value`'s evaluator reads a value's members through the aliases naming its type
(`Eval::unalias`), following a body that is a name, an array or a tuple, and stopping at any
other. To be told, it opened the alias with `written::open_one`, which copied the whole body
out of the shared declaration first: for an alias of an object type (`type State = { ... }`,
the common case) a copy of every field, made and dropped to learn it isn't followed. On an
adopter's largest entry that was 5,584 openings, 205 M instructions of the pass's one thread
and 118 M more to drop them.

`written::step` is now `peek` (the declaration as found, with its type parameters bound) and
the copy; `open_one` takes what the caller follows and copies only that.

Measured (fast profile, 15 interleaved runs, min / median ms): pass 459.5 / 470.1 ->
439.2 / 445.1, analysis unchanged. Findings byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`tests/work.rs` counts work done from scratch: a question asked again where a memo was missed,
a file parsed again. Between the pass's speed work and the ten pieces after it the pass grew by
15% on an adopter's largest entry with every count held, because new work came as new walks,
copies and questions a memo answers, which no count saw.

- `memo_questions`: the questions asked of the pass's shared memos (`Run::resolve_callee`,
  `Run::file`, `Run::resolve_binding`, `Run::type_at`), answered from them or not. A rule that
  asks again about every call counts here.
- `allocations`: the allocations of the whole check, on one thread so that they come out the
  same each run (to about 10 in 250,000; growths aren't counted, which depend on the length of
  the temporary path), against a ceiling it may pass by 1%. A new walk over every function,
  a summary per function, a copy made to read a field all allocate.
- `feature_sets_loaded` and `alias_bodies_copied`, the work two cuts of this branch took out.

Counting is off unless a test turns it on (`work::reset`): `memo_questions` is asked hundreds of
thousands of times a pass. The generated project gains an alias of an object type read by
member (`Limits`, `cap{i}`), so that aliases are opened where they are read; the counts it adds
(calls, names, types, propagations, package calls) are in the new ceilings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
README and CHANGELOG: the checker's speed again, and what the work guard now counts
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m7s
Tests / test (pull_request) Successful in 10m46s
a286063fbd
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch speed-two 2026-11-22 21:39:00 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!139
No description provided.