| @@ -0,0 +1,163 @@ |
| 1 | +# ADR 0054: Precompiled renderers for HTML templates |
| 2 | + |
| 3 | +**Status:** Proposed (2026-10-19); needs engineering review (#44). |
| 4 | + |
| 5 | +## Context |
| 6 | + |
| 7 | +- An HTML literal lowers to `Element` constructor calls and `html.render` |
| 8 | + walks the tree ([ADR 0009](0009-html-literals.md)); a tagged template |
| 9 | + lowers to a `Template` record a templater interprets at run time ([ADR |
| 10 | + 0048](0048-tagged-templates.md)). Nothing about HTML exists after type |
| 11 | + checking — both ADRs bought their simplicity from that. It also means |
| 12 | + every render of a page rebuilds the whole tree (a Node, Text, |
| 13 | + Attribute and list cell per element, attribute and text run) and then |
| 14 | + walks it, at O(nodes) heap blocks per request. |
| 15 | +- The static parts are already free: constant constructor trees are |
| 16 | + static, immortal blocks in the C backend (string literals always were; |
| 17 | + the trees joined them). What remains is the per-request work: building |
| 18 | + the parts that depend on holes, and walking everything. |
| 19 | +- `std/html.render` is `bytes.build_string(render_into(element, |
| 20 | + bytes.builder()))`: one unique builder, escaping written straight into |
| 21 | + it. The builder path is good; the tree in front of it is the waste. |
| 22 | + |
| 23 | +## Decision |
| 24 | + |
| 25 | +A Core pass (`opt/precompile.rs`, `LOUISE_NO_PRECOMPILE` disables it) |
| 26 | +compiles "static byte chunks + typed holes" into code that appends the |
| 27 | +chunks and escapes the holes straight into the response builder, never |
| 28 | +building the `Element` tree. It is an optimisation, not a semantic |
| 29 | +feature: the property ADR 0009 bought is kept — Core after this pass |
| 30 | +still contains no HTML, only builder calls; and every program means what |
| 31 | +it meant, checked by running the same programs unoptimised (the |
| 32 | +interpreter and the native backend, issue #219). |
| 33 | + |
| 34 | +### What fires |
| 35 | + |
| 36 | +1. **A render of a locally visible literal.** `html.render(t)` — or, once |
| 37 | + the simplifier has inlined `render`, `build_string(render_into(t, |
| 38 | + builder()))` — where `t` is bound in the same function to a tree of |
| 39 | + `Node`/`Text`/`Raw`/`Fragment`/`Attribute`/`Flag`/`Cons`/`Nil` |
| 40 | + constructions with literal or hole fields, becomes the append |
| 41 | + sequence `render_into` would run: static parts (a whole static |
| 42 | + subtree included) folded to one string constant at compile time, text |
| 43 | + holes escaped in place (`tl_bytes_append_escaped`), `Raw` holes |
| 44 | + appended as is, element and list holes rendered by the ordinary |
| 45 | + `render_into`/`render_all` into the same builder, a `Flag` attribute |
| 46 | + appended under a switch on its boolean. The tree must be |
| 47 | + *unobservable*: the variable bound at each construction step is used |
| 48 | + exactly once, by the next step or the render call. |
| 49 | +2. **Component boundaries: render variants.** When the rendered value is |
| 50 | + a direct call `f(args)` and `f`'s body returns a literal tree |
| 51 | + (possibly behind the switches of a `match`), the pass generates |
| 52 | + `f$render(builder, args)` — `f`'s body with the same non-tree |
| 53 | + bindings in the same order and the appends where the tree's `return` |
| 54 | + was — and the render site calls it. This is why inlineable small |
| 55 | + components compose without being inlined: a component is a function, |
| 56 | + and its render variant is its composed form. An opaque callee (its |
| 57 | + body is not a literal tree, or it is recursive through itself) falls |
| 58 | + back to tree + `render_into`, into the same builder. |
| 59 | +3. **`render_all(list.map(xs, f), b)`** — the rows of a table — with `f` |
| 60 | + a direct closure of a function with a render variant becomes |
| 61 | + `list.each(xs, fn(x) { f$render(b, x) })`: the per-row tree is never |
| 62 | + built. |
| 63 | + |
| 64 | +Nothing else fires. A tree that is stored, returned, matched on, or |
| 65 | +shared between two uses is built as today; a templater call |
| 66 | +(`pug`\`…\``, `html`\`…\``) is an ordinary function call and falls back |
| 67 | +(its `parts` list is still one static block, per the constant-tree |
| 68 | +work). Precompiling a templater is left out deliberately: `pug`'s parse |
| 69 | +is user code running at run time, and [ADR 0048](0048-tagged-templates.md) |
| 70 | +keeps it there; see §Follow-ups. |
| 71 | + |
| 72 | +### Why the order of effects is preserved |
| 73 | + |
| 74 | +The rule is structural, not a purity gate like fusion's (ADR 0043): the |
| 75 | +pass removes or moves only *constructions* — `Node`, `Cons`, … — which |
| 76 | +allocate and nothing else. Everything that can be observed stays put: |
| 77 | + |
| 78 | +- **Holes evaluate exactly as today.** Lowering emits each hole's |
| 79 | + expression — including its `Show.show` dictionary calls and the |
| 80 | + `tl_int_show` of an integer attribute — before the tree's |
| 81 | + constructions, attributes before children, left to right. The pass |
| 82 | + keeps every such binding where it is and deletes only the |
| 83 | + constructions, so effects happen in the same order, the same number |
| 84 | + of times, at the same program points. (A hole evaluated for a void |
| 85 | + element's children, which `render_into` never walks, is still |
| 86 | + evaluated; only the dead constructions around it go.) |
| 87 | +- **Rendering itself is pure.** The appends and escapes are the |
| 88 | + runtime's `owned`, effect-free externs (the same ones `render_into` |
| 89 | + calls), and their results are unobservable until `build_string`. So |
| 90 | + the append sequence may sit where the render call sat, and a `map`'s |
| 91 | + applications may interleave with the renders of earlier rows: the |
| 92 | + only things reordered are pure. A panic in a hole escapes at the same |
| 93 | + point with the same message (a monitor sees the same `Crashed`, ADR |
| 94 | + 0004); a builder half-filled by then is local and is discarded by the |
| 95 | + unwind, as today's half-walked tree is. |
| 96 | +- **Sharing is unobservable**, as for the constant-tree statics: values |
| 97 | + are immutable and every equality in the language is structural — there |
| 98 | + is no pointer-identity primitive — so "the tree was never built" |
| 99 | + cannot be told from "the tree was built and walked", byte for byte of |
| 100 | + the output. |
| 101 | + |
| 102 | +Byte-identity of the output is by construction where possible: the |
| 103 | +compile-time folder for static text calls the same |
| 104 | +`louise_rt_prims::html_escape` the runtime escaper uses, and the void |
| 105 | +element list is replicated from `std/html.is_void` (pinned by a |
| 106 | +differential test that renders every void tag). Everything dynamic goes |
| 107 | +through the same runtime entry points as `render_into`. |
| 108 | + |
| 109 | +### The fallback story |
| 110 | + |
| 111 | +Every hole position the pass does not recognise — a parameter, a value |
| 112 | +from an opaque call, a list not built by `Cons` — is rendered by the |
| 113 | +ordinary `render_into`/`render_all` into the same builder, so a page |
| 114 | +degrades gracefully to per-subtree precompilation. With no eligible |
| 115 | +site at all the pass is a no-op. |
| 116 | + |
| 117 | +## Consequences |
| 118 | + |
| 119 | +- A request's allocations stop being O(nodes): for a precompiled page |
| 120 | + they are the holes' own values (the shown numbers, the concatenated |
| 121 | + strings) plus the one builder, not three blocks per element. |
| 122 | +- `louise core` output changes for the better at render sites; the pass |
| 123 | + reports its rewrites the way fusion reports its rules. |
| 124 | +- The optimiser's unoptimised oracle (issue #219) covers the pass: |
| 125 | + `LOUISE_NO_OPT` skips it, and the same programs must print the same. |
| 126 | +- Components stay functions: no cross-module inlining is needed, so C |
| 127 | + units keep their per-module stability (ADR 0029). |
| 128 | + |
| 129 | +## Follow-ups (not in this ADR's implementation) |
| 130 | + |
| 131 | +- **Tagged templates.** A `html`\`…\`` call whose `parts` are static and |
| 132 | + whose result flows to `render` could precompile to the same append |
| 133 | + sequence (the parts are known at compile time; the `html` templater |
| 134 | + interleaves them with the holes and does not re-parse). `pug` needs |
| 135 | + compile-time evaluation of user code, which does not exist (ADR 0048); |
| 136 | + a `pug` template could first get a *compiled parse*: the indentation |
| 137 | + tree is a function of the static parts alone, so a templater-specific |
| 138 | + pass could parse at compile time and lower straight to the append |
| 139 | + sequence. Both need their own eligibility rules (the templater must be |
| 140 | + the std one, resolved lexically). |
| 141 | +- **The specialised `map` shape.** When the specialiser has rewritten |
| 142 | + `list.map(xs, f)` before the pass sees it, the producer's name is |
| 143 | + gone; recognising the specialised copy's loop shape would catch the |
| 144 | + rows of a table then too. |
| 145 | +- **Client target.** A client renderer can reuse the eligibility and |
| 146 | + order arguments; its `msg` holes are a different chunk type. |
| 147 | + |
| 148 | +## Alternatives considered |
| 149 | + |
| 150 | +- **A purity gate like fusion's.** Rejected as unnecessary: the pass |
| 151 | + never reorders two potentially effectful computations, so it needs no |
| 152 | + purity analysis at all — only the constructions it deletes must be |
| 153 | + pure, and construction is pure by definition. This is strictly |
| 154 | + stronger than fusion's gate, which must keep `f` and `g` from |
| 155 | + interleaving observably. |
| 156 | +- **Type-directed lowering at the literal site** (emitting builder |
| 157 | + calls from `lower/html.rs` directly). Rejected: lowering cannot know |
| 158 | + whether the tree escapes (a component may be rendered, stored, or |
| 159 | + matched on); only the render site knows. The pass keeps ADR 0009's |
| 160 | + "constructors after type checking" story intact. |
| 161 | +- **Constant-folding `render` of a static subtree only.** A special |
| 162 | + case of §What fires (1); on its own it leaves every hole-bearing row |
| 163 | + untouched. |