std: InlineArray<T>, array chains that are always loops (rule inline fails closed) #163

Merged
sionzee merged 7 commits from evobug/inline-array into main 2026-11-24 14:44:20 +01:00
Owner

Stacked on #162 (the engine). Its base is main, so the diff shows #162 too until that one merges. The commit for this PR is the last one.

InlineArray<T> in @evobug_com/safe-ts-std/array: an array whose method chains always become loops, as if each statement carried /** @inline */.

  • inlineArray(xs) returns xs itself, typed InlineArray<T>, or ReadonlyInlineArray<T> for a readonly array. Nothing is copied, and the code works without the transform.
  • InlineArray<T> extends Array<T>. Its methods that make a new array (map, filter incl. the prelude's filter(Boolean) → Truthy, flatMap, slice, concat, toSorted, toReversed, toSpliced, with) are typed to return InlineArray<U>, so a result stored in a variable keeps the guarantee.
  • Why extends Array and a readonly variant: ReadonlyArray#map returns a mutable U[], so a readonly brand can't type map as InlineArray<U> and still extend the lib type. ReadonlyInlineArray<T> is the parameter type: it can't be changed through it, and its methods return the mutable InlineArray.
  • Mutation: an InlineArray parameter is treated like a T[] by the mutation rules. xs.push(1) on a parameter reports no-param-mutation, just as it does for number[], so a parameter that is changed needs Mut<...>.

Checker (analyze.rs):

  • A binding is an InlineArray when it is declared with the type (imported from std's array module), initialised with inlineArray(...), or initialised with an array-giving method of one.
  • A chain whose receiver is one runs with forced (the @inline path).
  • An Err is reported under rule inline on the chain: "This chain is on an InlineArray, so it must become loops, and it can't: ". It is also reported when a callback uses q(...).
  • safe-ts explain marks these sites (InlineArray).
  • Transformed TS: a chain's array result is cast as any, since a loop-built buffer has no brand.

Bench (rows added to fusion.bench.ts and the bench:check baseline; the same chains on an InlineArray against the callbacks, ns/element at 10 / 1,000 / 100,000):

case callbacks InlineArray
filter→map(i)→forEach 9.4 / 10.3 / 30.3 2.7 / 1.8 / 2.4
request's example 66 / 118 / 182 58 / 116 / 164
filter(isPaid).map(amountOf).reduce(add, 0), imported functions 19.7 / 20.9 / 24.5 6.7 / 7.3 / 7.6
lone filter 4.5 / 5.5 / 5.9 4.9 / 6.2 / 7.2

What "always" costs is documented: a lone filter is 0.8–0.9x the speed of V8's. An imported function used after the first step gets its own loop, because its arity can't be seen, so the buffer the third argument would be gets built.

Tests:

  • New fixture fixtures/inline_array: no diagnostics on the fine chains; errors on a multi-line function-expression callback and on await in a chain called in place; a plain array's lone filter stays silent.
  • packages/std/test/fusion/inline-array.test.ts: rewritten without @inline, results and callback order, a stored result still rewritten, inlineArray is the identity. Also in typecheck:emit.
  • Results: cargo nextest run --workspace, npm run test:std, npm run typecheck and npm run test:docs pass.

Docs:

  • docs/guide/loops.md: new "InlineArray" section with the measured table.
  • docs/rules/inline.md: new "On an InlineArray" section, with a type-checked ❌/✅ pair.
  • Also std-helpers, the README tables, docs/README and the CHANGELOG.

Known gaps:

  • An InlineArray reached through a call's result, a member (cart.lines) or an import isn't recognised (local bindings only), so it gets the plain-array rules silently.
  • A function from another module has no visible arity.
  • A multi-line non-arrow callback can't be copied before the loop, so it is an error (the fix the message gives: use an arrow).

Update: rebased onto the new #162 (ec5e4680). The 10-element InlineArray bench rows get the same 4x tolerance. All CI steps pass locally on head 08563d33: cargo nextest run --workspace, full test:rewrites, test:std 471/471, typecheck, test:soundness, test:vite, test:docs.

🤖 Generated with Claude Code

Stacked on #162 (the engine). Its base is `main`, so the diff shows #162 too until that one merges. The commit for this PR is the last one. **`InlineArray<T>` in `@evobug_com/safe-ts-std/array`**: an array whose method chains always become loops, as if each statement carried `/** @inline */`. - `inlineArray(xs)` returns `xs` itself, typed `InlineArray<T>`, or `ReadonlyInlineArray<T>` for a `readonly` array. Nothing is copied, and the code works without the transform. - `InlineArray<T> extends Array<T>`. Its methods that make a new array (`map`, `filter` incl. the prelude's `filter(Boolean)` → `Truthy`, `flatMap`, `slice`, `concat`, `toSorted`, `toReversed`, `toSpliced`, `with`) are typed to return `InlineArray<U>`, so a result stored in a variable keeps the guarantee. - **Why `extends Array` and a readonly variant:** `ReadonlyArray#map` returns a mutable `U[]`, so a readonly brand can't type `map` as `InlineArray<U>` and still extend the lib type. `ReadonlyInlineArray<T>` is the parameter type: it can't be changed through it, and its methods return the mutable `InlineArray`. - **Mutation:** an `InlineArray` parameter is treated like a `T[]` by the mutation rules. `xs.push(1)` on a parameter reports `no-param-mutation`, just as it does for `number[]`, so a parameter that is changed needs `Mut<...>`. **Checker** (`analyze.rs`): - A binding is an `InlineArray` when it is declared with the type (imported from std's `array` module), initialised with `inlineArray(...)`, or initialised with an array-giving method of one. - A chain whose receiver is one runs with `forced` (the `@inline` path). - An `Err` is reported under rule `inline` **on the chain**: "This chain is on an `InlineArray`, so it must become loops, and it can't: <why>". It is also reported when a callback uses `q(...)`. - `safe-ts explain` marks these sites `(InlineArray)`. - Transformed TS: a chain's array result is cast `as any`, since a loop-built buffer has no brand. **Bench** (rows added to `fusion.bench.ts` and the `bench:check` baseline; the same chains on an `InlineArray` against the callbacks, ns/element at 10 / 1,000 / 100,000): | case | callbacks | InlineArray | |---|---|---| | filter→map(i)→forEach | 9.4 / 10.3 / 30.3 | 2.7 / 1.8 / 2.4 | | request's example | 66 / 118 / 182 | 58 / 116 / 164 | | filter(isPaid).map(amountOf).reduce(add, 0), imported functions | 19.7 / 20.9 / 24.5 | 6.7 / 7.3 / 7.6 | | lone filter | 4.5 / 5.5 / 5.9 | 4.9 / 6.2 / 7.2 | What "always" costs is documented: a lone `filter` is 0.8–0.9x the speed of V8's. An imported function used after the first step gets its own loop, because its arity can't be seen, so the buffer the third argument would be gets built. **Tests:** - New fixture `fixtures/inline_array`: no diagnostics on the fine chains; errors on a multi-line function-expression callback and on `await` in a chain called in place; a plain array's lone `filter` stays silent. - `packages/std/test/fusion/inline-array.test.ts`: rewritten without `@inline`, results and callback order, a stored result still rewritten, `inlineArray` is the identity. Also in `typecheck:emit`. - Results: `cargo nextest run --workspace`, `npm run test:std`, `npm run typecheck` and `npm run test:docs` pass. **Docs:** - docs/guide/loops.md: new "`InlineArray`" section with the measured table. - docs/rules/inline.md: new "On an `InlineArray`" section, with a type-checked ❌/✅ pair. - Also std-helpers, the README tables, docs/README and the CHANGELOG. **Known gaps:** - An `InlineArray` reached through a call's result, a member (`cart.lines`) or an import isn't recognised (local bindings only), so it gets the plain-array rules silently. - A function from another module has no visible arity. - A multi-line non-arrow callback can't be copied before the loop, so it is an error (the fix the message gives: use an arrow). **Update:** rebased onto the new #162 (`ec5e4680`). The 10-element InlineArray bench rows get the same 4x tolerance. All CI steps pass locally on head `08563d33`: `cargo nextest run --workspace`, full `test:rewrites`, `test:std` 471/471, `typecheck`, `test:soundness`, `test:vite`, `test:docs`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Loop rewrite: every array method, in any combination, split into loops at the methods without one
Some checks failed
Benchmarks / bench (pull_request) Failing after 8m22s
Tests / test (pull_request) Failing after 22m15s
44eb05fde4
A chain is now a list of steps over the receiver: `filter`, `map`, `flatMap`, `flat(d)` and
`slice(a, b)` stream into the next step; `forEach`, `reduce` (also without an initial value),
`some`, `every`, `find`, `findIndex`, `findLast`, `findLastIndex`, `includes`, `indexOf`,
`lastIndexOf` and `at(k)` end the loop; any other method (`toSorted`, `join`, `concat`, ...)
runs as written on the array the loop before it built, and a new loop starts over its result.
Holes are kept, skipped or read as `undefined` the way each method does; an index after a
`filter` counts what it kept. Callbacks by name (evaluated once, called with the method's
arguments), `thisArg`, a step that reads the `array` parameter (a new loop over the array it
asks for), and chains inside larger expressions (loop before the statement when the chain is
evaluated first in it, else an arrow called in place) are rewritten too.

On plain arrays this happens where packages/std/bench/fusion.bench.ts measured a win (every
chain of two or more steps; `forEach`, `map`, `flatMap`, `flat`, `findLast*` alone); `@inline`
gets all of it. packages/std/test/fusion/ compares 420 generated chains with the methods on
dense, sparse, empty and NaN/-0 arrays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
std: InlineArray<T>, chains that are always loops (fail closed under rule inline)
Some checks failed
Benchmarks / bench (pull_request) Successful in 6m25s
Tests / test (pull_request) Failing after 12m30s
a098246d76
`inlineArray(xs)` from @evobug_com/safe-ts-std/array gives `xs` typed as an `InlineArray`
(`ReadonlyInlineArray` for a readonly array and for parameters). At run time it is the array
itself. Every chain of methods on one becomes loops at build time, as if its statement carried
`/** @inline */`; a chain that can't is an error under `inline`, on the chain, with the reason.
Its methods that make a new array are typed to give an `InlineArray`, so a stored result keeps
the rewrite. The checker knows one from `inlineArray(...)`, a binding declared with the type, and
its methods' results.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed evobug/inline-array from a098246d76
Some checks failed
Benchmarks / bench (pull_request) Successful in 6m25s
Tests / test (pull_request) Failing after 12m30s
to 08563d337f
All checks were successful
Benchmarks / bench (pull_request) Successful in 4m19s
Tests / test (pull_request) Successful in 15m34s
2026-11-24 14:25:17 +01:00
Compare
Merge main into evobug/inline-array
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
83b49462b9
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch evobug/inline-array 2026-11-24 14:44:20 +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!163
No description provided.