Hazel reports · ^report(45) · hazel-jit · started 2026-11-06, updated 2026-11-08 19:50 MST · work in progress · for the meeting with Cyrus on Wednesday 2026-11-11 (^report(42), item 2)
hazel-jit: Livelit Views Compiled to JavaScript
How much faster can a livelit's view run as JavaScript compiled from its Hazel, instead of on Hazel's interpreter? Decided at the meeting with Cyrus on 2026-11-05, as the best case first: JS's numbers allowed to differ from Hazel's for now (^ticket(694)). This report follows the work as it goes, for the next meeting on Wednesday 2026-11-11 at 10:00 Mountain, where it is item 2 of the agenda, ^report(42).
What the milliseconds here measure (added 11-08): livelits' views and updates, and on the slides the clicked livelit's own change. That is a small part of what follows an edit. A change to a livelit's model is a change to the program, which then runs in Hazel's worker; what the run changes shows only after it. Timed so far on one slide, the Witch, in ^report(46): 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, 1.2 to 2.6 s in the 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, where its time goes). The other slides' runs are not timed yet. (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".) Since 07:05: most of those seconds, 70 to 72% on the neck edits, are the run's answer going from the worker to the page, shared and encoded in the worker and decoded on the page; the evaluator is 18 to 20% (the trip). ^report(46) also speculates on compiling the run itself, as this report compiles views, so that under ideal conditions it runs on the main thread and its answer never leaves JS (compiling the run). Since 17:22: the run itself is compiled now, and it is milliseconds too. hazel-jit compiles the Witch slide's whole program, and in Node it runs in 4.2 to 5.0 ms against the interpreter's 502 to 529 ms, 106 to 119 times faster, with the same values; a slider's step, compiled once with the new model a parameter and every view called, 2.3 to 2.5 ms against 684 ms (the whole run, compiled). Not yet in the app.
Outline#
- How much faster: a render of the Polygons slide's view, on the interpreter and compiled, its first renders and once warm.
- The whole run, compiled (
^ticket(722), 11-08): the Witch slide's whole program, 2,222 lines with 53 livelit uses, compiled and run in Node: 4.2 to 5.0 ms against the interpreter's 502 to 529 ms; a slider's step, compiled once with its model a parameter, 2.3 to 2.5 ms against 684 ms; every value the program binds the same both ways. Not yet in the app. - Where the time goes now: the trip back, the view's answer made again as Hazel terms, is most of a compiled render; the trip in is small.
- Three ideas for the way back (Matthew, 2026-11-07): cheaper ids, a view's answer never made Hazel, and the way back as a diff; all three work, and not coming back at all helps most.
- In a browser (
^ticket(716),^ticket(717)): on the six polygon slides in Firefox, compiled views draw exactly what the interpreter draws, before and after the same clicks, and a click shows sooner on every slide; the rest of the page's work is now most of an event. These times end at the clicked livelit's own change: on the Witch, what the program's run then changes shows 5.3 to 8.8 s after the edit (^report(46)). - Beside JS written by hand (
^ticket(699)): on^report(43)'s Petri rule, hazel-jit's code takes 1.59 µs a call, the hand-written 0.90, the interpreter 358; ReScript's and Melange's beat both, and^report(46)says why. - How it works: the compiler, how Hazel's values look in JS, and the trip in and out.
- In the app: since 11-07, a view drawn straight from its compiled JS, and an update compiled, its new model back as a diff; the setting, and what happens when it cannot.
- Does it give the same answers: the tests, and checking that they can fail.
- What it does not do yet.
- For Wednesday's meeting: the agenda's questions about this work.
- What next, and how this was put together.
Update, 2026-11-08 evening: the Witch's whole run, compiled, now runs in the app at a slider's step (^report(49)): in Firefox it answers in 10 to 12 ms (3 to 4 ms for the next event of a drag), all 53 livelits' views included, and in 2 of 4 steps the picture showed at the page's first redraw, 318 and 469 ms after the step, against 1.90 to 2.24 s with nothing compiled in the same run. On those steps the evaluator is out of the way; what is left is the page's own work (statics, the whole slide's view twice) and the program compiled again after each step. The program the page holds is not the one measured in Node below: a slide is elaborated without its livelits' views in the run, which hazel jit --views put in by probing every use (what the Node numbers assumed).
How much faster#
A render of the Polygons slide's view takes 13.7 ms on the interpreter and 0.34 ms compiled, about 40 times faster, counting the trip into JS and back, once Node has warmed up to it. The first 30 renders after compiling take 1.03 ms (their median), about 13 times faster: the first took 2.24 ms, and they fall to about 0.36 ms while Node optimizes the way back. The compiled code itself takes 0.043 ms of that, about 315 times faster than the interpreter. Smaller programs gain more, since there is no trip to take: the Petri net rule of ^report(43), answering four events, 7.19 ms against 0.011 ms (630 times), and a recursive sum of 1,000 numbers, 47.9 ms against 0.042 ms (1,152 times).
Update, 2026-11-06 19:00: the first version of this report said a render was about 16 times faster. Those renders were timed first and cold, the first 30 after compiling, and their times fell from 2.1 ms to 0.4 ms over the 30 runs as Node optimized the way back, so their median measured the warm-up more than a render. Each series is now timed after 10 untimed runs. Warm, a render was then about 34 times faster; the first renders, timed cold as before, are a row of their own (^ticket(694)).
trial/694-view-jit (4c0ed173f1), on one laptop. The compiled times count the compiled code only; the Polygons render is also shown with the model carried in and its answer carried back: its first 30 renders after compiling, and 30 more once warm (after 10 untimed).These are Node's numbers, from a development build, on one laptop, medians of 30 runs in one test session; the runs vary (the Petri net rule's interpreter time ran from 4.8 to 9.2 ms over the day's test sessions), so read the ratios to within a factor of two. The interpreter's numbers do not include the call memo the app gives a view (^report(1)'s CallMemo), which saves calls repeated between renders; compiling the definition took 4.46 ms, once. Browser numbers, and a release build, come next.
| Program | Interpreter, ms | Compiled, ms | Faster |
|---|---|---|---|
| A Polygons render, its first 30 renders, with the trip in and out | 13.7 | 1.03 | 13× |
| A Polygons render, warm, with the trip in and out | 13.7 | 0.34 | 40× |
| A Polygons render, the compiled call alone | 13.7 | 0.043 | 315× |
| The Polygons view's whole program (its definitions and the view applied) | 14.7 | 0.052 | 284× |
| The Petri net rule, four events | 7.19 | 0.011 | 630× |
| A recursive sum of 1,000 | 47.9 | 0.042 | 1,152× |
The whole run, compiled#
In the app (2026-11-08 evening, ^report(49)): the numbers in this section are in Node, on the program elaborated with every livelit use probed. The page holds the slide's program without the livelits' views in it, so the compiled run first had no model to hand a new value to; it now folds the views in itself. In Firefox, at a slider's step, it answers in 10 to 12 ms (3 to 4 ms for the next event), all 53 views included (a step, run compiled).
hazel-jit compiles the Witch slide's whole program and runs it in 4.2 to 5.0 ms, against 502 to 529 ms on Hazel's interpreter: 106 to 119 times faster, inside a 60 fps frame's 16.7 ms, with the same values. Matthew, 2026-11-08: "Should we hazel-jit more of the evaluator?", and then "Compiled run first" (^ticket(722)). The program's run was most of a small edit on that slide (^report(47), experiment 2; ^report(46), compiling the run); this is the first measurement of it compiled. In Node on the laptop, not yet in the app.
- What it took. The program is
witch.hz, 2,222 lines with 53 livelit uses (sliders, choices, polygons, the picture). hazel-jit already compiled nearly all of it. It refused a livelit use and a splice; the interpreter steps through both to what they hold (Transition), so hazel-jit now compiles each as what it holds. And one builtin a livelit's expansion calls,%splice_value, which finds a spliced value inside a model, got a JS version. - What it still does not compile, and the run never reaches. 52 holes, one quotation and one
undefined. A new survey mode counts them: each becomes a stub that throws if the run evaluates it (HazelJit.survey), and none throws. - The same values. The run's value is the interpreter's. And for a fuller check, the program's last expression was replaced by a tuple of it and every name its top-level lets bind, then run both ways: of 194 names, the 124 that hold data are the same in both (every slider's value, every part of the outfit, the picture), none differs, and the other 70 are functions or modules, which have no JS form to compare.
- The times. Three runs of the CLI's new
hazel jitcommand, each a mean over five runs after one to warm: the interpreter 502 to 529 ms, compiled 4.2 to 5.0 ms. The compiled run makes its JS anew each time, parsing its 176 KB of source, so a run with the code made once would be quicker still. Compiling the program takes hazel-jit 38 to 44 ms. Two caveats: the CLI is a development build (js_of_ocaml without inlining), so the interpreter may run slower here than in the app's release build; and the run here records no probes, where the app's worker records the slide's 108. - A slider's step, compiled once (17:53,
ab4a41e587). Elaborated as the app elaborates a slide, with each livelit's view called in the run (UserLivelit's view fold-in; the runs above have no views in them, since the CLI probes no projector), the interpreter takes 653 ms and the compiled run 7.2 ms. Then a step: the neck slider's model from 28 to 30. The interpreter runs the program so changed in 684 ms. Compiled once, with each literal model a parameter (HazelJit.params_on: the model compiles as$R.param(its id, the literal), each view call as$R.sample(the projector's id, the answer)), the step is a call with the new model: 2.3 to 2.5 ms, all 53 views' answers recorded. The same 124 values both ways after the step, and the step changed exactly the 8 it should:neck_length,neck,neck_at,face_at,hair_at,hat,outfit, anddrawn, the picture. (hazel jit --views --check --step neck.) - What a step would still pay today. Before any run, the CLI parses this program in 1.26 to 1.33 s and checks and elaborates it in 2.80 to 2.86 s. The app does not redo all of that for a slider's step, but it does redo the syntax and statics it can (
^ticket(721)); a step that changes only a livelit's model should pay none of it.
Next, in the app (what next, step 10): on a slider's step, call the slide's compiled program with the new model, and draw each livelit whose answer changed straight from its JS, the route compiled views already take; then timed in Firefox on GitHub's machines. Probes and the editor's highlights stay with the interpreter, run after. The data: 045/data/run-witch.json; the code at 0926a80c74 on trial/694-view-jit.
Where the time goes now#
The trip back is 79% of a compiled render: 0.27 ms of 0.34. The trip in takes 0.037 ms (11%) and the compiled view 0.043 ms (13%), so the way back costs about 7 times the way in, and about 6 times the view itself. Each step was timed alone, warm, and the three add up to 0.35 ms, about the whole render's time.
Why the way back costs more: there is more of it. The compiled view needs its model as JS values, so the model is converted on the way in; and the app draws a Hazel value, so the view's whole answer, its command and every node of its Html tree, is made again as a Hazel term on the way out. The slide's model is 115 nodes (cons cells, tuples, constructors and the numbers and strings in them); the answer is 861, about 7.5 times as many, with 7 event handlers, none of them capturing a value. Per node, the way back costs a little more: 0.31 µs a node, against 0.32 µs a node in.
Making ids is now 16% of the way back; yesterday it was almost half. Each Hazel term carries an id, and each term the way back makes gets a fresh one. The trip back makes 906 terms; 906 fresh ids now take 0.044 ms (0.05 µs each), and with no ids to make at all, every term given one shared id, the trip back would take 0.20 ms instead of 0.27. How, under cheaper ids.
Update, 2026-11-07 07:40: this paragraph said each fresh id was a random UUID. It is not: Id.mk was already a counter, with a random tail drawn once per session. What cost was building each id's 16 characters out of eight concatenations; that is what changed.
What else could make the way back cheaper: keep the answer in JS until it is drawn, so the part of the app that turns it into the page's elements reads JS values directly; convert lazily, only the parts that are read; or keep what did not change since the last render. The first and the last are tried below.
Three ideas for the way back#
All three work, and the one that helps most is not coming back at all. Matthew asked, on 2026-11-07: can the ids be cheaper; can a view's answer, which is only drawn, skip Hazel altogether; and can the answer come back as a diff against the last one? Cheaper ids are done, in Id.mk itself. A view's answer drawn straight from its JS makes the same page in about a third of the time, and a whole render, model to page, is then about 37 times faster than on the interpreter. A diff carries one changed point of 1,000 back 111 times faster than carrying the whole list. Both are in the app since 11-07 (^ticket(714), ^ticket(715)): a view's answer drawn from its JS, and an update compiled, its new model back as a diff, so an event that took about 80.7 ms (the definition evaluated, then its update) takes 0.050 ms. What is not there yet is a diff applied in time sublinear in a long list: that needs lists to be trees on both sides (^ticket(710)).
Cheaper ids: done#
Making 906 ids now takes 0.044 ms; made as before, in the same session, 0.29 ms (between 2.5 and 6 times as long over today's runs). An id is 16 bytes: a counter in the first eight, then eight random bytes drawn once per session. Under js_of_ocaml a string is a JS string, and Id.mk built each id by concatenating one-byte strings, eight concatenations an id. Bytes 1 to 15 are the same for 256 ids in a row, so now they are made once for each 256, and each id is one concatenation. The ids are the same as before, byte for byte. Nothing downstream of the way back reads the ids of a view's answer; the call memo already puts its keys in a form with no ids.
This is not hazel-jit's alone. The interpreter mints an id for every term it rebuilds and every binding it makes, hundreds of thousands a run, through the same Id.mk. The string-built ids came in with a commit of 2026-10-10 on #2635's line (7a5667e6f2), which dev does not have yet.
A view's answer, never made Hazel#
Yes: drawn straight from its JS, a view's answer makes the same virtual DOM in 0.30 ms, against 0.71 ms through Hazel. Through Hazel, the answer is carried back (0.27 ms), its command run down to its Html (0.0007 ms), and the Html drawn by HazelDOM into the virtual DOM (0.44 ms), which costs more than the trip back. A prototype, HazelJitDOM, reads the compiled view's JS answer and makes the virtual DOM nodes directly. Model to page, a render is then 0.38 ms, against 0.79 ms through Hazel and 14.1 ms on the interpreter.
- The same page. The two drawings, made into virtual-dom's own objects, are equal field by field (10,861 characters of them as JSON), event handlers compared only as functions. A quick test checks it.
- Handlers cross only when their event comes. A click's message is carried back as Hazel when the click comes, not on every render. A handler that takes a payload (a mouse position, an input's text) is the compiled JS function: it is called in JS on the payload, and only the message it answers is carried back. Through Hazel, the interpreter applies the Hazel handler instead.
- What it covers (since 889271cfbb,
^ticket(714)): every form HazelDOM draws. Every view command:Pure;Bind, its continuation the compiled JS function, called in JS;Editor,EvalSpliceandResultView, the small values they hand on made in JS, as the app'sViewCmdRunnermakes them in Hazel. (11-07 11:40, bcec455abc: until then they were made in Hazel and carried in, and the splice's value, carried back to Hazel only to be wrapped and carried in again, was 20 of the 21 ms of the Witch slide's picture drawn from JS; now 2.6 ms, the trip in included 3.4, against 155 on the interpreter, in the Hazel test:^report(46), one frame. In the app, in Firefox: 88 ms a render on the interpreter until 15:05, when a drawing before the program's run stopped giving up on the view's definition (cb560ebb86); now 3.0 ms compiled: in Firefox.) Every element (text and numbers, splices and their results, inputs, text areas, options, images, custom and SVG elements, and the elements that take attributes and children); every attribute; every handler, its payload (a mouse event, a key, a wheel, an input's text) made as HazelDOM makes it. What is not well formed, and apps (which commit to the program's syntax, not to a model), go through Hazel, where HazelDOM shows them as before. A second quick test draws a view of many forms (aBind, every kind of handler, a table, a select, a link, SVG) both ways: the same virtual DOM. - In the app (
^ticket(714)): a view's answer is kept two ways, its JS and its Hazel Html, the Html made only when something asks for it. The two places that evaluate a view draw from its JS: the render at an event (before the run catches up), and the render before the run has any sample. That render's Html is made only to compare it with a sample, once the models agree. Update, 2026-11-07 10:20: this item said three places would draw from JS, the live samples among them. The live samples are the interpreter's own values, from the program's run, so they are drawn by HazelDOM as before.
The way back as a diff (^ticket(709))#
A diff of the last answer and the new one, applied to the last Hazel value, carries one changed point of 1,000 back in 0.052 ms, against 5.76 ms whole. The diff is computed in JS and stops wherever the two answers share an object: compiled code builds its answer functionally, so what it did not touch (a list's tail after the element it replaced, a field it did not set) is the same object as before. The patch keeps every part of the old Hazel value that did not change: in the test, 999 of the 1,000 elements are the old ones.
An update's new model, back as a diff, in the app (^ticket(715)). On an event, a livelit's update now runs compiled, and when it answers Pure(new model), the new model comes back as a diff against the model it was given, read by the definition's Model type. The Polygons livelit's own update, on its slide's model, on twelve actions, gives the interpreter's model every time, and every shape it did not touch comes back as the old one. The bigger saving is what is no longer done: on every event the app evaluated the whole definition, 68.3 to 83.3 ms for the Polygons livelit, before applying its update (0.67 to 5.18 ms). Compiled, with the diff, an event's update takes 0.023 to 0.081 ms; a step of a drag, 80.7 ms before, 0.050 ms now. The update alone is 16 to 134 times faster. Medians of 30 runs each:
| Action | As the app ran it (the definition, then the update), ms | The update alone, ms | Compiled, the model back as a diff, ms | Shapes kept |
|---|---|---|---|---|
Next | 72.8 | 1.11 | 0.031 | 2 of 2 |
Prev | 76.3 | 0.89 | 0.023 | 2 of 2 |
NewPolygon | 78.3 | 0.96 | 0.028 | 2 of 3 |
NewPath | 68.3 | 0.88 | 0.026 | 2 of 3 |
Toggle | 68.9 | 1.26 | 0.081 | 1 of 2 |
Remove | 72.4 | 0.67 | 0.028 | 1 of 1 |
SetName | 70.2 | 0.99 | 0.023 | 1 of 2 |
SetFill | 71.7 | 0.97 | 0.030 | 1 of 2 |
SetWidth | 76.2 | 1.17 | 0.032 | 1 of 2 |
Down | 83.3 | 5.18 | 0.039 | 1 of 2 |
MoveTo (holding a point) | 80.7 | 4.22 | 0.050 | 1 of 2 |
Up (holding a point) | 74.8 | 0.90 | 0.028 | 2 of 2 |
How often is it worth it? For a view, now, never: its answer need not come back at all. What must come back is what lives in the program: the model a livelit's update returns, and the answer of any compiled function the interpreter calls. On today's slides those are small: the Polygons model is 115 nodes, and carrying it in takes 0.037 ms. Carrying back costs about 0.3 µs a node for the Polygons view's answer, and about 5.76 µs a point for a list of labeled points with no type to read them by, so a whole list of 1,000 points is 5.76 ms of a 16.7 ms frame. A diff pays once a model holds hundreds of elements: a drawing with many points, a table, a long document.
The diff language is small. A diff is one of:
- the same: nothing to carry;
- new: a value carried back whole, at the type expected there;
- a constructor's argument, changed by a diff;
- a tuple's components, those that changed, each by a diff;
- a list's edits, in order from its head: keep the next n elements, change the next by a diff, insert one, drop one, append a list at the end, or end here. After the last edit, the rest is the old list's own tail.
The common cases, each a quick test that patching gives what carrying back whole gives: one element changed in the middle; one put at the head; the head dropped; one put at the end; a list cut short; a list emptied; nothing changed; a constructor's argument changed; one labeled field set and the other kept; floats in a list. Putting one at the head, or dropping the head, is found without looking at the rest: the new list's tail is the old list, or the other way round.
Sublinear, not yet. The diff and the patch both walk a list from its head to the change. Each step is cheap (a few nanoseconds to compare two objects in JS, a few tens to keep an element in OCaml), so one change of 10,000 points costs 0.11 ms, but it grows with the change's position, not with the size of the change. Time in log n needs a long list to be a tree on both sides of the boundary: in the compiled code, instead of cons cells, and in Hazel's values, instead of a list literal's flat list. Adapton-style level trees, as in Fumola, give every list one shape however it was built, so a change touches one path. That is a change to how Hazel represents lists: ^ticket(710), for a talk first.
Two costs found on the way back, and fixed. A whole number came back through Printf and big-integer parsing, about 2 µs each; one that fits an int is now converted directly. And a tuple's labels were told from its indices by raising and catching an exception for each label. With both, a list of 1,000 labeled points carries back whole in 2.2 to 5.6 ms over today's runs, against 8.2 ms before.
In a browser#
On the six polygon slides in Firefox, from a release build, compiled views draw exactly what the interpreter draws, as loaded and after the same clicks, and every click's change shows sooner: 77 ms against 124 on Polygons, 319 against 368 on Full Body. A step of a drag on Polygons shows in 36 ms against 62. The gain is smaller than the livelit's own (an event's update from about 80 ms to 0.05): what is left is the rest of the page's work after an event, the program's text changed and analysed again and the editor drawn, which is now most of it (^ticket(716), ^ticket(717)).
These times end at the clicked livelit's own change (added 11-08). A click that changes a livelit's model changes the program, and the program then runs in Hazel's worker; what the run changes, on the Witch slide its picture, shows only after the run and its answer come back. Timed on the Witch in ^report(46), with a slider: the slider's own change came 0.3 to 0.4 s after the edit, and the picture's 5.3 to 8.8 s after it, compiled or not (a small edit). The milliseconds below are that first part; the seconds are not in them.
| Slide | Livelits | A click, compiled, ms | A click, on the interpreter, ms | Loaded fresh, compiled / on the interpreter, s |
|---|---|---|---|---|
| Polygons | 1 | 77 | 124 | 5.4 / 4.6 |
| Polygon Viewer | 1 | 83 | 73 | 4.5 / 4.5 |
| Full Body | 43 | 319 | 368 | 12.9 / 21.3 |
| the Witch | 53 | 304 | 354 | 19.2 / 22.2 |
| Hands | 17 | 168 | 213 | 14.9 / 14.5 |
| Scenes | 10 | 122 | 147 | 13.6 / 14.8 |
- How.
scripts/jit_slides.py, on the livelit deck's own Firefox harness, in a workflow on GitHub's machines (jit.yml; the laptop has no browser to drive): each slide loaded fresh with Compiled views on (?jit=on, a link parameter added for this) and off; every livelit's drawing compared as data; then the first livelit's buttons clicked in turn, six clicks, each landing before the next, and on Polygons a drag; each event timed from its capture to the first change of the page under its livelit. The medians are of those clicks (one on Polygon Viewer, whose only button answered once). Firefox only; Chrome is not tried yet. - What ran how, as the page counted it with Compiled views on: on Full Body, 1,219 drawings from JS, 62 views and 6 updates compiled, 9 views on the interpreter; on the Witch, 1,613, 69 and 6, 9. The views left on the interpreter are those whose model, before the program's first run, still holds its splices' code, which the interpreter cannot draw either: it shows them waiting until the run.
- Found on the way, and fixed. The first run found 20 renders of Polygons on the interpreter: a model read from the program's text writes
-1as a minus before 1, which the trip in did not carry (567c8d9620). And the stored setting never reached the switch the views read at load, so Compiled views off did not survive a reload (433a216bd7). The first run's drawings also differed after the clicks, because the interpreter's widget, slower, showed a button half drawn and the next click found another one; each click now lands before the next.
Beside JS written by hand#
On ^report(43)'s Petri net rule, hazel-jit's code takes about 1.8 times as long a call as the JS the example's author wrote, and 225 times less than Hazel's interpreter. The example's own rule files (its preamble, Common, Rules and Main), put together as hazeljs puts them, compile whole; the compiled on_event and the example's hand-written jsHandler answer the same 2,000 events and end on the same marking. In plain Node, the median call is 1.59 µs compiled and 0.90 µs by hand (the fastest calls 0.66 and 0.71 µs); the interpreter, as the app applies a handler (Evaluator.evaluate_open), 358 µs. ^report(43) measured about 3.4 ms a call through hazeljs, which carries the values across as well (^ticket(699)).
ReScript's and Melange's compilers do better than both, 0.55 and 0.46 µs a call on the same rule written in their languages, and the reasons are in what they write: a function's arguments as JS arguments, not an array; === on Ints; loops for tail calls; options unboxed. ^report(46), beside this one, puts the five compilers' code side by side, with what it suggests for hazel-jit's next version (^ticket(698)).
On the way: applying the same handler with Evaluator.evaluate, which closes its answer, took 63 ms a call, 175 times evaluate_open: closing copies what the closure closes over into the answer. The app applies handlers open; a library that calls Hazel functions should too.
How it works#
hazel-jit turns an elaborated Hazel term into the source text of a JS function, once, and the browser's own JavaScript engine runs it. HazelJit.compile_full, in src/language/dynamics/HazelJit.re, walks the term Hazel's elaborator makes (the same term the interpreter runs) and writes JS, which is made into a function with new Function.
LivelitProj.eval_view since 4c0ed173f1.Hazel's values in JS. Numbers are JS numbers (so Int overflow and rounding are JS's: the best case first); a list is cons cells ({h, t}, with null for the empty list), so :: takes constant time; a tuple is a JS array, each labeled component also a property by its label, so p.x is a property read; a constructor is {c: "Name", v: payload}; a function is a JS function of one argument; a module is a labeled value of its members, in order. A let or a case binds JS variables declared at the top of the enclosing function, each with a name of its own, so the code is expressions all the way down and no closure is made for a let. Types, ascriptions and parentheses are erased. Builtins written in OCaml have JS versions by name; builtins written in Hazel are compiled once, before the program's code.
What it does not compile, it says why, and that code runs on the interpreter: holes, tests, quotations and splices, projectors, partial application. A member of a module that does not compile (a livelit's expand, say) becomes a stub that says why if it is called, so the rest of the module still compiles.
The trip in and out (HazelJitValue.re). Into JS is direct: each Hazel value as above. Back is harder, in two ways. A JS number does not say whether it was an Int or a Float. So the compiler records each constructor's type as the elaborator gave it, and the type aliases the term defines, and the way back reads a number by the type expected where it sits: a constructor's argument at its argument's type, through aliases. A whole number with no type to go by comes back an Int. A function in the answer, an event handler like Attr.on_input(fun v -> SetName(v)), has to come back as a Hazel function the interpreter can apply when the event comes. So every compiled function carries the index of the Hazel function it was compiled from and the values it captured, and comes back as that Hazel function closed over those values (the compiler notes, for each function, which outer variables its body uses).
In the app#
Livelits' views and updates run compiled in the app, behind a setting that is on by default, and the interpreter runs whatever hazel-jit cannot. LivelitProj compiles each livelit definition once (kept by its term). A view is called compiled on the model, and its answer drawn straight from its JS (HazelJitDOM), at an event and before the program's run has a sample; the samples, the interpreter's own values, are drawn by HazelDOM as always (^ticket(714)). An event's update is called compiled too, and its new model comes back as a diff against the model it was given (^ticket(715)). When the compiled code answers, the definition is not evaluated by the interpreter at all.
Update, 2026-11-07 10:20: until 889271cfbb, a compiled view's answer was carried back as Hazel and drawn by HazelDOM, and every event evaluated the definition to run its update on the interpreter.
- The setting is Compiled views, in the settings menu, on by default; off, every view runs on the interpreter as before.
- When it cannot (a form it does not compile, a recursive function in the answer, an error while running), the view runs on the interpreter, the browser's console says why, once for each reason, and that definition is not tried again.
- Checked in a browser (
^ticket(716)): Firefox, on the six polygon slides, the same drawings both ways.
Does it give the same answers#
Yes, on every program tested, and the tests catch a broken compiler. test/Test_HazelJit.re has 52 tests, all passing at 4c0ed173f1 (37 at 7a7151443c): 24 small programs whose compiled results agree with the interpreter's (arithmetic, floats, recursion, tuples and labels, lists, constructors, options, strings, equality, closures, nested patterns, modules, builtins, and the Petri net rule); the Polygons slide's view, its whole Html tree equal to the interpreter's; values carried back as the interpreter's own (whole floats staying floats, a type alias, labels with no type, the Petri net's commands); a view and an event handler through the trip in and out, the handler then applied by the interpreter; and the Polygons view through it. Breaking the compiler on purpose fails them: compiling Int's + as - failed 5 of 18 programs; reading numbers without their types failed 3 tests; dropping the values a function captures failed the handler test. Since 11-07, also: a diff applied to the last value gives the new value carried back whole (ten cases); the Polygons view and a view of many forms, drawn straight from JS, are HazelDOM's drawings; and the Polygons update, compiled with its model back as a diff, gives the interpreter's model on twelve actions.
What it does not do yet#
- Numbers are JS's. Int overflow, division and rounding may differ from Hazel's: allowed for the best case, to be decided before anything ships.
- The call memo, probes and the step budget do not apply inside a compiled view: no reuse of repeated calls between renders, no probe's samples from inside the view, and a deep recursion is JS's stack, not Hazel's step limit (a stack overflow falls back to the interpreter).
- A recursive function in a view's answer is refused (a Hazel closure cannot hold a cycle as a plain value), and so is a JS builtin passed as a handler.
- Values made again on the way back get new ids; whether anything in the app depends on a model's ids surviving the trip has not been checked.
- The whole run is compiled only in Node. The app still runs a slide's program on the interpreter in its worker (the whole run, compiled).
- One browser. Compiled views are tried in Firefox on GitHub's machines (in a browser), not yet in Chrome or Safari, and not by hand on the laptop.
- Two slides measured in Node. Polygons here, and the Witch's picture in
^report(46)(a view: in the Hazel test a frame 155 ms on the interpreter, 6 fps, and 3.4 ms compiled, 290 fps, as if updating the state were free; in the app, in Firefox, 3.0 ms compiled, about 330 fps, against 87 ms on the interpreter, 11 fps; an edit on the slide shows in the picture 5.3 to 8.8 s later, nearly all of it the program's run and its answer, a small edit). Full Body, Hands and the Scenes slide's woods, the slides with the most polygons, come next; in Firefox, all six polygon slides draw the same both ways (in a browser).
For Wednesday's meeting#
This is item 2 of the agenda for Wednesday 2026-11-11 at 10:00 Mountain (^report(42), item 2), which asks: is the best-case framing right, numbers allowed to differ? Is compiling the view alone the right first cut? What would make a convincing comparison: which slides, and against what (the interpreter with its call memo, or Messed Up Dress Up's hand-written JS)? Item 3 asks how BuckleScript, ReScript and Melange bear on it (^ticket(698)).
Since then, ^report(46)'s small edit bears on the second question: on the Witch slide, an edit shows in the picture 5.3 to 8.8 s later, and compiling the picture's view saves 84 ms of it. The program's run and its answer are the rest.
What next#
- Draw a view's answer straight from its JS in the app:
eval_viewand the three places that draw a livelit's view, and the rest of HazelDOM's forms. Done, 11-07 (^ticket(714), 889271cfbb): the two places that evaluate a view (the live samples are the interpreter's), and every form; tried in a browser in step 4. - Carry a livelit update's model back as a diff against the model it was given. Done, 11-07 (
^ticket(715), 4c0ed173f1): the update compiled, its model back as a diff; an event's update 0.023 to 0.081 ms, against 68.3 to 83.3 ms. - Talk through sublinear diffs, lists as trees on both sides (
^ticket(710)). - Try it in the app on the polygon slides, with the setting on and off, and check the drawings are the same. Done, 11-07 (
^ticket(716)): in a browser, the same drawings on all six slides, before and after the same clicks. - Time it in browsers on GitHub's machines, from a release build, on Polygons, Polygon Viewer, Full Body, the Witch, Hands and the woods. Done in Firefox, 11-07 (
^ticket(717)): a click's change 77 against 124 ms on Polygons, 319 against 368 on Full Body; Chrome next. A whole edit, the program's run with it, timed on the Witch in^report(46): 5.3 to 8.8 s; the other slides next. - Compare the compiled Petri net rule with the hand-written one of
^report(43), the code side by side and the time per call (^ticket(699)). Done, 11-07 (28fc073ed5): beside JS written by hand, 1.59 µs a call against 0.90; the code side by side in^report(46). - Put a short note beside this one on how BuckleScript, ReScript and Melange compile the same small program (
^ticket(698)). Done, 11-07:^report(46), the Petri rule compiled by ReScript, Melange, js_of_ocaml and hazel-jit, and five changes it suggests for hazel-jit. - Compile more than views: the program's run on a slide, so that under ideal conditions (no holes or errors, no probes shown, an edit to livelit models only, a run within a budget) it runs on the main thread and each livelit's model goes straight into its compiled view as a JS value. Speculated in
^report(46)(compiling the run), after it found the trip between the worker and the page, not the evaluator, to be most of a small edit; the cheaper cuts to that trip come first (what the answer holds). Begun 11-08 (^ticket(722)): the Witch slide's whole program compiles and runs in 4.2 to 5.0 ms in Node, the same values as the interpreter's 502 to 529 ms (the whole run, compiled); in the app next.
How this was put together#
- The code is on
trial/694-view-jitin hazelgrove/hazel, a branch off #2635 at ab7273c984: slice 1, the compiler and its agreement tests (8c5cd80a29, 14b006d008); slice 2, modules and the Polygons view (79fe5bbebf); slice 3, the trip in and out andeval_view(7a7151443c). All are Claude's, on Matthew's word, 2026-11-06. - The timings are
Test_HazelJit's slow tests, run in Node 22.23.2 (node --stack-size=30000) from a development build of 4c0ed173f1 on a Framework 13 laptop (Intel Core i7-1370P), at 10:09 MST on 2026-11-07: each program 30 times on the interpreter and 30 times compiled, after compiling once, timed withperformance.now; the median is the upper middle of the 30 sorted runs, as the tests print it. The interpreter's time for a render is the evaluated view applied to the evaluated model, without the app's call memo. For the Polygons render, every series runs 10 times untimed first, but for the first 30 renders, timed cold; its three steps are timed alone (the trip in on the slide's model, the call on that model already in JS, the trip back on one answer of the call), and the test counts what each trip carries and times as many fresh ids as the trip back makes, made both ways. It then times what the app does with the Hazel answer (the command, HazelDOM's drawing,to_raw) and the same drawing straight from JS, and compares the two. The update timings are the Polygons livelit's own update on its slide's model, twelve actions, each three ways (as the app ran it: the definition, elaborated once, evaluated and then its update applied; the update alone; compiled, its model back as a diff), 30 times after 5 untimed. The diff timings are a list of 10 to 10,000 labeled points, the middle one replaced by a compiled Hazel function, each 15 times after 5 untimed. The version of 2026-11-06 19:01 used a session at 18:56 at 810df240ea, before the cheaper ids. The first version of this report used a session at 16:08 at 7a7151443c, with no warm-up (see the update under how much faster). - The Polygons view is the livelit of
hazel-programs/docs/livelits/playground/polygons.hz, applied to the model the slide's ownshirtgives it, cut from the slide's text by the test. - The page is made by
045/gen045.pyfrom045/body.htmland045/data/timings.json; its figures are drawn from those numbers. - 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 in045/data/.