Hazel reports · ^report(46) · hazel-jit · 2026-11-07, updated 2026-11-08 19:50 MST · work in progress · beside ^report(45) (its next step 7)
BuckleScript, ReScript and Melange, beside hazel-jit
Cyrus named BuckleScript and ReScript at the meeting of 2026-11-05 as technologies to know about, and perhaps to compare with, if not to use (^ticket(698)). This note compiles two programs with each of them and with hazel-jit, and puts what they make side by side: the JavaScript, how values look in it, and how fast it runs. The first is small, ^report(43)'s Petri net rule; the second is the Livelit Playground deck's largest view, the Witch slide's picture.
What the milliseconds here measure (added 11-08): a view's own work, and a small part of what follows an edit. On the Witch slide, one step of a slider shows in the picture 5.3 to 8.8 s later (317 to 526 frames at 60 fps), compiled or not, in two runs of four edits each way. The picture's view is 3.0 ms of that compiled (0.04%), and 87 ms on the interpreter (1.1%). Nearly all the rest is the program's run in Hazel's worker, 1.2 to 2.6 s in its evaluator, and its answer coming back, 4.4 to 5.4 MB, made in the worker and decoded on the main thread: a small edit and where its time goes. (Matthew, 2026-11-08: the numbers here "must only be measuring a small fraction of the total work, since they are on the order of ms, not sec".)
Outline#
- What each is: ReScript is BuckleScript renamed and grown into its own language; Melange is the fork that kept OCaml; js_of_ocaml, which builds Hazel, works another way.
- How fast: on the rule, Melange and ReScript beat JS written by hand; hazel-jit is about 2.9 times ReScript, and 225 times faster than the interpreter.
- The same rule, compiled: side by side.
- How each lays out values: lists, records, constructors, options, arguments.
- A view: the Witch slide's picture: all five within 15% of one another, 0.17 to 0.19 ms (about 5,200 to 6,000 fps), since most of the work is one builtin; in the Hazel test a frame is 155 ms (6 fps) on the interpreter and 3.4 ms (290 fps) compiled; in the app, in Firefox, a render is 3.0 ms compiled (330 fps) against 87 ms on the interpreter, once a drawing before the program's run stopped giving up on the definition. These are the view's own times: an edit on the slide shows in the picture seconds later, and the view is 0.04% of that (a small edit). The drawings, and one page per compiled output.
^report(45)'s ideas, here: the view's answer is drawn straight from its JS, never made Hazel; no diffs, since a view's answer never comes back.- A small edit: one slider step shows in the picture 5.3 to 8.5 s later, compiled or not; the program's run in the worker and its answer coming back are nearly all of it, the picture's view 0.04% of it.
- Where a small edit's time goes: the evaluator is 1.2 to 2.6 s of it; most of the rest is the worker's answer, 4.4 to 5.4 MB for a 10 KB request, made in the worker and decoded on the main thread, 1.9 to 3.1 s with the page frozen.
- The trip between the worker and the page: confirmed, most of an edit, 70 to 72%: the worker shares the result in 1.4 to 1.6 s, finding almost nothing to share, and encodes it in 1.1 to 1.2 s; the page decodes it in 1.4 to 2.0 s. The evaluator, 18 to 20%.
- What the worker's answer holds: mostly one binding in one probe sample's environment, the slide's
Drawingmodule written out whole, 3.8 MB of 5.4; four cuts, none of them a compiler. - Two cuts, made and measured: a small edit now shows in 1.9 to 2.4 s, 2.2 to 3.2 times sooner; the result 0.42 MB; the evaluator now about half of an edit, and the sharing, at 154 to 255 ms, costs more than it saves. Dropped: 1.3 to 1.7 s an edit, the trip 5 to 8%, the evaluator most of the rest.
- Compiling the run: a speculation: under ideal conditions the slide's program run compiled on the main thread, each livelit's model handed to its compiled view as a JS value, and no trip at all.
- Better numbers, from
^report(45)'s sharing: after such an edit the view could redo only the changed shapes, 5 times less work, if the trip in shares what did not change. - How each lays out the view's values: shapes, vertices, Html, numbers formatted, the walks.
- What holds across both: the compilers differ in calls, matches and layouts, and converge where a builtin does the work.
- What it suggests for hazel-jit: five changes to its output, in the order they would help.
- Could any of them be used: not as hazel-jit's back end; their layouts and optimizations, yes.
- What next.
- Each alternative, for and against: hazel-jit stays the base; ReScript and Melange are the models for its output; js_of_ocaml stays Hazel's own build.
- How this was made.
What each is#
Three compilers from one family, and one from another.
- BuckleScript (2016, Bloomberg) compiled OCaml, and later Reason, to readable JavaScript: OCaml's front end and type checker, then its own back end from OCaml's typed intermediate language, one JS module per OCaml module.
- ReScript is BuckleScript renamed (2020) and grown into a language of its own, with a syntax for JavaScript developers, its own standard library, and, since version 11, functions uncurried by default. Version 12.3.1 here.
- Melange is the fork of BuckleScript's compiler that kept OCaml (and Reason) and joined OCaml's own tools: dune and opam. Its output is close to ReScript's, as the listings below show. Version 5.0.0 here, with OCaml 5.3.
- js_of_ocaml, which builds Hazel itself, is the other family: it compiles OCaml's bytecode, a whole program, to JavaScript, with OCaml's runtime in JS around it. Values keep OCaml's memory layout (every block an array, its tag first), and it optimizes the whole program at once. Version 5.9.1 here.
- hazel-jit (
^report(45)) compiles Hazel's elaborated terms to JS source, in the browser, while the program runs, with a runtime of about 2.5 KB.
How fast#
On ^report(43)'s rule, Melange's and ReScript's JavaScript beat the JS a person wrote: 0.46 and 0.55 µs a call against 0.90. hazel-jit's takes 1.59 µs, about 2.9 times ReScript's and 1.8 times the hand-written; Hazel's interpreter takes 358 µs. Each answers the same 2,000 events (fire transition i mod 4, then the command performed on the marking), and all end on the same marking. js_of_ocaml's 0.71 µs is a best case: it compiled the timing loop and the rule as one program, and inlined the whole rule into the loop.
| Compiler | µs a call, median | the fastest call |
|---|---|---|
| Melange 5.0.0 | 0.46 | 0.35 |
| ReScript 12.3.1 | 0.55 | 0.38 |
| js_of_ocaml 5.9.1 (the rule inlined into the loop) | 0.71 | 0.40 |
JS written by hand (the example's jsHandler) | 0.90 | 0.71 |
| hazel-jit | 1.59 | 0.66 |
Hazel's interpreter (Evaluator.evaluate_open) | 358 |
The hand-written handler is not the fastest because it is written the way people write JS: it copies arrays to walk them backwards ([...t.outputs].reverse()) and calls a closure for each input (every). The compilers' code allocates only what the result holds.
The same rule, compiled#
Melange and ReScript make the JavaScript a careful person would write; hazel-jit's is a direct translation, and js_of_ocaml's is OCaml's memory layout in JS. The rule's two functions, fire_at and on_event, as each compiler wrote them (the whole outputs are in this report's data):
Hazel (the example's rules/Rules.hz)
# Fire(i): fire transition i, if it can. # # Step: fire one of those that can, chosen by the app's draw. # let fire_at = fun (n, i) : (Net, Int) -> case Common.pick(n.transitions, i) | None => Say("there is no transition " ++ string_of_int(i), Done) | Some(t) => if Common.enabled(n.marking, t) then Common.fire(t) else Say(t.name ++ " cannot fire now", Done) end; let on_event = fun (n, e) : (Net, Event) -> case e | Fire(i) => fire_at(n, i) | Step => let r = Common.ready(n.marking, n.transitions, 0) in let k = Common.len(r) in if k == 0 then Say("deadlock: nothing can fire", Done) else let j = int_of_float(n.draw *. float_of_int(k)) in fire_at(n, Common.nth(r, j)) end
ReScript source (data/Petri.res, the rule as the Hazel is written)
let fire_at = (n, i) => switch pick(n.transitions, i) { | None => Say("there is no transition " ++ Int.toString(i), Done) | Some(t) => enabled(n.marking, t) ? fire(t) : Say(t.name ++ " cannot fire now", Done) } let on_event = (n, e) => switch e { | Fire(i) => fire_at(n, i) | Step => let r = ready(n.marking, n.transitions, 0) let k = len(r) if k == 0 { Say("deadlock: nothing can fire", Done) } else { let j = Float.toInt(n.draw *. Int.toFloat(k)) fire_at(n, nth(r, j)) } }
OCaml source (data/petri.ml, for Melange and js_of_ocaml)
let fire_at n i = match pick n.transitions i with | None -> Say ("there is no transition " ^ string_of_int i, Done) | Some t -> if enabled n.marking t then fire t else Say (t.name ^ " cannot fire now", Done) let on_event n e = match e with | Fire i -> fire_at n i | Step -> let r = ready n.marking n.transitions 0 in let k = len r in if k = 0 then Say ("deadlock: nothing can fire", Done) else fire_at n (nth r (int_of_float (n.draw *. float_of_int k)))
hazel-jit (formatted by esbuild; its runtime is $R)
return def$75 = $R.fn((a$76) => { let n$77, i$78, m$79, t$80; return n$77 = a$76[0], i$78 = a$76[1], m$79 = Common$73["pick"]([n$77["transitions"], i$78]), m$79.c === "None" ? { c: "Say", v: ["there is no transition " + $R.b.string_of_int(i$78), { c: "Done" }] } : m$79.c === "Some" ? (t$80 = m$79.v, Common$73["enabled"]([n$77["marking"], t$80]) ? Common$73["fire"](t$80) : { c: "Say", v: [t$80["name"] + " cannot fire now", { c: "Done" }] }) : $R.nomatch("case"); }, 9, [Common$73]), fire_at$81 = def$75, def$82 = $R.fn((a$83) => { let n$84, e$85, m$86, let$87, r$88, let$89, k$90, let$91, j$92, i$93; return n$84 = a$83[0], e$85 = a$83[1], m$86 = e$85, m$86.c === "Fire" ? (i$93 = m$86.v, fire_at$81([n$84, i$93])) : m$86.c === "Step" ? (let$87 = Common$73["ready"]([n$84["marking"], n$84["transitions"], 0]), r$88 = let$87, let$89 = Common$73["len"](r$88), k$90 = let$89, $R.eq(k$90, 0) ? { c: "Say", v: ["deadlock: nothing can fire", { c: "Done" }] } : (let$91 = $R.b.int_of_float(n$84["draw"] * $R.b.float_of_int(k$90)), j$92 = let$91, fire_at$81([n$84, Common$73["nth"]([r$88, j$92])]))) : $R.nomatch("case"); }, 10, [Common$73, fire_at$81])
ReScript 12.3.1
function fire_at(n, i) { let t = pick(n.transitions, i); if (t !== undefined) { if (has(n.marking, t.inputs)) { return fire(t); } else { return { TAG: "Say", _0: t.name + " cannot fire now", _1: "Done" }; } } else { return { TAG: "Say", _0: "there is no transition " + i.toString(), _1: "Done" }; } } function on_event(n, e) { if (typeof e === "object") { return fire_at(n, e._0); } let r = ready(n.marking, n.transitions, 0); let k = len(r); if (k === 0) { return { TAG: "Say", _0: "deadlock: nothing can fire", _1: "Done" }; } let j = n.draw * k | 0; return fire_at(n, nth(r, j)); }
Melange 5.0.0
function fire_at(n, i) { const t = pick(n.transitions, i); if (t !== undefined) { if (has(n.marking, t.inputs)) { return fire(t); } else { return { TAG: /* Say */ 2, _0: t.name + " cannot fire now", _1: /* Done */ 0 }; } } else { return { TAG: /* Say */ 2, _0: "there is no transition " + String(i), _1: /* Done */ 0 }; } } function on_event(n, e) { if (!/* tag */ (typeof e === "number" || typeof e === "string")) { return fire_at(n, e._0); } const r = ready(n.marking, n.transitions, 0); const k = len(r); if (k === 0) { return { TAG: /* Say */ 2, _0: "deadlock: nothing can fire", _1: /* Done */ 0 }; } else { return fire_at(n, nth(r, n.draw * k | 0)); } }
js_of_ocaml 5.9.1 (inlined into the loop; around fire_at's answers)
rest$1 = arcs[2], a$0 = arcs[1], i$14 = /*<<petri.ml:12:63>>*/ a$0[1]; c: { d: { var xs = xs$0, i$10 = i$14, _bN_ = a$0[2]; for(;;){ /*<<petri.ml:9:19>>*/ if(! xs) break; var rest = xs[2], x$0 = xs[1]; /*<<petri.ml:9:56>>*/ if(0 === i$10) break d; var i$11 = /*<<petri.ml:9:77>>*/ i$10 - 1 | 0; xs = rest; i$10 = i$11; } var _bO_ = /*<<petri.ml:9:39>>*/ 0; break c; } var _bO_ = /*<<petri.ml:9:70>>*/ x$0; } var _bP_ = /*<<petri.ml:12:76>>*/ _bN_ <= _bO_ ? 1 : 0; if(! _bP_) break b; arcs = rest$1; } var _bS_ = /*<<petri.ml:12:43>>*/ 1; break a; } var _bS_ = /*<<petri.ml:13:18>>*/ _bP_; } /*<<petri.ml:22:36>>*/ if(_bS_) var _bQ_ = /*<<petri.ml:18:41>>*/ puts(t$1[3], 0), _bR_ = /*<<petri.ml:18:26>>*/ takes(t$1[2], _bQ_), cmd = /*<<petri.ml:18:62>>*/ [2, t$1[1], _bR_]; else var s1 = /*<<petri.ml:22:54>>*/ t$1[1], cmd = /*<<petri.ml:22:86>>*/ [2, s1 + " cannot fire now", 0]; } else var s2 = /*<<stdlib.ml:266:2>>*/ "" + i$16, cmd = /*<<petri.ml:21:60>>*/ [2, "there is no transition " + s2, 0];
By hand (the example's jsHandler)
function jsHandler(net, ev) { const t = net.transitions[ev.tag === "Fire" ? ev.value : 0]; const ok = t && t.inputs.every(a => net.marking[a.place] >= a.weight); if (!ok) return { tag: "Say", value: ["(js) cannot fire", { tag: "Done" }] }; let k = { tag: "Done" }; for (const a of [...t.outputs].reverse()) k = { tag: "Put", value: [a.place, a.weight, k] }; for (const a of [...t.inputs].reverse()) k = { tag: "Take", value: [a.place, a.weight, k] }; return { tag: "Say", value: ["(js) " + t.name, k] }; }
How each lays out values#
They differ most in constructors and in how a function takes several arguments.
| Value | hazel-jit | ReScript | Melange | js_of_ocaml |
|---|---|---|---|---|
| a list cell; the empty list | {h, t}; null | {hd, tl}; 0 | {hd, tl}; 0 | [0, hd, tl]; 0 |
| a record, or labeled tuple | an array, each label also a property | an object by field name | an object by field name | an array, [0, f1, f2, …] |
Take(a, b, k) | {c: "Take", v: [a, b, k]} | {TAG: "Take", _0, _1, _2} | {TAG: 0, _0, _1, _2} | [0, a, b, k] |
Done (no argument) | {c: "Done"} | "Done" | 0 | 0 |
Some(t); None | {c: "Some", v: t}; {c: "None"} | t; undefined | t; undefined | [0, t]; 0 |
| a function of two | one argument, an array [a, b] | f(a, b) | f(a, b) | f(a, b) |
i == 0 on Ints | $R.eq(i, 0) | i === 0 | i === 0 | 0 === i |
| a tail call | a call | a while loop | a while loop | a for loop |
A view: the Witch slide's picture#
On the deck's largest view, the five versions run within 15% of one another: 0.17 to 0.19 ms (about 5,200 to 6,000 fps, as if updating the state were free), hand-written JS the fastest and hazel-jit the slowest of the five, 9% behind ReScript. Most of the work is one builtin: the picture's 1,985 numbers formatted with toFixed take 0.14 ms alone. The exception is js_of_ocaml through OCaml's Printf: 1.01 ms (990 fps), 5.3 times its time when it calls toFixed instead. All five draw the same SVG, byte for byte (26,044 characters), and it is the interpreter's drawing.
These are milliseconds of the view alone. After an edit on the slide, the picture shows it 5.3 to 8.8 s later, nearly all of it the program's run in Hazel's worker and its answer coming back (a small edit, where its time goes): how fast the view runs, in any of the five, changes little a person would see after an edit.
The Livelit Playground deck's Witch slide draws Messed Up Dress Up's witch, 133 shapes and 924 points in all. Its ^picture livelit shows a picture it is given: the view fits a box around every point, then writes one SVG polygon or polyline per shape, each coordinate formatted to one decimal. Here that view is written again in ReScript and in OCaml (for Melange and js_of_ocaml) as the Hazel is written, and once by hand in JS. Each version is timed on the same picture in one Node process (js_of_ocaml's in its own), from the picture as that language holds it to the Html the view returns.
toFixed(1) and nothing else.| Version | ms, the view's work (median) | fps, if updating the state were free | the fastest call, ms |
|---|---|---|---|
| JS written by hand | 0.17 | 6,000 | 0.16 |
| Melange 5.0.0 | 0.17 | 5,800 | 0.17 |
| ReScript 12.3.1 | 0.18 | 5,600 | 0.17 |
js_of_ocaml 5.9.1, numbers by JS's toFixed | 0.19 | 5,300 | 0.18 |
| hazel-jit | 0.19 | 5,200 | 0.19 |
js_of_ocaml 5.9.1, numbers by OCaml's Printf.sprintf "%.1f" | 1.01 | 990 | 0.91 |
| Hazel's interpreter (in the app's terms, below) | 155 | 6 |
In the Hazel test: one frame#
Corrected 11-07, 13:28: this section first said "in the app's terms". It is the Hazel test's process, with the view handed the picture already evaluated. In the app, in Firefox, the picture's view was not compiled until 15:05 (cb560ebb86); now a render takes 3.0 ms there (below).
On the interpreter, a frame of the Witch's picture takes 155 ms (6 fps), about 9 frames' time at 60 fps. Compiled, it takes 3.4 ms (290 fps), 45 times less, leaving 13 ms of a 60 fps frame's 16.7 ms. These are timed in the Hazel test's process, the way the app runs a view. On the interpreter: the view evaluated (13.3 ms), then its command run, which evaluates the splice's value and is most of the time, then its Html drawn as virtual DOM. Compiled: the model carried into JS (0.88 ms), the view and its commands run in JS (1.16 ms), and the Html drawn straight from JS (0.64 ms).
That process holds all of Hazel, and there the same compiled view takes 6 times as long as in the benchmark's own process: 1.16 ms against 0.19 ms. The difference is not the code; it is likely the process's large heap and its garbage collection.
Test_HazelJit's witch_view_run, a dev build, Node; two runs of 15, pooled medians), the last in the benchmark's own process. Shaded: inside one frame at 60 fps. The frames a second are each time alone, as if updating the state were free.In Firefox, in the app#
In the app, in Firefox, the Witch's picture now runs compiled: 3.0 ms a render (about 330 fps), against 87 ms (11 fps) on the interpreter, 29 times faster, leaving 14 ms of a 60 fps frame. Until cb560ebb86 it did not: every render took 90 ms on the interpreter, because a drawing before the program's run had given up on the view's definition for good. The slide's four polygon editors run compiled too: 0.29 to 0.45 ms (about 2,200 to 3,400 fps), against 26 to 29 ms on the interpreter. A render is the view's work only: after an edit, the program's run comes first, and the picture shows the edit 5.3 to 8.8 s later (below).
Corrected 11-07, 15:05: this section said, at 13:28, that the picture's model reaches the view holding the splice's code. That is true only of the drawings before the program's run. Found with a diagnostic on GitHub's Firefox (branch diag/694-witch-splice): after the run, the model holds the picture itself (the picture's 133 shapes, no splice; corrected 20:30: this said 203 polygons, which was how often the word Polygon appears in the model's printed text), and the compiled path still refused it, with the old reason.
What was wrong. The slide draws the picture first from its model as written, before the program's run: the splice's code, skinned(dressed(outfit)), whose variables have no value there, so it cannot go into JS. The app treated that refusal as the definition's fault, and gave up on compiling the definition at all; after the run, with the picture's value in the model, the view never tried compiled again. Now a model that cannot go into JS is that model's fault: the definition stays compiled, and the next model goes in (HazelJitValue.call_view_why, a quick test beside it).
How it was timed. In the page itself: each livelit view the app draws leaves a way to run it again on its last model, without the app's memo of the last answer, both ways (globalThis.hazelJit.bench, ^ticket(694)), and the Firefox check (scripts/jit_slides.py --bench) loads the Witch slide fresh and times every polygon and picture view on it, each sample a loop of calls at least 25 ms long, since Firefox's clock there ticks in whole milliseconds. The compiled render, model in, view and drawing, is 3.0 ms; the view alone, 1.6 ms. Both are near the Hazel test's (3.4 and 1.16 ms).
^report(45)'s ideas, here#
The Witch's compiled numbers draw the view's answer straight from its JS and never make it Hazel, as ^report(45) does. They use no diffs: a view's answer never comes back to Hazel, so there is nothing for a diff to carry. Matthew asked, on 2026-11-07 (this section added 20:30): are we using diffs, and are we skipping the trip of the Html back to Hazel?
- The Html's trip back to Hazel: skipped, in every compiled time here. The view's answer is drawn straight from its JS (
HazelJitDOM;^report(45)'s a view's answer, never made Hazel,^ticket(714)). It is made Hazel only if something asks for it (the lazyhtmlofLivelitProj.eval_view_answer), and on this slide nothing does. The commands the view runs are answered in JS too. Until bcec455abc, the splice's value went back to Hazel and in again, and that was 20 of the 21 ms of the picture's drawing from JS in the Hazel test.^report(45)'s cheaper ids are in as well, though a drawing from JS makes none. - Diffs: not used, and a render has nothing for one to carry.
^report(45)'s diff carries a new value back to Hazel against the last one (the way back as a diff,^ticket(715)): in the app, the model a livelit's update returns. The picture's update returns its model unchanged (Pure(m)), and its model changes only when the program's run gives the splice a new value. The slide's sliders and polygon editors do run their updates compiled, their models back as diffs, but this report times views. What a render does carry whole is the other way: the picture into JS, 0.88 ms of the 3.4 ms frame in the Hazel test, each time the model changes. A diff that way is below.
A small edit#
One step of a slider on the Witch slide shows in the picture 5.3 to 8.5 s later, compiled or not: 8.0 s with Compiled views on and 7.0 s off (medians of four), about 478 frames at 60 fps. Nearly all of it is the program's run in Hazel's worker and its answer coming back (broken down below). The picture's view is 3.0 ms of it compiled (0.04%) and 87 ms on the interpreter (1.1%), so on this slide compiling the view changes nothing a person would see after an edit. (Matthew, 2026-11-07: "we should measure the cost of a small edit".)
Corrected 11-07, 21:20: this said, at 20:30, that nearly all of it is the program's run on the interpreter. Broken down (below), the evaluator is 1.2 to 2.6 s of it; most of the rest is the worker's answer, 4.4 to 5.4 MB, made in the worker and decoded on the main thread.
- The edits. The neck slider, two steps (28 to 30, and back), moves the head and what sits on it: 17 of the picture's 133 shapes change. The arms slider, ten steps (55 to 65, and back): 30 shapes change. Each edit is the browser's input and change events on the slider, as a click on it makes them, on the slide as loaded, with Compiled views on and then off.
- Before anything is drawn: 0.31 to 0.41 s. The page's first change after the edit (the slider's new number, and the status saying a run has started) came 0.31 to 0.41 s after it, 19 to 24 frames at 60 fps. That is the main thread's own work on the edit: the program's text changed, and whatever follows before the run is sent off. It is not broken down yet. In a real drag the slider follows the pointer before this, since a drag's input events only preview; this work comes when the change commits.
- The run. The program then runs off the main thread, in Hazel's worker, and the picture is drawn when the run's value arrives. The status said the run went on until 5.8 to 11.9 s after the edit; on the arms edits, the picture changed before that.
- Compiled or not, the same. The four edits' times overlap both ways (5.3 to 8.5 s on, 5.8 to 7.6 s off). The 84 ms the compiled view saves is well within the run's own variation.
- Drawn again, unchanged. With Compiled views on, each edit drew livelits from their JS 218 to 266 times, and called a compiled view 3 times. The rest were answers the app had kept (its memo of the last answer), drawn again on each render of their livelit.
So the better numbers for a small edit are in the program's run and what it sends back, not in the view. ^report(45) and this report compile livelits' views and updates; the program itself still runs on the interpreter. What compiling did for the picture's view in Firefox (29 times faster) hints at what compiling the run might do, but that is not measured.
Where a small edit's time goes#
The program's evaluation is 1.2 to 2.6 s of a small edit's 5.3 to 8.8 s. Most of the rest is the worker's answer: 4.4 to 5.4 MB for a 10 KB request, about 536 times its size. Beyond its evaluator, the worker takes 2.5 to 4.7 s before its result arrives. The main thread then decodes the result in 1.9 to 3.1 s, one long task during which the page cannot draw: at least 116 frames at 60 fps. (Matthew, 2026-11-07: "Yes, break it down".)
Util.BenchMarks). Main thread, blue: the page's own work (each update, calculate and view); orange: decoding a message from the worker. Worker, grey: from the request to its result; when within that its evaluator ran is not recorded, only its total.- How it was timed. The page now keeps its own timeline when asked:
Util.BenchMarks, on whenglobalThis.hazelBenchMarksis an array, and otherwise doing nothing. It records each update and calculate (with the statics inside it), the page's view, the work after display, the request posted to the worker with its size, and each message back, decoded and then handled. A result also brings the worker's own evaluator time. The check ran the same four edits each way with it on (3ed2f31844, run 42445268287); this time the picture showed the edit after 5.3 to 8.8 s. - Before the request: 0.18 to 0.27 s. The edit's update, one view of the page, and a calculate (the statics, and the request's pieces) run on the main thread; then the request goes, 10 KB. The page's first redraw comes after one more view.
- The worker. On the neck edits, the picture comes with the run's result, 4.1 to 5.9 s after the request. The worker's evaluator ran 1.2 to 2.6 s of each run in all, so 2.5 to 4.7 s of the worker's time before its result went to something else. Making the result and encoding it is the likely part, but the worker's own split is not recorded yet. (Recorded 11-08, below: making the result takes under a millisecond; sharing it and encoding it are the time.) On the arms edits, the picture comes sooner, in a 4.4 MB stream message the worker sends while it runs, and the result follows seconds later.
- Decoding on the main thread. A 4.4 MB stream took 1.3 to 1.5 s to decode, and a 5.4 MB result 1.9 to 3.1 s. Each is one task: while it runs, nothing on the page moves. On the arms edits that happens twice, the second time after the picture has already changed.
- The page's view. Each time the page draws, its view takes 24 to 506 ms (median 117), and an edit draws it five times before the picture shows: 0.5 to 0.9 s in all. That is the whole slide's view, its code and its livelits; the picture's own view is 3.0 ms of it.
- In all, the main thread was busy 2.1 to 3.6 s of the 5.3 to 8.8 s, most of it decoding; the rest of the time it waited for the worker.
| the edit | compiled views | its request posted, s | the worker, from the request to the message that carries the picture, s | of which its evaluator (the whole run), s | that message | decoding it on the main thread, s | then the page, to the picture, s | the picture shows the edit, s | the main thread busy until then, s |
|---|---|---|---|---|---|---|---|---|---|
| neck 28 to 30 | on | 0.20 | 5.9 | 1.2 | the result, 5.4 MB | 2.4 | 0.29 | 8.8 | 3.4 |
| neck 30 to 28 | on | 0.27 | 4.7 | 1.7 | the result, 5.4 MB | 2.1 | 0.23 | 7.4 | 3.2 |
| arms (up / down) 55 to 65 | on | 0.18 | 3.6 | 1.6 | a stream, 4.4 MB | 1.3 | 0.19 | 5.3 | 2.1 |
| arms (up / down) 65 to 55 | on | 0.22 | 4.4 | 2.6 | a stream, 4.4 MB | 1.5 | 0.23 | 6.3 | 2.5 |
| neck 28 to 30 | off | 0.21 | 4.7 | 1.6 | the result, 5.4 MB | 2.4 | 0.56 | 7.9 | 3.6 |
| neck 30 to 28 | off | 0.22 | 4.1 | 1.6 | the result, 5.4 MB | 2.2 | 0.42 | 6.9 | 3.3 |
| arms (up / down) 55 to 65 | off | 0.18 | 3.5 | 1.6 | a stream, 4.4 MB | 1.4 | 0.41 | 5.5 | 2.4 |
| arms (up / down) 65 to 55 | off | 0.23 | 3.8 | 2.5 | a stream, 4.4 MB | 1.4 | 0.41 | 5.9 | 2.5 |
So the next numbers to improve are the size of the worker's answer and the page's view, not the evaluator alone. Why the answer is so large is not known yet. It carries the run's value and its state (each probe's and livelit's samples, the picture among them), pruned for shipping; but at 5.4 MB, something in it is far larger than the picture's 133 shapes. The next step is to record the worker's own split (evaluating, making the result, encoding it) and what the result holds.
The trip between the worker and the page#
Confirmed: the run's answer, sent from Hazel's worker to the page, is most of a small edit's time, 70 to 72% of it; the evaluator is 18 to 20%. On the neck edits, whose picture comes with the run's result, the picture shows in 5.6 to 6.5 s. The worker shares the 5.4 MB result in 1.4 to 1.6 s and encodes it in 1.1 to 1.2 s, and the main thread decodes it in 1.4 to 2.0 s: 4.0 to 4.6 s in all, 3.5 to 4.0 times the evaluator's 1.1 to 1.3 s. (Matthew, 2026-11-08: "let's presume that the worker-main-thread communication cost dominates (preparing result, sending, decoding); let's confirm that".)
dfa3fcdffe, run 42486001354), the neck edits, whose picture comes with the run's result. Each bar runs from the edit to the picture showing it, its parts laid end to end. The worker's parts come from its own timeline (new here); the worker's other work is what is left of its time after the evaluator, sharing and encoding (the reuse plan, composing the request, the waits between slices).- How it was timed. The worker now keeps a timeline of its own while the page keeps one (
ClientMessage.Record), and sends it after each result (ServerMessage.Timeline), each step placed on the page's clock: the request decoded and composed, the reuse plan, each run of slices with its evaluator's share, making the result, and each message shared, encoded and posted, with its size. The same 8 edits as before, both ways (dfa3fcdffe, run 42486001354). - Sharing is the largest step, and it finds almost nothing to share. Before encoding a message, the worker walks it and hashes every value, so that a value the page already holds goes as a reference (
Util.ShareCodec,^ticket(513)). In these results it found none on the arms edits and 242 references on the neck edits, which still came to 5.4 MB. So on this slide it costs 1.4 to 1.6 s a result, and 2.0 to 3.4 s on an arms edit with its stream, and saves nothing measurable. - Encoding, posting, decoding. Encoding is
Marshalto a string, about 0.22 s a MB here. Posting the string takes 2 to 4 ms. Making the result (its value pruned, its cache left in the worker) takes under a millisecond. Decoding, on the main thread, isMarshalback, then the shared references resolved and the new values hashed and kept, about 0.32 s a MB, in one task during which the page cannot draw. - The arms edits. Their picture comes sooner, 4.2 to 5.5 s after the edit, in a 4.4 MB stream message the worker sends while it runs; that message's trip, shared and encoded in the middle of the run and decoded on the page, is 2.6 to 3.8 s. The result follows, with its own trip of 2.9 to 6.1 s, and the run is done 6.9 to 10.0 s after the edit; their evaluator, 1.2 to 1.8 s.
- The rest. The page's own work, before the request and after decoding the result, is 0.40 to 0.60 s; the worker's other work (the reuse plan, the request composed, the waits between slices), 0.11 to 0.25 s. Compiled views on or off, the same.
| the edit | compiled views | the picture shows it, s | the worker's evaluator, s | the result shared (in the worker), s | encoded (in the worker), s | decoded (on the main thread), s | the result's trip, s | the picture came with | the run done, the result handled, s |
|---|---|---|---|---|---|---|---|---|---|
| neck 28 to 30 | on | 6.4 | 1.1 | 1.4 | 1.2 | 1.9 | 4.6 | the result | 6.4 |
| neck 30 to 28 | on | 5.6 | 1.1 | 1.5 | 1.1 | 1.4 | 4.0 | the result | 5.6 |
| arms (up / down) 55 to 65 | on | 4.2 | 1.2 | 1.7 | 0.6 | 1.8 | 4.2 | a stream, 4.4 MB: shared 0.9, encoded 0.4, decoded 1.2 | 7.1 |
| arms (up / down) 65 to 55 | on | 5.2 | 1.8 | 1.0 | 0.6 | 1.3 | 2.9 | a stream, 4.4 MB: shared 1.0, encoded 0.4, decoded 2.0 | 6.9 |
| neck 28 to 30 | off | 6.5 | 1.3 | 1.4 | 1.2 | 2.0 | 4.6 | the result | 6.5 |
| neck 30 to 28 | off | 6.3 | 1.2 | 1.6 | 1.2 | 1.7 | 4.4 | the result | 6.3 |
| arms (up / down) 55 to 65 | off | 4.9 | 1.2 | 2.3 | 1.1 | 2.7 | 6.1 | a stream, 4.4 MB: shared 1.1, encoded 0.6, decoded 1.2 | 10.0 |
| arms (up / down) 65 to 55 | off | 5.5 | 1.2 | 1.0 | 1.4 | 1.4 | 3.9 | a stream, 4.4 MB: shared 1.3, encoded 1.2, decoded 1.3 | 8.0 |
What the worker's answer holds#
Mostly the environments of the probes' samples: 4.1 to 4.2 MB of the 5.4 MB result (77%). Nearly all of that is one binding in one sample: Drawing, the module that defines the slide's livelits, 3.8 to 3.9 MB, about 70% of the result by itself. The program's value is 1 KB, and the samples' own values 0.36 to 0.40 MB. Each sample keeps, beside its value, the bindings its expression refers to, each value written out in full (Sample.Env.elide, which substitutes the environment into it). The 4.4 MB stream of the arms edits is the same: 4.2 MB of environments.
- Whose samples. The result carries 108 probes, about two for each of the slide's 53 livelits: the run samples each livelit use's
view(model)and its model (the view fold-in,UserLivelit.instrument_view), and that is how the page gets each livelit's view and model after a run. The page's livelit code reads a sample's value (LivelitProj.live_html,latest_value), and a livelit's samples keep no environment: the page asks for none for them (CachedStatics.compute_targets,refs: []). - The one sample that is most of it (added 11-08, 07:23, run 42487576018,
405bc81bfc, which names each large probe's bindings). It is the sample of the editor's ⇓ toggle, a probe on the program's last line,(Drawing.anchor(drawn, "crown"), (hx, hy)), where the slide shows where the witch's crown and hands ended up (Refractors.tail_target). Its value is a few bytes, a closed, first-order value, and its drawer shows only that. But it is an explicit probe, and an explicit probe on an expression keeps every name the expression uses (Statics.Map.refs_in), so its environment is 4.0 to 4.1 MB in four bindings:Drawing,drawn,hxandhy. (Matthew, 2026-11-08: "why does the result need Drawing module? Isn't it a closed, first-order value?" It is, and it does not; the environment does.)drawn, the picture, is 0.20 MB.Drawingis 3.8 to 3.9 MB: the module that defines^polygons,^picture,^sliderand the drawing helpers, a record of functions. A sample's environment leaves out a value only when the value is itself a function (Sample.Env.elide:Opaque), so a module goes whole, every function in it with its environment substituted in. - The observation trace. The run's final state also keeps its observation trace, 11 events and 1.4 MB, nearly all of it the environments of its 5 span-close events (1.3 MB). The evaluation the worker runs in slices returns its state as it is, and the worker's
slim_stateempties only the state's cache, so the trace goes to the page, where nothing reads it. (The main thread's own evaluation clears it,clear_transient.) - The final stream. The result carries the run's last stream, 107 to 216 entries, one per top-level statement finished since the last; each entry's state holds the probes again, the same values, which the encoding writes once. So the parts below add up to more than the whole.
| the result, as it crossed | neck 28 to 30, MB | arms (up / down) 55 to 65, MB |
|---|---|---|
| the whole result | 5.38 | 5.40 |
| the probes' samples: their environments | 4.14 | 4.15 |
| the probes' samples: their values | 0.36 | 0.40 |
| the probes' samples: call stacks, arguments and frames | 0.14 | 0.13 |
the observation trace (the final state's obs_trace) | 1.40 | 1.44 |
| of it, the environments of its span-close events | 1.27 | 1.31 |
| the program's value | < 0.01 | < 0.01 |
| the final stream's entries, besides their states | 0.01 | 0.01 |
So most of the trip can be cut without compiling anything. In the order they would help, each to be measured on this slide:
- Leave out a module, or any value holding functions, from a sample's environment, as a function is left out now:
Drawingalone is 70% of the bytes, and so most of the encoding and the decoding. (Corrected 11-08, 07:40: this also said to send no environments with a livelit's samples; they have none already.) Done 11-08 (two cuts). - Skip the sharing of the worker's answers, or make it cheap: 1.4 to 3.4 s an edit in the worker, for almost no references. Done 11-08: dropped from the answers (two cuts, the paragraph after the table).
- Clear the trace before the result leaves the worker, as the main thread's evaluation does: 1.4 MB. Done 11-08 (two cuts).
- Skip the fold-in's view in the worker for a livelit the page draws compiled: the worker runs 53 views on the interpreter that the page then draws from JS.
At the rates above, a result of half a megabyte would take about a quarter of a second to encode and decode, against 1.1 to 1.2 s and 1.4 to 2.0 s now; with no sharing, a neck edit might then take about 2 s, the evaluator most of it. That is an estimate, not a measurement. (Two cuts made, 11-08: 1.9 to 2.4 s, with the sharing still in, below.)
Two cuts, made and measured#
With two of the cuts made, a small edit on the Witch slide shows in the picture 1.9 to 2.4 s after it, 2.2 to 3.2 times sooner than before, compiled or not. The result is 0.42 MB instead of 5.4, about 13 times smaller. The evaluator, 1.0 to 1.1 s, is now about half of an edit (44 to 56%), and the trip 16 to 22%. (Matthew, 2026-11-08: "Yes, make the cuts".)
20a3067c18, run 42490487136), the 7 edits whose picture came with the run's result, both ways, after the two cuts: a value holding a function left out of a sample's environment, and the trace cleared. The dashed mark is the same edit before the cuts (run 42486001354). The evaluator (blue) is now about half of each bar; the trip (orange) is the result shared, encoded and decoded.- The two cuts (
20a3067c18, on the hazel-jit trial branch). A value holding a function anywhere is left out of a sample's environment, like a function (Sample.Env.holds_function), unless the probe keeps function values, as a stepper's does;Drawingnow goes asOpaque. And the worker clears the observation trace before its answer leaves (WorkerServer.slim_state,clear_transient). The same 8 edits, both ways (run 42490487136); the drawings are the same both ways. - The trip now. The result is shared in 154 to 255 ms, encoded in 48 to 57 ms, and decoded in 113 to 159 ms; 0.32 to 0.44 s in all. On one edit of the 8 the picture came sooner, in a 356 KB stream at 2.1 s.
- Sharing now costs more than it saves. It finds 2,901 to 3,320 values the page holds, and they take the result from 0.46 MB to 0.42 MB. The few milliseconds of encoding and decoding that saves are less than the 154 to 255 ms the sharing takes, which is more than encoding and decoding the whole result. So the sharing cut is next.
- What is left of the answer. The probes, 0.34 to 0.35 MB: the picture, written out again in each of four samples (the ⇓ probe's
drawn, 0.14 MB; the^^probe(drawn)sample's value and its environment; the picture livelit's samples). Each copy is made afresh, by the substitution into it, so not even the encoding shares them. That is^ticket(718)'s case: first-order data in a form of its own could be kept once. - The rest. The page's own work, before the request and after the decoding, is 0.36 to 0.79 s; the fold-in cut (the worker running the slide's livelits' views, which the page then draws compiled) is inside the evaluator's time, and is not made yet: it changes what a livelit falls back on when its view cannot be compiled.
| the edit | compiled views | the picture shows it, before the cuts, s | after, s | the evaluator, s | the trip of the picture's message, s | that message | faster, times |
|---|---|---|---|---|---|---|---|
| neck 28 to 30 | on | 6.4 | 2.1 | 1.0 | 0.40 | 0.41 MB, the result | 3.1 |
| neck 30 to 28 | on | 5.6 | 1.9 | 1.0 | 0.42 | 0.41 MB, the result | 2.9 |
| arms (up / down) 55 to 65 | on | 4.2 | 1.9 | 1.1 | 0.33 | 0.41 MB, the result | 2.2 |
| arms (up / down) 65 to 55 | on | 5.2 | 1.9 | 1.0 | 0.32 | 0.41 MB, the result | 2.8 |
| neck 28 to 30 | off | 6.5 | 2.4 | 1.1 | 0.39 | 0.42 MB, the result | 2.7 |
| neck 30 to 28 | off | 6.3 | 2.0 | 1.0 | 0.44 | 0.41 MB, the result | 3.2 |
| arms (up / down) 55 to 65 | off | 4.9 | 2.1 | 1.1 | 0.35 | 0.36 MB, a stream | 2.3 |
| arms (up / down) 65 to 55 | off | 5.5 | 2.0 | 1.1 | 0.33 | 0.41 MB, the result | 2.8 |
Sharing dropped (added 11-08, 08:02): a small edit now shows in 1.3 to 1.7 s, 2.9 to 4.6 times sooner than before the cuts; the trip is 79 to 102 ms, 5 to 8% of the edit, and the evaluator, 0.81 to 0.92 s, 50 to 64%. (Matthew, 2026-11-08: "drop sharing".) The worker's answers are no longer shared (4747a4120d, run 42492117212): the result is 0.59 MB unshared, against 0.42 MB shared, but it is encoded in 59 to 80 ms and decoded in 16 to 24 ms, against 113 to 159 ms to decode it shared: the page no longer resolves references and hashes every value it is sent. The evaluator's time fell too, from 1.0 to 1.1 s; why is not measured (the worker no longer keeps a table of every value it sent, and collects less garbage, is a guess). The requests keep their sharing; the drawings are the same both ways. Readings in data/witch/firefox-noshare.json.
Compiling the run: a speculation#
In the app (2026-11-08 evening, ^report(49)): the first point below, that the run samples every livelit's view today, does not hold on the slides: a slide is elaborated with only its cells' own probes, so the run calls no livelit's view, and the page runs each view itself (none of the Witch's 53 livelits is drawn from the run's samples). The compiled run now folds the views in itself, and the idea is measured: in Firefox the run answers a slider's step in 10 to 12 ms, and on two of four steps the picture showed at the page's first redraw, 318 and 469 ms after the step (^report(49)).
Now measured (11-08, 17:22): hazel-jit compiles the Witch slide's whole program and runs it in Node in 4.2 to 5.0 ms against the interpreter's 502 to 529 ms, 106 to 119 times faster, with the same values; not yet in the app (^report(45), the whole run, compiled; ^ticket(722)). The estimate below said 39 to 62 ms at the smallest speedup then measured.
The trip between the worker and the page is most of an edit (above), and the way to save all of it is not to take it. Under the right conditions, the slide's program could run compiled to JS on the main thread, and each livelit's model could go straight into its compiled view as a JS value, as a view's answer goes straight to the page now. Nothing would be made Hazel, encoded, posted or decoded, and nothing would wait on the worker. This is a speculation, not measured. (Matthew, 2026-11-08: "speculate on ways to hazel-jit more of the worker thread for a slide, so it can be absorbed into the main JS thread under ideal conditions".)
- What the page takes from the run today. Each livelit's view and model, as samples: the run computes
view(model)for every livelit use on the slide (53 here) and samples it at the use's projector (the view fold-in,UserLivelit.instrument_view), and the page draws each livelit from its latest sample (LivelitProj.live_html). Besides those: the probes' samples and the tests, for the editor; the program's value, for its result; and each statement's status, for the editor's highlights. After a slider's edit on this slide, only the first is drawn: the picture, and the slider itself. - The idea, from views to the run. A compiled view's answer stays in JS from the call to the page's elements (above). The run could do the same. Compile the slide's program once, with each livelit use's model as a parameter (its literal lifted out of the code), so an edit to a model is a new argument, not new code. At each use, where the run samples
view(model)now, call the livelit's compiled view on the model's JS value, and draw its answer as now. The run's answer, as far as the page is concerned, is those calls' answers; nothing else has to leave JS. - The conditions: when the fast path is allowed.
- The program is statically clean: no type errors and no holes (an indeterminate value has no JS form), and every form in it is one hazel-jit compiles.
- No probes or tests are shown for the slide (their samples are Hazel values with environments; a test's pass or fail could be counted in JS instead).
- The edit changes livelit models only (a slider, a polygon editor), each to a value of the same type, so the statics need not run again.
- The run ends within a budget. On the main thread nothing can stop a loop, so the compiled code would count its calls (fuel) and give up past a limit.
- JS's numbers are accepted, as for compiled views (
^ticket(694)'s best case). - No stepper or trace is open on the slide.
- The worker, afterward. The fast path shows the edit; the worker can still run the program after it, at a lower priority, for what only it gives: probes, statuses and the editor's decorations. Its answer can also check the JS's, as
^report(45)'s tests check compiled views against the interpreter. - What it might gain. The evaluator's 1.1 to 1.8 s is Hazel's interpreter. Compiled,
^report(45)'s whole programs, their answers kept in JS, ran 284 to 1,152 times faster in Node, and the picture's view in Firefox, with its trip in, 29 times. Even at the smallest of those, the run would take 39 to 62 ms, and the trip, 4.0 to 4.6 s, would be gone. What would be left is the page's own work: about 0.18 to 0.27 s before the request today, and the whole slide's view, 117 ms each time it is drawn. That page work would then be the next cost. - The cuts above first. They keep the worker, and every condition below goes away with them; they are the first step whichever way this goes. A compiled run in the worker, posting the livelits' models as plain JS data (tens of KB for the picture) instead of Hazel's values, would be a step between them and this.
- Incremental, later. With the models as the program's inputs, its top-level definitions could be kept by their inputs, and an edit could rerun only what depends on the model it changed: the neck slider moves 17 of the picture's 133 shapes. A first cut does not need this.
Better numbers, from ^report(45)'s sharing#
After a small edit, the picture's view could redo only the shapes that changed. In Node that is 0.043 ms for the neck's 17 shapes and 0.071 ms for the arms' 30, against 0.24 ms for the whole view: 5 and 3 times less work, and 0.007 ms for one shape. But it needs the new picture to share its unchanged parts with the last one, and the sharing has to come from the trip in itself. Done after the trip in, as a walk of its own, it costs 0.23 ms: as much as the view it saves.
The idea is ^report(45)'s diff, run the other way. There, the diff stops wherever the last answer and the new one share an object, and the patch keeps every unchanged part of the old value. Here, the new picture is first merged with the last one, so that every part equal to the last one's is the last one's object. Then a view that keeps its per-shape work by the shape it was given redoes only the changed shapes. (The keeping is a memo by object identity, on the compiled shape_node and on shape_nodes' tail.) A prototype, data/witch/edit-share.mjs, takes hazel-jit's own compiled view and the slide's picture, moves the first vertex of k shapes spread through the picture, and times both ways, medians of 500 runs. The markup is the same both ways for every k:
| shapes changed, of 133 | the whole view, ms (today) | the merge with the last picture, ms | the kept view on the merged picture, ms | the whole view over the kept one, times | the same markup |
|---|---|---|---|---|---|
| 1 | 0.236 | 0.231 | 0.007 | 33 | yes |
| 5 | 0.231 | 0.232 | 0.027 | 9 | yes |
| 17 | 0.211 | 0.212 | 0.043 | 5 | yes |
| 30 | 0.219 | 0.226 | 0.071 | 3 | yes |
| 60 | 0.222 | 0.239 | 0.129 | 2 | yes |
- The merge is the catch. To find what is equal, it walks the whole picture (6,693 values), so it costs about what the view does. The new picture arrives from the program's run as a fresh value that shares nothing with the last one, so something must compare the two. The place to do it is the trip in, which walks the whole picture anyway: compare each part with the last model's as it goes, reuse the last JS object where they are equal, and build only what changed.
- The drawing would follow. A drawing kept by the answer it drew (in
HazelJitDOM) would draw only the changed shapes' nodes, and the virtual DOM's diff skips a node that is the same object as before. Today the drawing from JS is 0.64 ms of the 3.4 ms frame in the Hazel test, and the app draws a kept answer again on every render (above). - The trip in itself could be cheaper. js_of_ocaml's
to_jstakes 0.88 ms for the picture in the Hazel test. Building the same JS objects in plain JS takes 0.08 ms in Node, 11 times less. These are different processes, so the comparison is rough. - Put together, a render after a small edit might cost about the trip in's walk, instead of the 3.4 ms frame. On this slide, though, that saves milliseconds out of seconds: the run is what a small edit waits for.
The drawings#
Each version's Html, written out as SVG markup by a function in its own language, shown here as an image. They are the same, byte for byte, and each links to its page:
0.19 ms (5,200 fps)
0.18 ms (5,600 fps)
0.17 ms (5,800 fps)
1.01 ms (990 fps)
0.17 ms (6,000 fps)
The view, compiled: one page per output#
The compiled views are long, so each has a page of its own, with its source where there is one, its listing highlighted, and its drawing.
- Hazel, the source: the livelit as the slide defines it, 105 lines.
- hazel-jit: the livelit's whole module, as the app compiles it in 6.1 ms: each function of a tuple called with an array,
to_fixedthrough the runtime, every walk a recursive call. - ReScript: the source and its output:
whileloops, string tags,toFixedcalled directly. - Melange: the OCaml source and its output, ReScript's with numbered tags.
- js_of_ocaml: the OCaml source and the view's part of the whole program: its walks inlined as
forloops, its numbers throughPrintf. - By hand: arrays and
for…of.
How each lays out the view's values#
The view's values are records, lists, Html nodes and strings, and the compilers lay them out much as they did the rule's. What sets the times apart is how each makes a number's text, and, for hazel-jit, an array for every call of a function of a tuple.
| Value, or step | hazel-jit | ReScript | Melange | js_of_ocaml | by hand |
|---|---|---|---|---|---|
a vertex, (at = (x, y), label = Plain) | [at, label], with at and label as properties too; at is [x, y] | {at: [x, y], label: "Plain"} | {at: [x, y], label: 0} | [0, [0, x, y], 0] | [x, y, null] |
a shape, Polygon((name = n, points = ps, …)) | {c: "Polygon", v: [n, ps, …]}, the labels as properties too | flat: {TAG: "Polygon", name, points, …} | flat: {TAG: 0, name, points, …} | [0, n, ps, …] | {kind: "Polygon", name, points, …} |
an Html node, Html.node("polygon", attrs, []) | {c: "Node", v: ["polygon", attrs, null]} | {TAG: "Node", _0, _1, _2} | {TAG: 0, _0, _1, _2} | [0, "polygon", attrs, 0] | {tag, attrs, kids} |
an attribute, Attr.create("fill", f) | {c: "Create", v: ["fill", f]} | {TAG: "Create", _0, _1} | {TAG: 0, _0, _1} | [0, "fill", f] | ["fill", f] |
a number's text, to_fixed(x, 1) | $R.b.to_fixed([x, 1]): an array, the digits clamped, then toFixed | x.toFixed(1) | x.toFixed(1) | Printf.sprintf "%.1f": the format read at every call, then toFixed | x.toFixed(1) |
a function of a tuple, grow((b, v)) | one array argument, made at each call | grow(b, v) | grow(b, v) | inlined | inlined by hand |
the walks, grow_all, bounds, framing | recursive calls | while loops | while loops | nested for loops, inside the view | for…of |
| the bounds, a 4-tuple | a new array per point | a new array per point | a new array per point | a new block per point | four variables |
p.name == "frame" | $R.eq(…) | === | === | === | === |
| what the view returns | a command, Bind(EvalSplice(ref), k); k, called with the splice's value, makes the Html | the Html, from the splice's value (the Hazel's case r) | the Html | ||
What holds across both#
The compilers differ in how they make calls, matches and values, and those costs show where a program's work is mostly calls and matches, as in the rule. Where a builtin does most of the work, as in the view, they come out almost the same; then the cost is in how each reaches the builtin. The two tables above, read together:
| Question | The Petri rule | The Witch's picture | What generalizes |
|---|---|---|---|
| Where does the time go? | Calls, matches, small allocations: a few list walks and a command built per event. | Numbers formatted, 1,985 of them (0.14 of 0.17 ms by hand), and strings joined. | The compilers' choices matter in proportion to the program's control and allocation, not its arithmetic or its builtins. |
| hazel-jit, against ReScript | 2.9 times as long. | 9% longer. | The gap is in hazel-jit's calls (an array per call of a function of a tuple), its generic equality and its recursion; the five changes below address exactly those. |
| Compiled, against the interpreter | 225 times faster (hazel-jit). | A frame 45 times faster; the view's work alone 134 times, in the same process. | Compiling pays most where the interpreter takes many small steps: a view over a large value is such a case. |
| JS written by hand | Slower than three compilers: it copies arrays and calls closures, as people write JS. | The fastest: plain loops over arrays. | Hand-written JS is as fast as its idioms; a compiler's is the same every time. |
| js_of_ocaml | Among the fastest, by inlining the rule into its caller. | The slowest by far through Printf, among the fastest when it calls JS's toFixed. | Whole-program inlining helps; OCaml's runtime library, written in JS, can cost more than the program. |
| Value layouts | Constructors and options decide the allocations. | Records and Html nodes: one object each in ReScript and Melange, an object and an array in hazel-jit. | Flat objects, one shape per type, as ReScript and Melange make them, are the layout to copy. |
| Do they agree? | The same marking after 2,000 events. | The same SVG, byte for byte, and the interpreter's. | On both, nothing was lost in translation that the outputs show. |
What it suggests for hazel-jit#
Five changes to what hazel-jit writes, each something these compilers already do, would close most of the gap to ReScript. In the order they would likely help this rule:
- Pass a function's arguments as JS arguments. A Hazel function of a tuple,
fun (n, i) -> …, is a JS function of one array, so every call allocates an array and reads it back. Where the parameter is a tuple pattern, compile(n, i) => …and call it withf(n, i), keeping the one-argument form for a function passed as a value. - Compare Ints with
===. hazel-jit calls its generic structural equality for every==; the elaborated term knows when both sides are Ints (or Floats, Bools, Strings). - Turn tail calls into loops, as all three do for
nth,pickandhas: faster, and no stack to overflow on a long list. - Unbox options, and make constructors flat.
Some(t)astandNoneasundefinedsaves an allocation per option;{TAG, _0, _1, _2}saves the inner array of{c, v: [a, b, k]}. - Lay out labeled tuples as objects only, not as arrays with the labels as properties too: one shape per type, which JS engines optimize best.
On the Witch's picture the five matter less, since the builtin does most of the work there, but the first and fourth still apply: each point calls grow and grow_all with a fresh array of arguments, and each number goes through $R.b.to_fixed([x, 1]), another array, where the others call x.toFixed(1). A sixth change follows from it: call a builtin JS has directly when its arguments are known to be in range (to_fixed's digits a literal 1, say), as ReScript and Melange do.
Each changes the layout that the trip in and out reads and writes (^report(45)'s to_js and of_js), so each is a change to both sides, and to HazelJitDOM's reading of a view's answer. The first three change only the code inside a compiled function, and are the place to start.
Could any of them be used#
Not as hazel-jit's back end; but what they do, yes.
- As a back end, each would need Hazel translated into its source language (ReScript or OCaml) and then compiled, in the browser, as the program changes. Hazel's holes, its gradual types and its livelits have no counterpart in either, so the translation would refuse much of what hazel-jit compiles, or fall back as hazel-jit does now. And the toolchain is large: Melange's playground compiles in the browser, but only by loading the whole compiler, built to JavaScript, first.
- Their value layouts are a good target for hazel-jit's next version, above, and would let Hazel values pass to ReScript or Melange code directly, if Hazel apps ever share values with code written in them.
- js_of_ocaml is what Hazel itself is built with, and its whole-program optimization (inlining across functions, as it did here) is not something a compiler working one definition at a time, in the browser, can do the same way.
What next#
- Try the first three changes in hazel-jit (arguments as JS arguments,
===on Ints, tail calls as loops), and time the rule again. - Then the layouts (flat constructors, unboxed options, objects for labeled tuples), with the trip in and out and
HazelJitDOMchanged to match. - Time the Witch's picture on the slide itself, in Firefox. Done 11-07 (in Firefox): at first not compiled, 90 ms a render on the interpreter; since 15:05, 3.0 ms compiled.
- Make the Witch's picture compile in the app. Done 11-07 15:05 (
cb560ebb86): the model did hold the picture after the run; a refusal before the run had given up on the definition. Now 3.0 ms a render in Firefox (in Firefox). - Break down a small edit (above): the 0.31 to 0.41 s on the main thread before the first redraw, and the run's seconds in the worker. That is where a small edit's time is. Done 11-07 21:20 (where a small edit's time goes): the evaluator is 1.2 to 2.6 s; the worker's answer, 4.4 to 5.4 MB, takes most of the rest, on both sides.
- Find what is in the worker's 5.4 MB answer, and record the worker's own split (evaluating, making the result, encoding it). A smaller answer saves time in the worker and on the main thread, where decoding it freezes the page. Done 11-08 (the trip, what the answer holds): the samples' environments, 77% of it; sharing and encoding in the worker, decoding on the page.
- Make the four cuts (above), each measured on the Witch's small edits: the livelits' samples without environments, no sharing of answers, the trace cleared, no fold-in view for a compiled livelit. Two done 11-08 (two cuts): function-holding values out of samples' environments, and the trace cleared; 1.9 to 2.4 s an edit. The livelits' samples had no environments already. Then the sharing dropped: 1.3 to 1.7 s. Next: the fold-in, and
^ticket(718). - First-order data in a form of its own (
^ticket(718)), for dev: the systematic fix for paying the costs of open terms on closed data. - Try compiling the run (the speculation), first in the worker, its answer as plain JS data, then on the main thread under the conditions listed.
- The page's view: 117 ms each time on this slide; find what in it is redone when only a slider changed.
- Keep a drawn answer's node with the answer, so that a kept answer is not drawn from JS again on every render.
- Once the run is fast: share in the trip in, and keep the view's and the drawing's per-shape work (above).
- Find what the interpreter spends its 155 ms on: most of it is the splice's value, the picture, evaluated again on every render, which the app's call memo may already spare it.
Each alternative, for and against#
For running a Hazel program's views and updates compiled, in the browser, as the program is edited, hazel-jit stays the right base: it is the only one of these that compiles Hazel itself, all of it that it can, while the program runs. ReScript and Melange are the models for its output; js_of_ocaml stays what Hazel itself is built with. The question for each is the one Hazel has: compile a program that is being edited, holes and all, in the page, and hand its values back and forth with the editor.
hazel-jit#
- For: it compiles Hazel's own elaborated terms, so nothing is translated first, and whatever it cannot compile (a hole, a test, a splice) runs on the interpreter, one definition or one form at a time. It runs in the page, and fast enough to compile on every edit: the Petri rule's program in about 0.6 ms, the Polygons livelit's definition in 3 to 10 ms. Its values come back as Hazel values, closures as Hazel closures, so the editor, the probes and the interpreter take them as their own. Its runtime is about 2.5 KB; the compiler is already in Hazel's bundle.
- Against: its code is the slowest of the compilers here, 1.59 µs a call, about 2.9 times ReScript's, for the reasons above: arguments in an array, a generic equality, no loops, boxed options. It uses no types to choose its code yet. Its Ints are JS numbers, which overflow and round as JS's do, the best-case trade agreed for now. It compiles one definition at a time, so it cannot inline across a program as js_of_ocaml does. On the Witch's picture it is within 9% of ReScript, at 0.19 ms (5,200 fps).
ReScript#
- For: among the fastest code here, 0.55 µs a call, and the most readable: named tags, objects by field name, functions called with their arguments. It is mature and used in production, its layouts are documented, and its code passes values to and from plain JS easily. On the Witch's picture, 0.18 ms (5,600 fps).
- Against: it is a language of its own, with its own syntax and standard library, so Hazel would be translated into it first, and Hazel's holes, gradual types and livelits have no counterpart there: the translation would refuse an unfinished program, which is most programs while they are being edited. Its compiler is a native program; in the page it would have to be loaded as JavaScript, as its online playground does. And each edit would mean a translation and a build.
Melange#
- For: the fastest compiled code here, 0.46 µs a call on the rule, 0.17 ms (5,800 fps) on the Witch's picture. It compiles OCaml, the language Hazel is written in, with OCaml's own tools (dune, opam), so Hazel's implementers know it, and OCaml that Hazel's tools generate could go through it unchanged. Its playground shows it running in the browser.
- Against: everything said of ReScript as a back end, with OCaml in place of ReScript: Hazel translated to OCaml first, and OCaml's type checker rejecting what Hazel accepts (a hole, a mismatch the gradual types allow). The whole compiler loaded in the page. Its constructors are numbered, not named, so its JS is a little harder to read than ReScript's.
js_of_ocaml#
- For: it already builds Hazel. It optimizes a whole program at once, and inlined the whole rule into its caller here (0.71 µs a call, a best case). It compiles OCaml's bytecode, so any OCaml runs, exactly as OCaml means it, its integers included.
- Against: for a Hazel program it would mean Hazel to OCaml, OCaml to bytecode, bytecode to JS, ahead of time: far too heavy to do as a program is edited. Its JS is OCaml's memory layout (every value an array with its tag first), hard to read and awkward to hand to plain JS. And OCaml's library comes along, as JS: on the Witch's picture, numbers formatted with
Printfmake its view 5.3 times as slow as the same view calling JS'stoFixed(1.01 against 0.19 ms).
BuckleScript#
- For: it is where ReScript's and Melange's code generator comes from, and why their output looks alike.
- Against: it is no longer developed under that name: ReScript continues it, and Melange is the fork that kept OCaml. Not an option of its own.
Writing the JS by hand#
- For: no compiler at all, and full control: the Petri example's own fallback handler is written this way, for when its Hazel rules do not build.
- Against: the point is to write the rule in Hazel, and see it in Hazel's editor. And hand-written JS is not the fastest by default: 0.90 µs a call here, slower than three of the compilers, because it is written the way people write JS. (On the Witch's picture, written with loops over arrays, it is the fastest: 0.17 ms.)
Hazel's interpreter#
- For: all of Hazel: holes, tests, probes, stepping, the call memo, Hazel's own Ints. No compile step, and the semantics are the definition's.
- Against: 358 µs a call on this rule, 225 times hazel-jit's; on a Polygons render, about 37 times (
^report(45)); a frame of the Witch's picture, 155 ms (6 fps), 45 times hazel-jit's, and far outside a 60 fps frame. It stays what runs whatever hazel-jit cannot.
How this was made#
- The program is
^report(43)'s Petri net rule as matthewhammer/hazeljs-petri-example has it at 4c85792 (itsrules: the preamble,Common,RulesandMain), written again in ReScript (data/Petri.res) and OCaml (data/petri.ml), function for function, as the Hazel is written. - The compilers: ReScript 12.3.1 from npm; Melange 5.0.0 (with OCaml 5.3.0) in its playground at melange.re, its JS copied from there; js_of_ocaml 5.9.1 from the opam switch Hazel builds with (
ocamlcto bytecode, thenjs_of_ocaml --pretty); hazel-jit at28fc073ed5ontrial/694-view-jit, its compiled program run in plain Node with its runtime. - The timings: Node 22.23.2 on a Framework 13 laptop (Intel Core i7-1370P), 2026-11-07 at 10:43 MST. One script (
data/bench.mjs) runs the hand-written handler, ReScript's, Melange's and hazel-jit's in one process, each on its own values, 2,000 events each; js_of_ocaml's program times itself the same way. Five runs of each; the table gives the median of the five runs' medians, and the fastest single call. The interpreter's number is fromTest_HazelJitat28fc073ed5, a development build. - The view is the Witch slide's
Drawing.^picture(hazel-programs/docs/livelits/playground/witch.hzatba72773d46,data/witch/picture.hz), on the picture the slide draws, evaluated by the Hazel test (Test_HazelJit'switch_view_run, test 50) and written out as JSON (data/witch/picture.json). Its view is written again in ReScript (Witch.res), in OCaml (witch-core.ml, with a one-line prelude for each compiler) and by hand, from the point where the splice's value arrives. hazel-jit's output is the test's own compile of the livelit's module, with the runtime it printed (hazeljit-witch-src.js,hazeljit-runtime-src.js); in the benchmark its model is laid out asto_jslays it out, and its command run asHazelJitDOM.runruns it. - Its timings:
data/witch/witch-bench.mjstimes hazel-jit's, ReScript's, Melange's and the hand-written view in one Node 22.23.2 process, three runs, each the median of five rounds of 200 calls after 20 untimed; js_of_ocaml's two programs time themselves, five runs of 200. Each version's Html is written out as markup in its own language and compared with the SVG the test drew from hazel-jit's answer, which the test checks against the interpreter's drawing. The frame's numbers are from the test, atbcec455abcontrial/694-view-jit(the splice's answers made in JS), two runs of 15 pooled (raw-run1.json,raw-run2.json), 2026-11-07 at 11:28 and 11:35 MST. - The small edits:
scripts/jit_slides.py --editon hazel-jit's Firefox check (2a5b2ffefa, run 42441046851, a release build on GitHub's machines), on a fresh load of the Witch slide each way. The script sets the slider's value, dispatches its input and change events together, and watches the whole page with aMutationObserverfrom that moment. It records the first change under the slider's livelit,#dynamics's class (the run's status), and the first time the picture's livelit (the one drawing the most shapes) shows a different set of shapes, then waits for the page to settle before the next edit. Readings indata/witch/firefox-edit.json. The observer's own work, finding the livelits again at each change, is inside these times. - The timeline of an edit: the same edits again (
3ed2f31844, run 42445268287, the Witch slide only), withglobalThis.hazelBenchMarksset just before each edit, so the page recorded its own steps (Util.BenchMarks, inMain.re,PerfMetrics.reandWorkerClient.re) withperformance.now, the clock the check's edit times use. Recording costs two clock reads and a push a step. Readings indata/witch/firefox-timeline.json. - The worker's timeline and the answer's parts: the same edits again (
dfa3fcdffe, run 42486001354), with the worker's own timeline (WorkerServer.wtime, onperformance.timeOriginso the page can place it) and each large answer kept as it arrived and broken down after the edit's timing was done (AnswerParts,globalThis.hazelAnswerParts, 56 to 83 s an edit). A part's size is estimated by a walk (a block 8 bytes a field, a string its length, a shared value once) and scaled to the bytes the whole message took:Marshalitself, part by part, took more than the check's 90 s an edit. The evaluator's time is the result's owneval_ms: the run of slices that posts a result is recorded after the timeline it would be in has gone. Readings indata/witch/firefox-answer.json. - The two cuts, measured: the same edits again (
20a3067c18, run 42490487136), as above. Readings indata/witch/firefox-cuts.json; the largest probes' bindings before the cuts indata/witch/firefox-answer-probes.json(run 42487576018). - The sharing prototype:
data/witch/edit-share.mjsin Node 22.23.2 on the laptop, at 20:20 MST on 2026-11-07, on hazel-jit's compiled view aswitch-bench.mjsruns it, with a memo spliced into its source. For each k it does 500 runs; each draws the last picture once to fill the memo, makes the new picture fresh, then times the whole view on it, the merge, and the kept view on the merged picture. Readings indata/witch/edit-share.json. - The numbers, for programs too: every table in this report, and what its numbers measure, is also in this page as JSON, in its
<script type="application/json" id="report-data">element, so a reader or an agent that fetches the page alone has the dataset. The same, with any raw files that survive, is in046/data/.