# ADR 0031: Runtime variants: a checked runtime for tests, a fast one for release **Status:** Proposed (2026-10-12, issues #126, #130, #133; epic #135); needs engineering review (#44). ## Context Every program links one runtime archive, `liblouise_rt.a`. The runtime investigation (#135) found three things that need a second archive: - **LTO was never applied (#126).** The crate was built as both `staticlib` and `rlib`, and Cargo silently ignores the profile's `lto` then, even in the `dist` profile. The archive was 25 MB, and calls between the runtime's 16 codegen units were never inlined. - **The leak oracle costs in every build (#130).** `stats` counts every allocation and free in a thread-local (ADR 0002's note). Compiled out, `floats` ran 20% faster, `arrays` 8%, `dict` and `html` 5%. Tests need the oracle; shipped programs don't. - **Panics unwind for nothing.** Nothing in the runtime catches a Rust panic: a Louise panic unwinds through generated code's yield checks, not Rust's unwinder. `panic = "abort"` drops the landing pads. Each of these changes the archive, and tests must keep the oracle, so the choice is one design rather than three flags. ## Decision **Two runtime archives, chosen per build:** | | `checked` | `fast` | |---|---|---| | File | `liblouise_rt.a` | `liblouise_rt-fast.a` | | Built by | `cargo build -p louise_rt` | `scripts/runtime.sh [DIR]` | | Profile | the build's (`dev`, `release`, `dist`) | `runtime`: fat LTO, one codegen unit, `panic = "abort"` | | Features | `stats`, `mimalloc` | `mimalloc` | | Leak oracle | yes (`LOUISE_LEAKS=1` prints the count) | no (prints `unknown` and how to get it) | | Linked by | debug builds, `run`, `test --native`; any build with `LOUISE_RUNTIME=checked` | release builds; any build with `LOUISE_RUNTIME=fast` | - **The choice** (`louise_build::runtime_archive`): `CcOptions.runtime` if set, else `$LOUISE_RUNTIME`, else by profile. A release build without `liblouise_rt-fast.a` links `liblouise_rt.a`, with a one-line warning; an archive asked for by name must exist. Any other value of `LOUISE_RUNTIME` is an error. - **The crate is only a `staticlib`.** The `rlib` was for nothing: unit tests build their own harness. - **Where the archives live:** both next to `louise` (`target//`), or in `LOUISE_LIB_DIR`. `scripts/runtime.sh` copies the `fast` archive there only when it changed, since its modification time is part of a skipped build's check (ADR 0025 Revision 1). - **The fingerprint** of a build includes the archive chosen, so building `fast` later rebuilds programs that fell back to `checked`. - **Who needs `checked` explicitly:** release builds that count leaks. `scripts/stress.sh` and the demo's load test set `LOUISE_RUNTIME=checked`. ASan and TSan builds already use their own runtimes (`docs/runtime-abi.md`). - **Rust natives (#133):** a `kind = "cargo"` dependency brings its own Rust standard library, linked with `--allow-multiple-definition` as before (ADR 0005); the `fast` runtime's is built for `panic = "abort"`. The native fixture runs the same with both archives (`native_dependencies_of_every_kind`). Building the native crate and the runtime as one Rust program (one std) is deferred until a package needs it. ### The `effects` regression, and `drive` Built as one LTO unit, the runtime made `effects` 17% slower. Its generator clause, `yield(x) -> x + resume(())`, nests `resume_code`, `drive` and `call_clause` once per pending `resume`: 100,000 deep. With 16 codegen units, the nesting path kept a 32-byte frame; with LTO, `drive`'s whole 176-byte frame (the fiber's resume machinery) sat on it. Each level took 352 bytes of stack instead of 192, which doubled the page faults (4,800 to 8,300) and raised branch and iTLB misses. `drive` is now split: the loop that runs the fiber is an out-of-line `run`, and what nests is only the call of the clause. With `fast`, a level takes 176 bytes and the program 4,400 page faults. With `checked` it takes 208 bytes (16 more than before), which only debug builds pay. PGO (#134) was kept as the last resort for this regression. It wasn't needed. ## Measurements The setup: - **Programs:** every program of `tests/bench`, release builds. - **Sides:** `main` (3f7026e) against this branch with `fast`. The branch also contains the stack's first PR (#170). - **Metric:** user cycles and instructions on one scheduler thread, pinned to CPUs 0–11, best of 9 runs alternated. | Benchmark | Cycles | Instructions | |---|---|---| | `arith` | −0.7% | −0.2% | | `arrays` | −10.1% | −10.9% | | `dict` | −6.8% | −3.7% | | `effects` | +2.7% | −1.1% | | `fanin` | −13.5% | −23.1% | | `floats` | −15.8% | −14.7% | | `html` | −14.5% | −13.5% | | `idle` | −13.5% | −18.5% | | `lists` | −4.3% | −4.5% | | `messages` | −21.3% | −22.8% | | `parallel` | +1.0% | −0.1% | | `records` | −6.4% | −18.5% | | `selective` | −16.7% | −26.9% | | `timers` | −21.6% | −24.9% | - **The bar** (a net win, no benchmark more than 5% slower in cycles) is met: the worst are `effects`, +2.7%, and `parallel`, +1.0%. - **On 22 threads** (best of 5–9): `messages` −19%, `records` −10%, `idle` −9%, `selective` −8%, `timers` −6%, `fanin` −4%, `parallel` +1%. Cycles on many threads include idle workers waiting: a first run of `timers` read +17% with 19% fewer instructions, and its wall time was the same on both sides (0.20–0.24 s). - **Sizes:** | | `main` | `fast` | |---|---|---| | runtime archive | 25.0 MB | 9.6 MB | | hello, release | 2.48 MB | 0.75 MB | | `http_demo`, release | 2.64 MB | 1.07 MB | Most of the drop in hello's and the demo's size is PR #170's `--gc-sections` (hello: 0.98 MB with the `checked` archive). - **Builds:** `scripts/runtime.sh` takes about 10 s (one codegen unit, fat LTO), once per runtime change. A cold release build of `http_demo` took 1.75 s, against 1.80 s on `main`. ## Consequences - **A release program can't count its leaks** unless built with `LOUISE_RUNTIME=checked`. `LOUISE_LEAKS=1` says so rather than printing a number. - **Two archives to keep building.** `scripts/lib.sh` (`build_release_louise`) and `scripts/ci.sh` build `fast`. A plain `cargo build` doesn't, so a release build falls back to `checked` with a warning. - **Tests exercise `checked` mostly.** `fast` is covered by `louise_runtime_picks_the_runtime_archive`, the native fixture, and the benchmarks; it differs only by the profile and the `stats` feature. - **A third archive for cross-language LTO** (#127, ADR 0032) fits the same choice. ## Note — 2026-10-13: a smaller std (ADR 0042) `scripts/runtime.sh` builds the `fast` and `lto` archives with std rebuilt without its `backtrace` feature, and release links strip symbols and fold identical functions: a hello world is 395 KB instead of 817 KB. The `checked` archive is unchanged. ## Alternatives considered - **One archive without the oracle, a separate one for tests only.** Same thing, other names. `checked` stays `liblouise_rt.a` so `cargo build -p louise_rt`, `LOUISE_LIB_DIR` and the sanitizer builds work as before. - **The oracle behind a runtime switch.** It would keep a branch, and the thread-local's address, on every allocation and free; not measured. - **`lto = "thin"` or more codegen units for `fast`.** Not measured. Fat LTO in one unit is what #126 measured, and its one regression (`effects`) had a code fix. - **PGO (#134).** Not needed for the bar. It would need a profile per platform and a training run in the build.