louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0014: Arrays, owned extern parameters, and bigint promotion
Status: Accepted by the project owner on 2026-10-12; proposed 2026-10-09 (issue #47); needs engineering review (#44). It changes the runtime ABI: a new block kind (arrays), owned extern parameters, and a boxed Int (TL_ABI_VERSION 2).
Context
- Programs had only linked lists. Indexing was O(n), and nothing gave O(1) random access or a compact block of values. The ABI reserved
TL_SCAN_LARGE(scan_fsize0xFF: the count infields[0]) for arrays, but nothing allocated such blocks. - Externs borrow their arguments (ABI Revision 1). A borrowed argument with
rc == 1may still be used by the caller after the call, so the runtime cannot update it in place: ADR 0012 noted that in-place builder appends need an extern parameter that Perceus passes owned. Arrays need the same forsetandpush. Intwas exactly the 63-bit range, and overflow panicked (tl_int_overflow). The roadmap (issue #47) asks for promotion to a big integer instead. Every value that can be anIntwould then be either a tagged word or a block, which is an ABI change.
Decision
Array<a>: an immutable array, updated in place when unique
Semantics. Array<a> (std/array) is an immutable value, like List. Updating an array gives a new array; the old one is unchanged. When the array being updated is unique (its count is 1, and the caller passes its last reference), the runtime updates it in place instead of copying: Perceus FBIP, applied by the runtime.
API (std/array):
| Function | Behaviour |
|---|---|
empty, from_list, to_list, length | |
get(a, i) -> Option<a> | None out of range |
set(a, i, x) | panics out of range (array.set: index 5 out of range for length 3); in place when unique |
push(a, x) | appends; in place when unique and there is spare capacity, else grows (doubling) |
slice(a, start, end) | both bounds clamped to 0..length; empty when end <= start (as bytes.slice) |
map(a, f), fold(a, init, f) | in Louise, over length and an unchecked read; effect-polymorphic like list.map |
The type gets Eq, Ord (lexicographic), Show and Debug ([1, 2, 3], like a list) and Hash, so types containing arrays can derive them. These are written in Louise in std/array, the type's module (ADR 0007's coherence rule).
Representation. A TL_TAG_ARRAY block (0xFF0A) with scan_fsize = TL_SCAN_LARGE:
[ header | tl_int(n) | x1 ... xn | int64 spare | spare unused slots ]
fields[0] scanned raw area
fields[0]andfields[1..=n]follow the reserved layout, so drop,send's walk and the size computation handle arrays without special cases.- The raw area is the spare capacity: a count, then that many unused slots (zeroed). Its size is
8 * (1 + spare), so the block still describes itself. - An in-place
pushwrites the new element over the spare count and writes the decreased count one slot further: the block's size does not change. When there is no spare slot, a unique array is reallocated with twice the capacity (at least 4). - Arrays are allocated by the runtime only;
tl_allocstill refusesTL_SCAN_LARGE.
Sending follows ADR 0002 Revision 1 unchanged: a unique array is moved; one the sender keeps is copied, or promoted to shared when it is larger than 4 KiB, like strings and Bytes. A shared array has a negative count, so it is never updated in place: an update copies it.
Interpreter. An array is an Rc<Vec<Val>>, updated with Rc::make_mut: in place when the interpreter holds the only reference, copied otherwise. The interpreter keeps references in its environments, so it copies more often than native code does; that changes the cost, not the result.
Owned extern parameters
@extern(c, "sym", owned)declares an extern whose arguments are all owned by the callee: it consumes each of them (stores, returns or drops it). Perceus treats its arguments as owned positions, like a closure call's: the last use of a variable is moved into the call, and any other use takes adupfirst. Borrow inference makes a parameter passed there owned.- So, inside an owned extern,
rc == 1means the caller has no other reference: the runtime may update the block in place and return it. - Only runtime functions use it now (
std/array'ssetandpush). Other externs keep the borrowed convention.Program::ownedlists the symbols, asProgram::blockingdoes forblocking.
Int promotes to a big integer on overflow
Semantics. Int is an arbitrary-precision integer. Arithmetic never overflows; division and remainder still truncate toward zero, and dividing by zero still panics.
Representation: small ints stay tagged.
- A value in the 63-bit range is always a tagged small int, as before. A value outside it is a
TL_TAG_BIGINTblock (0xFF0B): no scanned fields; raw areaint64 len, then|len|64-bit limbs of the magnitude, least significant first. The sign oflenis the sign of the number. The top limb is not zero. - Canonical form: a result that fits in 63 bits is always returned small. So two
Ints are equal exactly when their words are equal or both are blocks with equal limbs, a small int never equals a big one, and matching on an integer literal (switchontl_int(n)) is unchanged.
Fast paths stay inline. In louise_rt.h, tl_int_add, tl_int_sub and tl_int_mul test that both arguments are small and use the overflow builtins, as before. Anything else (a block argument, or an overflow) calls the runtime's slow path (tl_int_add_slow, ...), which computes with bigints and returns a canonical result. The extra cost is one and and one branch per operation. Division, remainder, == and compare get inline small-int paths too (they were runtime calls): with them, no benchmark got slower. show and hash stay runtime functions.
Implementation. The slow paths use the num-bigint crate (rust-num; pure Rust, widely used). It converts the limbs to a BigInt, computes, and writes a new block. Multiplication, division and decimal conversion are where hand-written bigints go wrong, and the slow path is not hot. The interpreter uses the same crate, so both backends compute, print and parse identically.
Consequences in the runtime and the interpreter:
tl_int_overflowis gone. Addition, subtraction and multiplication no longer unwind, so generated code drops its yield checks after them.showprints every digit;string.parse_intaccepts any length.hashof a big integer mixes its sign and limbs (FNV-1a over the limbs, then the usual mixer); both backends use the same formula.- Runtime functions that use an
Intas an index, a count, a size or a duration (bytes.get,array.slice,process.sleep,net.read, ...) treat a big integer as the nearest 64-bit integer. An index that large is out of range like any other. - The interpreter's
Intis ani64that is always within 63 bits, or anRc<BigInt>outside them. - Integer literals are still limited to 63 bits (E0110 is unchanged). Larger constants are computed, or parsed with
string.parse_int.
ABI version. The block kind, the inline fast paths and the removal of tl_int_overflow make this ABI version 2. Generated main calls tl_abi_check(TL_ABI_VERSION) first, and the runtime stops when it was built for another version, as tl_root_cap already does for the number of proc operations. A stale runtime then fails with a message instead of misreading big integers.
Implementation notes — 2026-10-09
Branches:
v1-arrays(arrays, owned externs),v1-bigint(stacked on it).Arrays:
runtime/louise_rt/src/array.rs,std/array.lse; the interpreter'sVal::Array. Tests:tests/run/array.lseandarray_panic.lseon both backends (sending large, moved and small arrays), unit scenarios inarray.rsandsend.rs, andtests/bench/arrays.lse(a million pushes, a map and a million sets per round), which a CLI test runs under a deadline that only in-place updates meet.Big integers:
runtime/louise_rt/src/int.rsandcrates/louise_interp/src/int.rs, both onnum-bigint0.4. Tests:tests/run/bigint.lse(the 63-bit boundaries, factorial(100), mixed small and big arithmetic, comparisons, hashes, parsing, big indices, sending),panic_division.lse, and the ABI spike.Two performance traps, found by measuring. A first version computed the small case of the slow paths in 128 bits; a 128-bit division is a library call, which made
%in hot loops slower. Removingtl_int_overflowalso lefttl_panic_strwith few callers, so LLVM inlined it intoresume; each nestedresumeof actlgenerator then had a larger frame (+50% page faults over a million resumes, +18% on theeffectsbenchmark).tl_panic_stris now#[cold]and#[inline(never)].Benchmarks (
mainagainstv1-bigint, release, best of 15 runs interleaved on a loaded 22-core machine, ms):benchmark main v1-bigint change arith 61 59 -3% dict 207 195 -5% effects 62 57 -8% html 318 318 0% lists 259 254 -1% messages 405 412 +1% arrays (new) — 280 Differences of a few percent are within the noise of this machine.
Consequences
- Breaking: programs that relied on the overflow panic now compute a big integer.
tests/run/panic_overflow.lsebecomes a test of promotion. - Cost of
Int: one more test per addition and subtraction (both arguments small); multiplication becomes inline, and the yield check after arithmetic goes away. Thearithbenchmark must not get slower (see the implementation notes). - Arrays of
Floathold boxed floats; arrays ofInthold tagged words. There are no unboxed numeric arrays. - C bindings (
louise-ffi,@externC code) that read anIntwithtl_int_getmust handle a block, or document that they take small ints. The FFI crate gets a checked accessor.
Alternatives considered
- An array header field for the capacity (
fields[0]holding both length and capacity). It would change the meaning of the reservedTL_SCAN_LARGElayout, which drop andsendread; the spare count in the raw area keeps that layout as it is. - Arrays as persistent vectors (RRB trees). Cheaper copies of shared arrays, but every access walks a tree, and the unique case, the one FBIP makes common, gets slower. A plain block is simpler and fast when unique.
mapandfoldin the runtime, calling the closure from Rust. Closures can yield (preemption, panics, exit signals), so the Rust frame would have to take part in unwinding. Written in Louise, they are ordinary code.- Making every extern argument owned. Every runtime function would have to drop its arguments, and every caller would lose the borrowed fast path. Owned is opt-in, per extern.
- A hand-written bigint. Fewer dependencies, but division and decimal conversion are subtle, and both backends would need the same code.
num-bigintis the standard choice;ibig/dashuare faster on huge numbers, which is not the use case. - Checked 64-bit
Intwith a panic (status quo), or wrapping arithmetic. The roadmap asks for promotion: overflow should not crash an actor, and silent wrapping is worse. - Boxing every
Int. Simplest, but every arithmetic operation would allocate.
Note — 2026-10-12: inline array reads (issue #128)
Status: Proposed; needs engineering review (#44).
- What is inline:
tl_array_length,tl_array_getandtl_array_at, inlouise_rt.h, following the Int fast paths. Onarrays, 44% of cycles were runtime calls for what are a bounds check and a load. - The slow path: only
atout of range calls the runtime (tl_array_at_slow), to panic with the same message as before. - The ABI: generated code now reads the array layout, so the runtime ABI is version 4.