Whole-program pass: 16 s down to under a second on a package with large code tables #111

Merged
sionzee merged 2 commits from perf-whole-program-pass into main 2026-11-21 11:12:27 +01:00
Owner

safe-ts check took about 16 seconds on an adopter's project, and nearly all of it came from one package of 58 files. An agent is meant to run the checker after every edit, so this defeated the point of the tool. That package now takes under a second.

Where the time went

Measured with temporary per-rule timing (none of it is in the commit):

  • Each question about a value's type started a new evaluator with an empty cache, so the type was worked out again from scratch every time, often twice per site.
  • For most types that costs microseconds. For a type derived from a large code table ((typeof X)[keyof typeof X] over a codesOf(...) table) it builds about 5,000 type instances and takes about 65 ms.
  • 146 strict-truthiness sites asking whether such a code can be falsy accounted for 12.3 of the 15.6 seconds.

The fix

What a type evaluates to is now kept for the rest of the pass and reused, together with the evaluator state it leaves behind.

The cache key holds everything the result depends on: the type's structure, the declaration its name resolves to (so every file importing the same type shares one answer, and a different type with the same name does not), the context, and the per-site settings. It is used only when the evaluator has not evaluated anything yet, so a hit is exactly a re-evaluation without the work. It lives for one check and is never reused across edits.

Result

Whole-program pass on the adopter's project, three runs each:

Before After
The slow package (58 files) 16.4 s, 16.5 s, 16.2 s 0.64 s, 0.65 s, 0.70 s
All entries in one run (346 files) 18.4 s, 19.6 s, 19.3 s 1.5 s, 1.5 s, 1.7 s

The other entries are unchanged within noise.

How it was checked

  • Findings are identical. The full JSON output of the old and new builds, sorted and compared byte for byte, matches on the adopter's entries with and without the idiomatic rules (673 findings), on its directories (1,092), on the slow package alone (359), and on the three examples.
  • A new fixture covers what a wrong cache key would break: one code table imported by several files, the same table under another name, another file declaring its own type with that name, and a local type with that name inside a function. Keying by name alone makes it fail.
  • The Rust suite (326 tests), test:std and typecheck pass.

To know

  • Peak memory for the all-entries run goes from about 300 MB to about 385 MB, because evaluated types are kept for the whole pass.
  • A second, smaller hot spot was measured and left alone: in the client app, about 0.5 s of its 0.7 s goes to sentinel-error, which searches every source file of the package as text for each file with candidate functions. Making it cheaper means deciding which files count as callers, so it is not a mechanical change.

Created with Claude Opus 5.5 (measurement and implementation) and Claude Fable 5.2 (review, and an independent rerun of the comparison and timings) in Claude Code.

`safe-ts check` took about 16 seconds on an adopter's project, and nearly all of it came from one package of 58 files. An agent is meant to run the checker after every edit, so this defeated the point of the tool. That package now takes under a second. ## Where the time went Measured with temporary per-rule timing (none of it is in the commit): - Each question about a value's type started a new evaluator with an empty cache, so the type was worked out again from scratch every time, often twice per site. - For most types that costs microseconds. For a type derived from a large code table (`(typeof X)[keyof typeof X]` over a `codesOf(...)` table) it builds about 5,000 type instances and takes about 65 ms. - 146 `strict-truthiness` sites asking whether such a code can be falsy accounted for 12.3 of the 15.6 seconds. ## The fix What a type evaluates to is now kept for the rest of the pass and reused, together with the evaluator state it leaves behind. The cache key holds everything the result depends on: the type's structure, the declaration its name resolves to (so every file importing the same type shares one answer, and a different type with the same name does not), the context, and the per-site settings. It is used only when the evaluator has not evaluated anything yet, so a hit is exactly a re-evaluation without the work. It lives for one check and is never reused across edits. ## Result Whole-program pass on the adopter's project, three runs each: | | Before | After | |---|---|---| | The slow package (58 files) | 16.4 s, 16.5 s, 16.2 s | 0.64 s, 0.65 s, 0.70 s | | All entries in one run (346 files) | 18.4 s, 19.6 s, 19.3 s | 1.5 s, 1.5 s, 1.7 s | The other entries are unchanged within noise. ## How it was checked - **Findings are identical.** The full JSON output of the old and new builds, sorted and compared byte for byte, matches on the adopter's entries with and without the idiomatic rules (673 findings), on its directories (1,092), on the slow package alone (359), and on the three examples. - A new fixture covers what a wrong cache key would break: one code table imported by several files, the same table under another name, another file declaring its own type with that name, and a local type with that name inside a function. Keying by name alone makes it fail. - The Rust suite (326 tests), `test:std` and `typecheck` pass. ## To know - Peak memory for the all-entries run goes from about 300 MB to about 385 MB, because evaluated types are kept for the whole pass. - A second, smaller hot spot was measured and left alone: in the client app, about 0.5 s of its 0.7 s goes to `sentinel-error`, which searches every source file of the package as text for each file with candidate functions. Making it cheaper means deciding which files count as callers, so it is not a mechanical change. Created with Claude Opus 5.5 (measurement and implementation) and Claude Fable 5.2 (review, and an independent rerun of the comparison and timings) in Claude Code.
Whole-program pass: evaluate a declared type once per pass, not once per question
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m36s
Tests / test (pull_request) Successful in 6m51s
5ef2d0547d
On an adopter's package of 58 files the whole-program pass took about 15 s.
Measured with timing instrumentation: 12.5 s of it was `type_answers`
(computed the first time `type_settled` asked, from `lib_effect` in the
`unhandled-failure` walk), and nearly all of that was 146 `strict-truthiness`
sites whose value is an error code, `(typeof Errors)[keyof typeof Errors]`
over a `codesOf(...)` table. Each question in `json::value` (`holds`,
`truth_kind`, `closed`, `literal`, ...) starts a new `Generator`, so the code
table (about 5,000 instantiations of tuple arithmetic, ~65 ms) was evaluated
again for every question; `literal` did the same for each `typeof Table.X`
(1,122 calls) in dispatch and member sets. The linear `answers.iter().any`
in `type_settled` was real but cost 15 ms.

`Generator::norm` now keeps what it gives in a `Memo` held by the pass's
lookup (`Run`) when it is asked before the generator has evaluated anything
(`pristine`) with no type parameters bound: the result and the generator
state it leaves, which then depend only on the type, where it is written
(for a named type without arguments: the name and the declaration it finds,
since `norm` uses that file only for the lookup), the context string and
the settings (`lazy_index`, `brands_as_strings`, `tags_only`), all in the
key. A hit restores that state, so the generator continues exactly as if it
had evaluated the type again. The state is behind `Arc` (copied on first
change), so a restore is cheap. Turned on for the value questions and the
code-table helpers (`literal_value`, `const_members`); validator codegen
is unchanged.

That package's pass drops to ~0.8 s; findings are identical to before on
four adopter entries and the repo's examples. A fixture checks that the
same name for different declarations, and a renamed import of one, keep
their own answers in both the whole-program and the editor path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixture: build the code table with one signature, not an overload
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m25s
Tests / test (pull_request) Successful in 6m17s
a9b538c03a
The overload form is a cast in overload form, which another open change reports
(no-overload). The one-signature form with `trust` evaluates to the same type,
and the fixture's expected findings are unchanged.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch perf-whole-program-pass 2026-11-21 11:12:28 +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!111
No description provided.