
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0040: Perceus v2: arities in Core, sized reuse, tokens across calls, @fip
Status: Proposed (2026-10-13, issue #157); needs engineering review (#44). Settles the for review item of ADR 0002's Implementation (v1) on reuse sizes, and does the "arities in Core" target of its list.
Context
Perceus v1 (ADR 0002, issue #34) reuses the cell of a matched value for a constructor built later (FBIP). Core had no types, so:
- a reuse checked the token's size at run time, and freed it and allocated when it didn't match;
- the release of a token cleared every moved field, and the reuse wrote every field again, including those that keep their value (
State(..s, count: n)rewrote every field ofs); - tuples and single-constructor values are destructured without a
switch, so they gave no token at all; - a function that matches a value but builds nothing could not give the cell to the function it calls, which builds one;
- nothing let a programmer ask for in-place updates and be told when they don't happen.
Issue #157 asks for all of it; the owner asked to land it if no benchmark is more than 5% slower. Everything landed but the second point, reuse specialisation, which broke that bar (below).
Decision
Arities in Core
FnDecl::shapes maps a variable to the arities of its type's constructors, by tag (a tuple has one). Lowering records it where it knows the type: the scrutinee of a match, a tuple or single-constructor pattern in let, and the base of a record update. Nothing else in Core changes; the field is empty for code made up later.
In the specialiser, a switch branch knows which constructor it is on (Tag::Is), and a default branch which ones it is not (Tag::Not): the arity of a released cell is then exact when every remaining constructor has the same one.
Sized reuse
When the arity n is known, only a construction of exactly n fields reuses the token (Fit::Exact), and the reuse is sized: no size check at run time, only the uniqueness test. Otherwise the v1 rule stays (Fit::AtLeast, checked at run time). Core prints reuse v7 sized in State#0(v4, v5).
In place, the header is rewritten in one 8-byte store (tl_reuse_header in the generated prelude: unique, the new tag and size, no flags), instead of v1's 2-byte store of the tag. A compiler that knows the count is already 1 stores part of the header only; the store is volatile to prevent it. One store measured faster than a partial one (see Benchmarks). The runtime ABI does not change.
Tuples and single-constructor values
They are released like matched values when their shape is recorded: let (a, b) = p; (b, a) reuses p's cell, and State(..s, count: s.count + 1) reuses s's.
Borrow inference had to follow: a parameter it only destructures was borrowed (ADR 0002), so its cell could not be reused. Such a parameter now stays owned when the function, or a function it calls directly, builds a cell of one of its arities. One that is only read (fn first(p) { p.0 }) stays borrowed.
Tokens across calls
A path of the release with no fitting construction gives the token to the first direct call on its straight path (no branch in between) of a function that builds a cell of that size without reusing one. That function gets a copy, g$reuse{n}, taking the token as one more parameter and placing it in its own first fitting construction on each path (or giving it on, or dropping it). Functions are specialised callees first, so a caller knows what its callees build. Copies are made on demand, and making one may ask for another.
@fip
An attribute on a function, after Koka's: the function must run fully in place. It is checked on Core after Perceus (louise_perceus::fip), since only then is reuse decided:
- every cell it builds is a sized reuse of a cell it released;
- it makes no closure, calls no function value, performs or handles no effect, opens no region, defers nothing;
- it calls only functions that allocate nothing, inferred (the greatest fixed point, so recursion alone doesn't allocate), and externs from a list that never allocate.
Intarithmetic is on the list, though a result beyond 62 bits is a big integer on the heap: like Koka, the promise counts on small integers.Floats are boxed, so only their comparisons are.
Freeing is allowed: Core is untyped, so "frees nothing" (Koka's fip as opposed to fbip) can't be told from the code. The promise holds when the arguments are unique; a shared argument is copied, as everywhere. An @fip function is not transformed by tail recursion modulo cons (ADR 0039), whose context would be an allocation.
A violation is E0901, at the expression that allocates. louise check, run, build and test report it; since it needs Perceus, the CLI runs Perceus on a copy of the program, only when some function is marked. The language server does not report it (it doesn't lower to Core).
Consequences
- In-place updates that v1 missed: record updates, tuples, single-constructor values, and cells a function hands to the function it calls.
- A sized reuse has no size check.
louise_perceus::verifyis unchanged: the new reuses are ordinary tokens.- Code size: each
g$reuse{n}copy is a copy ofg's body; a copy is made only when a caller givesga token. - The interpreter doesn't run Perceus:
@fipis a promise about native code. The check still runs whichever backend is used. - Not covered: reuse specialisation proper (below), a token across a branch before the call (only the straight path is searched), reuse between different sizes (Koka's "reuse pairing" of smaller cells), and
@fipfor functions generic over a trait (their dictionary calls are function values).
Benchmarks
scripts/bench.sh binaries, main (cd65d12) against this branch, interleaved, best of 9, one scheduler thread. Wall clock in ms; user-space cycles (pinned to one performance core, best of 7) for the ones that move.
| bench | main | this | wall | cycles |
|---|---|---|---|---|
| arith | 31 | 30 | −3.2% | |
| dict | 203 | 196 | −3.4% | −2.4% |
| effects | 51 | 53 | +3.9% | +0.3% |
| html | 397 | 386 | −2.8% | −3.4% |
| lists | 171 | 172 | +0.6% | +1.2% |
| timers | 283 | 290 | +2.5% | +1.6% |
| others | within ±3% |
Nothing is more than 5% slower, the bar the owner set. The gains are small: the benchmarks reuse matched cells already, and only lose the size test. The machine (Core Ultra 7 155H, hybrid cores) moves these numbers by several percent with code placement alone: one ThinLTO build of main had a quarter of the branch misses of another build of the same C, in the runtime's drop loop.
Reuse specialisation, measured and left out. The first version of this ADR also had kept fields: a sized reuse would not rewrite a field that receives the value the release moved out of it (a record update writes one field), so a token kept its moved fields instead of clearing them; a header flag (TL_FLAG_TOKEN, runtime ABI 8) told tl_drop_slow to free an unused token without its dead fields. It ran 10% fewer instructions on html and was faster on dict (−15%) and lists (−9%), but html was 6–7% slower in wall clock and cycles: its in-place list.reverse loop got slower for reasons of code generation and placement we could not pin down (a variant identical to v1 except for the absent tl_reuse_token call was just as slow). That breaks the 5% bar, so it is not part of this decision; it is worth another look with a steadier way to measure (fixed code alignment, or counting on a machine without hybrid cores).
Alternatives considered
- Kept fields with
TL_FLAG_TOKEN: measured, and left out for now (Benchmarks). - Keep the run-time size check: it costs a load and a test on every reuse, for a size Core now knows.
- Pass tokens across calls by changing the callee itself (no copy): every caller would pay for an extra parameter; a copy costs code size only where it is used.
@fipchecked on HIR or typed code, as Koka does: the checker would have to predict Perceus' choices; checking its result can't disagree with it.@fipthat also forbids freeing (Koka'sfipvsfbip): needs types to tell a freed cell from a reused one; possible once Core is typed.
Revision 1 — 2026-10-15: the kept-fields reuse is not re-landed yet
Status: Accepted by the project owner on 2026-10-15; needs engineering review (#44). Issue #193.
The kept fields + TL_FLAG_TOKEN specialisation (measured gains: dict −15%, lists −9%) is not re-landed: its html cost (+6–7%) was traced to build-to-build code-placement variance in ThinLTO, and on the assessment machine this week the wall clock of an identical binary swung ±16% (load average 5–9) — the same instability it broke on. So the order is the issue's own: first fixed code alignment and pinned-core (or non-hybrid) measurement so identical builds agree to ~2%, then PGO/BOLT for the runtime's hot paths, then re-land on the stable layout and re-measure. The optimiser epic (#195–#200) already took most of the win the kept fields were chasing (dict −41%, lists −28% on instruction counts), so the marginal case for re-landing is narrower than it was, and the decision should be driven by the stabilised numbers. The one measurement that was already trustworthy — user-space instruction counts (perf stat, a few parts in 10^8 here) — is what the comparison's table now reports.
Revision 2 — 2026-10-15: the kept-fields reuse is re-landed
Status: Proposed; needs engineering review (#44). Issue #193.
On the stabilised measurement (Revision 1's order: fixed alignment, pinned cores, deterministic instruction counts), the specialisation is re-implemented and landed: lists −10.2% and html −2.8% in instructions, dict +2.7% instructions with wall clock unchanged (110 ms both ways; the helper's mask loop costs a little where the win is small), everything else within ±0.3%; wall clock on the movers within ±2%, under the owner's 5% bar. The earlier html cost (+6–7%) does not reproduce on the stable layout — it was the placement variance, as suspected. The kept set is a union over the token's paths, sized reuses only, capped at field 127 (the token helper carries two words); a field kept only where its slot receives the release's own binding back. The interpreter and the count balance (verify) are unchanged; the differential tests and the ASan spike pass with the leak oracle at 0.
Differences from the Revision-1 sketch: the token helper is a generated-prelude inline (tl_reuse_token_kept, the set as two mask words), not a runtime helper; the flag is the only runtime change (runtime ABI 8). LOUISE_NO_KEPT_REUSE turns it off.