std: InlineArray<T>, array chains that are always loops (rule inline fails closed) #163
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/inline-array"
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?
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)returnsxsitself, typedInlineArray<T>, orReadonlyInlineArray<T>for areadonlyarray. Nothing is copied, and the code works without the transform.InlineArray<T> extends Array<T>. Its methods that make a new array (map,filterincl. the prelude'sfilter(Boolean)→Truthy,flatMap,slice,concat,toSorted,toReversed,toSpliced,with) are typed to returnInlineArray<U>, so a result stored in a variable keeps the guarantee.extends Arrayand a readonly variant:ReadonlyArray#mapreturns a mutableU[], so a readonly brand can't typemapasInlineArray<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 mutableInlineArray.InlineArrayparameter is treated like aT[]by the mutation rules.xs.push(1)on a parameter reportsno-param-mutation, just as it does fornumber[], so a parameter that is changed needsMut<...>.Checker (
analyze.rs):InlineArraywhen it is declared with the type (imported from std'sarraymodule), initialised withinlineArray(...), or initialised with an array-giving method of one.forced(the@inlinepath).Erris reported under ruleinlineon the chain: "This chain is on anInlineArray, so it must become loops, and it can't: ". It is also reported when a callback usesq(...).safe-ts explainmarks these sites(InlineArray).as any, since a loop-built buffer has no brand.Bench (rows added to
fusion.bench.tsand thebench:checkbaseline; the same chains on anInlineArrayagainst the callbacks, ns/element at 10 / 1,000 / 100,000):What "always" costs is documented: a lone
filteris 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:
fixtures/inline_array: no diagnostics on the fine chains; errors on a multi-line function-expression callback and onawaitin a chain called in place; a plain array's lonefilterstays silent.packages/std/test/fusion/inline-array.test.ts: rewritten without@inline, results and callback order, a stored result still rewritten,inlineArrayis the identity. Also intypecheck:emit.cargo nextest run --workspace,npm run test:std,npm run typecheckandnpm run test:docspass.Docs:
InlineArray" section with the measured table.InlineArray" section, with a type-checked ❌/✅ pair.Known gaps:
InlineArrayreached 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.Update: rebased onto the new #162 (
ec5e4680). The 10-element InlineArray bench rows get the same 4x tolerance. All CI steps pass locally on head08563d33:cargo nextest run --workspace, fulltest:rewrites,test:std471/471,typecheck,test:soundness,test:vite,test:docs.🤖 Generated with Claude Code
inline)a098246d7608563d337f