# 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_fsize` 0xFF: the count in `fields[0]`) for arrays, but nothing allocated such blocks. - Externs *borrow* their arguments (ABI Revision 1). A borrowed argument with `rc == 1` may 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 for `set` and `push`. - `Int` was 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 an `Int` would then be either a tagged word or a block, which is an ABI change. ## Decision ### `Array`: an immutable array, updated in place when unique **Semantics.** `Array` (`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` | `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]` and `fields[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 `push` writes 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_alloc` still refuses `TL_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>`, 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 a `dup` first. Borrow inference makes a parameter passed there owned. - So, inside an owned extern, `rc == 1` means 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`'s `set` and `push`). Other externs keep the borrowed convention. `Program::owned` lists the symbols, as `Program::blocking` does for `blocking`. ### `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_BIGINT` block (`0xFF0B`): no scanned fields; raw area `int64 len`, then `|len|` 64-bit limbs of the magnitude, least significant first. The sign of `len` is 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 `Int`s 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 (`switch` on `tl_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_overflow` is gone. Addition, subtraction and multiplication no longer unwind, so generated code drops its yield checks after them. - `show` prints every digit; `string.parse_int` accepts any length. - `hash` of 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 `Int` as 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 `Int` is an `i64` that is always within 63 bits, or an `Rc` 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's `Val::Array`. Tests: `tests/run/array.lse` and `array_panic.lse` on both backends (sending large, moved and small arrays), unit scenarios in `array.rs` and `send.rs`, and `tests/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.rs` and `crates/louise_interp/src/int.rs`, both on `num-bigint` 0.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. Removing `tl_int_overflow` also left `tl_panic_str` with few callers, so LLVM inlined it into `resume`; each nested `resume` of a `ctl` generator then had a larger frame (+50% page faults over a million resumes, +18% on the `effects` benchmark). `tl_panic_str` is now `#[cold]` and `#[inline(never)]`. - **Benchmarks** (`main` against `v1-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.lse` becomes 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. The `arith` benchmark must not get slower (see the implementation notes). - **Arrays of `Float`** hold boxed floats; arrays of `Int` hold tagged words. There are no unboxed numeric arrays. - **C bindings** (`louise-ffi`, `@extern` C code) that read an `Int` with `tl_int_get` must 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 reserved `TL_SCAN_LARGE` layout, which drop and `send` read; 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. - **`map` and `fold` in 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-bigint` is the standard choice; `ibig`/`dashu` are faster on huge numbers, which is not the use case. - **Checked 64-bit `Int` with 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_get` and `tl_array_at`, in `louise_rt.h`, following the Int fast paths. On `arrays`, 44% of cycles were runtime calls for what are a bounds check and a load. - **The slow path:** only `at` out 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.