stable-shape: a parameter read in a loop that gets more than four shapes #8
Loading…
Reference in a new issue
No description provided.
Delete branch "stable-shape-call-sites"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
stable-shapenow 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 ruleThe 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 tolints::check), because it collects facts over the whole file and reports at the end, unlike the local checks inlints.rs.Measured first, and what that changed
Node 26.13, using the worker's method (batches of at least 25 ms, 12 rounds, median):
So it's reported only when the read is in a loop, a
for…of/while/…, or amap/forEach/filter/… callback inside the callee itself (not a loop around the function).Narrow on purpose
function f,const f = () =>), only arguments written as object literals with plain keys (spreads and computed keys aren't counted), and only parameters read asp.xorconst { x } = pin that loop. A parameter destructured in the signature is read once, so it's never reported.Enumread withmatch/switchon.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.tsfile underpackages/. I checked each file withsafe-ts check.Tests
crates/safe_ts_core/tests/fixtures/shapes/. Reported: a loop in the callee, amapcallback 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 bydocs.rsand tsc) and acomparebench.cargo nextest run --workspace: 94/95 pass. The failure isfixtures_match_annotationson fixturefeatures(main.ts:55,createDecipheriv), which fails the same way onmain0e01fb3here (looks like@types/nodedrift).npm run test:std: 115/116;every Node callable is mapped exactly oncefails with stale node:sqlite/node:path entries, unrelated and the same onmain(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