# ADR 0054: Precompiled renderers for HTML templates **Status:** Proposed (2026-10-19); needs engineering review (#44). ## Context - An HTML literal lowers to `Element` constructor calls and `html.render` walks the tree ([ADR 0009](0009-html-literals.md)); a tagged template lowers to a `Template` record a templater interprets at run time ([ADR 0048](0048-tagged-templates.md)). Nothing about HTML exists after type checking — both ADRs bought their simplicity from that. It also means every render of a page rebuilds the whole tree (a Node, Text, Attribute and list cell per element, attribute and text run) and then walks it, at O(nodes) heap blocks per request. - The static parts are already free: constant constructor trees are static, immortal blocks in the C backend (string literals always were; the trees joined them). What remains is the per-request work: building the parts that depend on holes, and walking everything. - `std/html.render` is `bytes.build_string(render_into(element, bytes.builder()))`: one unique builder, escaping written straight into it. The builder path is good; the tree in front of it is the waste. ## Decision A Core pass (`opt/precompile.rs`, `LOUISE_NO_PRECOMPILE` disables it) compiles "static byte chunks + typed holes" into code that appends the chunks and escapes the holes straight into the response builder, never building the `Element` tree. It is an optimisation, not a semantic feature: the property ADR 0009 bought is kept — Core after this pass still contains no HTML, only builder calls; and every program means what it meant, checked by running the same programs unoptimised (the interpreter and the native backend, issue #219). ### What fires 1. **A render of a locally visible literal.** `html.render(t)` — or, once the simplifier has inlined `render`, `build_string(render_into(t, builder()))` — where `t` is bound in the same function to a tree of `Node`/`Text`/`Raw`/`Fragment`/`Attribute`/`Flag`/`Cons`/`Nil` constructions with literal or hole fields, becomes the append sequence `render_into` would run: static parts (a whole static subtree included) folded to one string constant at compile time, text holes escaped in place (`tl_bytes_append_escaped`), `Raw` holes appended as is, element and list holes rendered by the ordinary `render_into`/`render_all` into the same builder, a `Flag` attribute appended under a switch on its boolean, and an if/match hole (a block) rendered per arm. The tree must be *unobservable*: the variable bound at each construction step is used exactly once, by the next step or the render call. 2. **Component boundaries: translation, not inlining.** When the rendered value — or any element hole — is a direct call `f(args)` and `f`'s body returns a literal tree (possibly behind the switches of a `match`), the pass translates `f`'s body with the call's arguments: the same non-tree bindings in the same order, and the appends where the tree's `return` was. Components stay functions and compose; an opaque callee (its body is not a literal tree, or it is on the translation stack) falls back to tree + `render_into`, into the same builder. Under a `list.map` the row function becomes a generated `f$render` step function (a real function, so `fold` can call it). 3. **`render_all(list.map(xs, f), b)`** — the rows of a table — becomes `list.fold(xs, b, f$render)`: the per-row tree is never built. If `f`'s body does not translate, the step function falls back to building the row and rendering it: the fusion still fires (the list is never consed). Nothing else fires. A tree that is stored, returned, matched on, or shared between two uses is built as today; a user templater call (ADR 0048: `css`\`…\``, `sql`\`…\``) is an ordinary function call and falls back. (std's own `pug`/`html`/`xml` templates were ordinary calls too when this ADR was written; [ADR 0057](0057-compiled-std-templaters.md) made them compiled syntax, so they lower to the trees above and this pass fires on them unchanged.) ### Why the order of effects is preserved The pass *deletes* only constructions (`Node`, `Cons`, …), which allocate and nothing else, and *copies* everything else verbatim. The one thing that moves is a component call's non-tree bindings: they evaluate where the render ran, not where the call sat. That move is governed by the **commute rule**: it fires only when either everything relocated is boring — pure and non-panicking, the same gate fusion uses (ADR 0043, `opt::pure`) — or every binding between the call and the render is. Both disjuncts exclude panics: a crash is observable (a monitor sees `Crashed`, ADR 0004), so a panicking binding may not cross another effect either way. - **Holes evaluate exactly as today.** Lowering emits each hole's expression — its `Show.show` dictionary calls and the `tl_int_show` of an integer attribute included — before the tree's constructions, attributes before children, left to right. The pass keeps every such binding where it is (a component body's are copied in order), so effects happen in the same order, the same number of times. (A hole evaluated for a void element's children, which `render_into` never walks, is still evaluated; only the dead constructions around it go.) - **Rendering itself is pure.** The appends and escapes are the runtime's `owned`, effect-free externs (the same ones `render_into` calls), unobservable until `build_string`; so the append sequence may sit where the render call sat, and a `map`'s applications may interleave with the renders of earlier rows: the only things reordered are pure, and the row function's own effects keep their order. A panic in a hole escapes at the same point with the same message; a builder half-filled by then is local and is discarded by the unwind, as today's half-walked tree is. - **Sharing is unobservable**, as for the constant-tree statics: values are immutable and every equality in the language is structural — there is no pointer-identity primitive — so "the tree was never built" cannot be told from "the tree was built and walked", byte for byte of the output. Byte-identity of the output is by construction where possible: the compile-time folder for static text calls the same `louise_rt_prims::html_escape` the runtime escaper uses, and the void element list is replicated from `std/html.is_void` (pinned by a differential test that renders every void tag). Everything dynamic goes through the same runtime entry points as `render_into`. ### The fallback story Every hole position the pass does not recognise — a parameter, a value from an opaque call, a list not built by `Cons` — is rendered by the ordinary `render_into`/`render_all` into the same builder, so a page degrades gracefully to per-subtree precompilation. With no eligible site at all the pass is a no-op. ## Consequences - A request's allocations stop being O(nodes): for a precompiled page they are the holes' own values (the shown numbers, the concatenated strings) plus the one builder, not three blocks per element. - The optimiser's unoptimised oracle (issue #219) covers the pass: `LOUISE_NO_OPT` skips it, and the same programs must print the same. - Components stay functions: no cross-module inlining is needed, so C units keep their per-module stability (ADR 0029). ## Follow-ups (not in this ADR's implementation) - **Tagged templates.** Resolved by [ADR 0057](0057-compiled-std-templaters.md): std's `pug`, `html` and `xml` are compiled syntax — their templates lower to the same constructor trees as literals, so this pass fires on them unchanged (the "compiled parse" sketched here is what ADR 0057 is). User-defined templaters stay runtime calls, as below. - **The specialised `map` shape.** When the specialiser has rewritten `list.map(xs, f)` before the pass sees it, the producer's name is gone; recognising the specialised copy's loop shape would catch the rows of a table then too. - **Hand-built `Node`s with computed tags.** `html.Node(tag, …)` with a non-literal tag keeps the tree (a runtime `is_void` switch could precompile it; nobody writes this). - **Client target.** A client renderer can reuse the eligibility and order arguments; its `msg` holes are a different chunk type. ## Alternatives considered - **A purity gate like fusion's, on the whole pipeline.** Rejected as unnecessary: the pass never reorders two potentially effectful computations without the commute rule, which asks only about the bindings that actually move (or sit between the two positions) — not about every function in sight. Most components are pure anyway; the rule is what lets an effectful page body precompile when its construction sits next to its render. - **Type-directed lowering at the literal site** (emitting builder calls from `lower/html.rs` directly). Rejected: lowering cannot know whether the tree escapes (a component may be rendered, stored, or matched on); only the render site knows. The pass keeps ADR 0009's "constructors after type checking" story intact. - **Constant-folding `render` of a static subtree only.** A special case of §What fires (1); on its own it leaves every hole-bearing row untouched. **Implementation note — 2026-10-28 (issue #333).** The single-use rule of §What fires (1) holds across a component boundary: a parameter counts as its argument's uses. A component that matches on its `Html` argument and places it as a child in its fallback arm (`_ -> Node("div", marks, [skeleton])`) uses the parameter several times, while the caller's tree has one use, the call; the pass consumed the caller's tree in the fallback and copied the switch on it, which then read a deleted variable. A tree passed to a parameter that the body uses other than exactly once is now never consumed: it is built, and rendered by `render_into`. This showed only across modules, where the simplifier does not inline the component first (ADR 0029). Separately, an element whose walk failed partway (a dynamic tag, a shared attribute list) forgot its appends but not their builder, so its fallback rendered into an unbound variable whenever the forgotten appends began a new chunk (after a hole, say); it now starts from the builder it began with. `tests/run/html_precompile_parameters.lse` covers both, the first with recursive components (never inlined either).