| @@ -0,0 +1,28 @@ |
| 1 | +# ADR 0045: Unbox Float (and U64) |
| 2 | + |
| 3 | +**Status:** Proposed (2026-10-15, issue #190); needs engineering review (#44). It changes the value representation (runtime ABI 9), so it changes what the runtime's `tl_value` may be — the type checker is untouched. |
| 4 | + |
| 5 | +## Context |
| 6 | +`Float` values are boxed (ADR 0013): every arithmetic result is a heap block, allocated and freed by Perceus. `Int` is immediate-tagged (the low bit); a double is not, because the low bit is taken and a box's header carries the runtime kind. The `floats` benchmark is dominated by that allocation, and the cross-language check put Louise 2–4× behind Rust and GHC on numeric code (with the calling convention the other suspect, ADR 0046). The optimiser epic (#195–#200) deliberately did not touch the representation, and `floats`/`arith` are flat against the pre-optimiser build (±0%): the pipeline passes have nothing to fuse when every `+` allocates. |
| 7 | + |
| 8 | +So the question is how a `Float` lives in a `tl_value` without a box, and how the existing code keeps working. The done-when of #190 is `floats` and a float-heavy kernel allocating nothing in their hot loops. |
| 9 | + |
| 10 | +## Decision |
| 11 | +**Worker/wrapper unboxing** (GHC's approach), riding the machinery ADR 0043's constructed-product-results just proved (#210): |
| 12 | + |
| 13 | +- A function whose parameters and results are known to be `Float` (from the types) gets a **worker** that takes and returns raw `double`s, and keeps a wrapper for the uniform path (closures, polymorphic callers). The C backend emits the worker with `double` parameters and return — a second value shape — so a float kernel's loop does no allocation. |
| 14 | +- The split is the CPR one: the analysis (which functions are float-monomorphic at their interface) and the caller rewrite (an unboxing call site) are the same shape as #210's, with `f64` in place of a tuple. The interpreter keeps the boxed semantics. |
| 15 | +- `U64` (ADR 0035) follows the same path (its box is already raw — a 2-word cell; the win is passing it in a register). |
| 16 | + |
| 17 | +The alternative representations were considered: |
| 18 | +- **NaN-boxing** (a double in the pointer bits, ints and pointers tagged within the NaN space): one word, no second shape — but every operation becomes a decode, and the tag layout interacts with the runtime's `tl_is_int` fast path (the hot one). The worker/wrapper keeps the value encoding exactly as it is and pays only at the boundary. |
| 19 | +- **Unboxed fields** in constructors whose field is a `Float` (a raw area, as `U64` uses): composes with worker/wrapper but is a bigger ABI change (a constructor's layout depends on its fields' types). Deferred; the worker/wrapper already removes the hot-loop allocation. |
| 20 | + |
| 21 | +## Consequences |
| 22 | +- `floats`, and a float-heavy kernel (n-body or a matrix product — a benchmark to add), allocate nothing in their hot loops. The 2–4× gap on numeric code should mostly close (with ADR 0046's calling-convention piece). |
| 23 | +- The runtime ABI gains a value shape (a `double` where a `tl_value` was) — runtime ABI 9; the boundary is at the wrapper, so foreign/`@extern` code and the closure/entry paths are unchanged. |
| 24 | +- Perceus's picture is unchanged: an unboxed double is not a reference, so it is neither dupped nor dropped. |
| 25 | + |
| 26 | +## Open for review |
| 27 | +- The exact trigger: known-`Float` from the signature is easy; the interesting question is whether to unbox record *fields* and constructor *fields* too (the bigger ABI change), or to stop at function boundaries for v1. |
| 28 | +- Whether the worker should be keyed on a monomorphic `Float` interface only, or generalised with #210's CPR into one "representation specialisation" pass. |