stable-shape: a parameter read in a loop that gets more than four shapes #8

Merged
sionzee merged 1 commit from stable-shape-call-sites into main 2026-11-03 21:34:08 +01:00
Owner

stable-shape now also reports a parameter that gets object literals with more than four different key lists (sets or orders) from the calls in its file, when the function reads that parameter in a loop or an array callback. Past four shapes the inline cache goes megamorphic.

Extension of stable-shape, not a new rule

The rule page already says what this is about: "objects of one kind with different keys … past four gives up on the fast path". The fix is the same too: the same keys in the same order, with a missing key set to undefined. The only new thing is where the problem shows up, at the read instead of the creation. A separate rule would just be one more switch for the same advice. The code lives in its own module (megamorphic.rs, a second visitor called next to lints::check), because it collects facts over the whole file and reports at the end, unlike the local checks in lints.rs.

Measured first, and what that changed

Node 26.13, using the worker's method (batches of at least 25 ms, 12 rounds, median):

  • One property read: 1 shape 0.7–0.9 ns, 4 shapes 1.1–1.5 ns, 5 shapes 3.6–4.6 ns, 8 shapes 4.7–4.8 ns. The cliff is at 5, as shapes.md says.
  • A small function called with five literal shapes in a loop: no difference (0.25–0.30 ns per call either way). TurboFan inlines it, and escape analysis knows each literal's shape at the read. So "called in a hot loop" is not the signal; reporting it would be noise.
  • The same five shapes when the function reads the parameter in its own loop: 5.4x (117 vs 22 ns per call). The rule page's example (bench block): 65–113 µs vs 11–19 µs per 1,000 calls, 4–7x over eight runs.
  • Objects stored and read later by other code: 1.6x. That isn't detectable here, so it's listed under "can't see".

So it's reported only when the read is in a loop, a for…of/while/…, or a map/forEach/filter/… callback inside the callee itself (not a loop around the function).

Narrow on purpose

  • Only plain functions called by name in the same file (function f, const f = () =>), only arguments written as object literals with plain keys (spreads and computed keys aren't counted), and only parameters read as p.x or const { x } = p in that loop. A parameter destructured in the signature is read once, so it's never reported.
  • Calls from other files, objects passed in variables, methods, and stored objects are all documented as "can't see". Each of them could only add shapes, so a report is a lower bound.
  • Not done: the union-type version (a parameter typed as a union of more than four object types). Those reads really are megamorphic, but in SAFE-TS code that union is almost always an Enum read with match/switch on .tag. That's the pattern the project recommends, and "give the variants one shape" would give up exhaustiveness. The page says so and suggests measuring if such a read is in your hottest loop. I didn't think a version of this could be quiet on real code.

Noise

No new reports on examples/demo (main.ts, run.ts), examples/vue, any docs example, the docs theme/demo code, or any .ts file under packages/. I checked each file with safe-ts check.

Tests

  • New fixture crates/safe_ts_core/tests/fixtures/shapes/. Reported: a loop in the callee, a map callback in the callee, destructuring in the loop. Left alone: a loop around a small callee, a parameter destructured in the signature, exactly four shapes, spreads and variables, a loop around the function rather than inside it.
  • docs/rules/stable-shape.md: a new section with bad/good blocks (checked by docs.rs and tsc) and a compare bench.
  • cargo nextest run --workspace: 94/95 pass. The failure is fixtures_match_annotations on fixture features (main.ts:55, createDecipheriv), which fails the same way on main 0e01fb3 here (looks like @types/node drift).
  • npm run test:std: 115/116; every Node callable is mapped exactly once fails with stale node:sqlite/node:path entries, unrelated and the same on main (installed @types/node)
  • npm run test:vite: passes. npm run test:docs: passes (215 examples compile, site builds; the built page's bench spec replayed in Node runs both variants and they agree).

🤖 Generated with Claude Code

`stable-shape` now also reports a parameter that gets object literals with **more than four different key lists** (sets or orders) from the calls in its file, **when the function reads that parameter in a loop or an array callback**. Past four shapes the inline cache goes megamorphic. ## Extension of `stable-shape`, not a new rule The rule page already says what this is about: "objects of one kind with different keys … past four gives up on the fast path". The fix is the same too: the same keys in the same order, with a missing key set to `undefined`. The only new thing is where the problem shows up, at the read instead of the creation. A separate rule would just be one more switch for the same advice. The code lives in its own module (`megamorphic.rs`, a second visitor called next to `lints::check`), because it collects facts over the whole file and reports at the end, unlike the local checks in `lints.rs`. ## Measured first, and what that changed Node 26.13, using the worker's method (batches of at least 25 ms, 12 rounds, median): - One property read: 1 shape 0.7–0.9 ns, 4 shapes 1.1–1.5 ns, 5 shapes 3.6–4.6 ns, 8 shapes 4.7–4.8 ns. The cliff is at 5, as shapes.md says. - **A small function called with five literal shapes in a loop: no difference** (0.25–0.30 ns per call either way). TurboFan inlines it, and escape analysis knows each literal's shape at the read. So "called in a hot loop" is *not* the signal; reporting it would be noise. - The same five shapes when the function **reads the parameter in its own loop**: 5.4x (117 vs 22 ns per call). The rule page's example (bench block): 65–113 µs vs 11–19 µs per 1,000 calls, 4–7x over eight runs. - Objects stored and read later by other code: 1.6x. That isn't detectable here, so it's listed under "can't see". So it's reported only when the read is in a loop, a `for…of`/`while`/…, or a `map`/`forEach`/`filter`/… callback inside the callee itself (not a loop around the function). ## Narrow on purpose - Only plain functions called by name in the same file (`function f`, `const f = () =>`), only arguments written as object literals with plain keys (spreads and computed keys aren't counted), and only parameters read as `p.x` or `const { x } = p` in that loop. A parameter destructured in the signature is read once, so it's never reported. - Calls from other files, objects passed in variables, methods, and stored objects are all documented as "can't see". Each of them could only add shapes, so a report is a lower bound. - **Not done: the union-type version** (a parameter typed as a union of more than four object types). Those reads really are megamorphic, but in SAFE-TS code that union is almost always an `Enum` read with `match`/`switch` on `.tag`. That's the pattern the project recommends, and "give the variants one shape" would give up exhaustiveness. The page says so and suggests measuring if such a read is in your hottest loop. I didn't think a version of this could be quiet on real code. ## Noise No new reports on `examples/demo` (main.ts, run.ts), `examples/vue`, any docs example, the docs theme/demo code, or any `.ts` file under `packages/`. I checked each file with `safe-ts check`. ## Tests - New fixture `crates/safe_ts_core/tests/fixtures/shapes/`. Reported: a loop in the callee, a `map` callback in the callee, destructuring in the loop. Left alone: a loop around a small callee, a parameter destructured in the signature, exactly four shapes, spreads and variables, a loop around the function rather than inside it. - `docs/rules/stable-shape.md`: a new section with bad/good blocks (checked by `docs.rs` and tsc) and a `compare` bench. - `cargo nextest run --workspace`: 94/95 pass. The failure is `fixtures_match_annotations` on fixture `features` (`main.ts:55`, `createDecipheriv`), which fails the same way on `main` 0e01fb3 here (looks like `@types/node` drift). - `npm run test:std`: 115/116; `every Node callable is mapped exactly once` fails with stale node:sqlite/node:path entries, unrelated and the same on `main` (installed `@types/node`) - `npm run test:vite`: passes. `npm run test:docs`: passes (215 examples compile, site builds; the built page's bench spec replayed in Node runs both variants and they agree). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
V8's inline caches keep four shapes per property read; past that the read goes megamorphic.
The rule now reports a parameter that gets object literals with more than four different key
lists (sets or orders) from the calls in its file, when the function reads it in a loop or an
array callback: 4-7x slower in the rule page's bench (Node 26).

The loop inside the function is the condition because that is where it costs: a small
function called with five shapes in a loop is inlined, V8 knows each literal's shape at the
read, and it measured no slower. It stays narrow (same file, plain functions, literal
arguments); unions of object types (enums read with match) are not reported on purpose.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch stable-shape-call-sites 2026-11-03 21:34:08 +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!8
No description provided.