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#

  1. 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.
  2. 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.
  3. The same rule, compiled: side by side.
  4. How each lays out values: lists, records, constructors, options, arguments.
  5. 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.
  6. ^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.
  7. 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.
  8. 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.
  9. 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%.
  10. What the worker's answer holds: mostly one binding in one probe sample's environment, the slide's Drawing module written out whole, 3.8 MB of 5.4; four cuts, none of them a compiler.
  11. 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.
  12. 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.
  13. 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.
  14. How each lays out the view's values: shapes, vertices, Html, numbers formatted, the walks.
  15. What holds across both: the compilers differ in calls, matches and layouts, and converge where a builtin does the work.
  16. What it suggests for hazel-jit: five changes to its output, in the order they would help.
  17. Could any of them be used: not as hazel-jit's back end; their layouts and optimizations, yes.
  18. What next.
  19. 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.
  20. How this was made.

What each is#

Three compilers from one family, and one from another.

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.

One call of the Petri rule, µs: the median of 2,000 calls (the median of five runs), and the fastestMelange0.46 µs (fastest 0.34)ReScript0.55 µs (fastest 0.38)js_of_ocaml0.71 µs (fastest 0.40)hand-written JS0.90 µs (fastest 0.71)hazel-jit1.59 µs (fastest 0.66)00.511.52Hazel's interpreter, on the same rule: 358 µs a call, off this scale.
Node 22.23.2, one laptop; each bar the median of five runs' medians, each tick the fastest single call. js_of_ocaml's in a process of its own, the rest in one.
The same calls with Hazel's interpreter among them, µs a call, a log scale (each step to the right is ten times as long)0.1 µs1 µs10 µs100 µs1000 µsMelange0.46 µsReScript0.55 µsjs_of_ocaml0.71 µshand-written JS0.90 µshazel-jit1.59 µsthe interpreter358 µs
The medians of the chart above, and the interpreter's (358 µs a call, Evaluator.evaluate_open, 200 calls). On this scale the compilers' bars differ by less than one step; the interpreter's is more than two steps longer than hazel-jit's.
Compilerµs a call, medianthe fastest call
Melange 5.0.00.460.35
ReScript 12.3.10.550.38
js_of_ocaml 5.9.1 (the rule inlined into the loop)0.710.40
JS written by hand (the example's jsHandler)0.900.71
hazel-jit1.590.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.

Valuehazel-jitReScriptMelangejs_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 tuplean array, each label also a propertyan object by field namean object by field namean 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"00
Some(t); None{c: "Some", v: t}; {c: "None"}t; undefinedt; undefined[0, t]; 0
a function of twoone argument, an array [a, b]f(a, b)f(a, b)f(a, b)
i == 0 on Ints$R.eq(i, 0)i === 0i === 00 === i
a tail calla calla while loopa while loopa 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.

The Witch's picture: the view's own work, ms (and frames a second, as if updating the state were free)hand-written JShand-written JS: 0.168 ms0.17 ms (6,000 fps)MelangeMelange: 0.173 ms0.17 ms (5,800 fps)ReScriptReScript: 0.177 ms0.18 ms (5,600 fps)js_of_ocaml, toFixedjs_of_ocaml, toFixed: 0.189 ms0.19 ms (5,300 fps)hazel-jithazel-jit: 0.193 ms0.19 ms (5,200 fps)js_of_ocaml, Printfjs_of_ocaml, Printf: 1.010 ms1.01 ms (990 fps)toFixed alone, 1,985 calls: 0.14 ms00.250.50.751
Node 22.23.2, one laptop: from the picture, as each language holds it, to the Html the view returns, as each holds it. Each bar the median of three runs, each run the median of five rounds of 200 calls; js_of_ocaml's in a process of its own, five runs. The dashed line is the same 1,985 numbers formatted with toFixed(1) and nothing else.
Versionms, the view's work (median)fps, if updating the state were freethe fastest call, ms
JS written by hand0.176,0000.16
Melange 5.0.00.175,8000.17
ReScript 12.3.10.185,6000.17
js_of_ocaml 5.9.1, numbers by JS's toFixed0.195,3000.18
hazel-jit0.195,2000.19
js_of_ocaml 5.9.1, numbers by OCaml's Printf.sprintf "%.1f"1.019900.91
Hazel's interpreter (in the app's terms, below)1556

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.

One frame of the Witch's picture, ms on a log scale, against a 60 fps frame (16.7 ms)0.1 ms1 ms10 ms100 ms60 fps, 16.7 msthe interpreterits view, command and drawingthe interpreter, its view, command and drawing: 155.43 ms155 ms (6 fps)hazel-jit, the appmodel in, view, drawinghazel-jit, the app, model in, view, drawing: 3.45 ms3.45 ms (290 fps)hazel-jit's viewin the app's processhazel-jit's view, in the app's process: 1.16 ms1.16 ms (860 fps)hazel-jit's viewin a Node process alonehazel-jit's view, in a Node process alone: 0.19 ms0.19 ms (5,200 fps)
The first three in the Hazel test's process (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).

The Witch slide's views in Firefox, in the app, ms a render on a log scale, against a 60 fps frame (16.7 ms)0.1 ms1 ms10 ms100 ms60 fps, 16.7 msDrawing.picture, before the fix: interpreterDrawing.picture before the fix: interpreter: 90.000 ms90 ms (11 fps)Drawing.picture, interpreterDrawing.picture interpreter: 87.000 ms87 ms (11 fps)compiled compiled: 3.000 ms3.00 ms (330 fps)Drawing.polygons 1, interpreterDrawing.polygons 1 interpreter: 26.000 ms26 ms (38 fps)compiled compiled: 0.373 ms0.37 ms (2,700 fps)Drawing.polygons 2, interpreterDrawing.polygons 2 interpreter: 26.000 ms26 ms (38 fps)compiled compiled: 0.293 ms0.29 ms (3,400 fps)Drawing.polygons 3, interpreterDrawing.polygons 3 interpreter: 27.000 ms27 ms (37 fps)compiled compiled: 0.333 ms0.33 ms (3,000 fps)Drawing.polygons 4, interpreterDrawing.polygons 4 interpreter: 29.000 ms29 ms (34 fps)compiled compiled: 0.453 ms0.45 ms (2,200 fps)
Firefox on GitHub's machines, a release build (cb560ebb86, run 42417643353; the first bar from 25226a8f79, run 42409125640, before the fix): each view run again on its last model, bypassing the app's memo, 15 samples, each a loop of calls at least 25 ms long. The frames a second are each time alone, as if updating the state were free.

^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?

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.

A small edit on the Witch slide, in Firefox: time from the edit, on a log scale, against a 60 fps frame (16.7 ms)1 ms10 ms100 ms1 s10 s60 fpsthe slider, redrawnthe page's first change, after the edit's own workthe slider, redrawn the page's first change, after the edit's own work: 372.0 ms0.37 s (22 frames)the picture shows the editcompiled views on (median of 4)the picture shows the edit compiled views on (median of 4): 7965.0 ms8.0 s (478 frames)compiled views off (median of 4) compiled views off (median of 4): 7033.0 ms7.0 s (422 frames)of which, the picture's viewon the interpreterof which, the picture's view on the interpreter: 87.0 ms87 ms (11 fps)compiled compiled: 3.0 ms3.00 ms (330 fps)
Firefox on GitHub's machines, a release build (2a5b2ffefa, run 42441046851): four edits each way (the neck slider 2 steps, there and back; the arms slider 10, there and back), each from the edit to the first change of the page under the slider, and to the picture's first new drawing. Frames are counted at 60 fps. The picture's view is the bench of in Firefox (cb560ebb86, run 42417643353), one render alone.

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".)

Two small edits on the Witch slide, in Firefox: where the time goes, seconds from the editneck, 28 to 30main threadupdate: 8 ms at 0.01 scalculate: 29 ms at 0.02 sview: 100 ms at 0.05 supdate: 7 ms at 0.15 scalculate: 48 ms at 0.16 sview: 127 ms at 0.20 safter display: 3 ms at 0.34 sworker message decoded: 3 ms at 0.35 sworker message decoded: 8 ms at 0.36 scalculate: 70 ms at 0.41 sview: 108 ms at 0.48 safter display: 2 ms at 1.13 supdate: 47 ms at 1.51 scalculate: 8 ms at 1.56 sview: 28 ms at 1.57 safter display: 2 ms at 3.88 sworker message decoded: 2387 ms at 6.09 s, 5.4 MBdecode 5.4 MB, 2.4 scalculate: 43 ms at 8.48 sview: 231 ms at 8.53 safter display: 3 ms at 8.77 sworkerthe worker, from the request to its result: 5.9 s, of which its evaluator 1.2 srequest 10 KB; its evaluator 1.2 s in all; the rest, making and sending 5.4 MB backfirst redraw, 0.3 sthe picture, 8.8 sarms (up / down), 55 to 65main threadupdate: 7 ms at 0.01 scalculate: 18 ms at 0.01 sview: 102 ms at 0.03 supdate: 9 ms at 0.13 scalculate: 42 ms at 0.14 sview: 113 ms at 0.18 safter display: 2 ms at 0.31 sworker message decoded: 4 ms at 0.32 sworker message decoded: 11 ms at 0.33 scalculate: 70 ms at 0.39 sview: 114 ms at 0.46 supdate: 52 ms at 1.49 scalculate: 8 ms at 1.54 sview: 24 ms at 1.54 sworker message decoded: 1335 ms at 3.75 s, 4.4 MBdecode 4.4 MB, 1.3 scalculate: 16 ms at 5.09 sview: 163 ms at 5.11 safter display: 2 ms at 5.50 safter display: 2 ms at 7.05 sworker message decoded: 2092 ms at 8.06 s, 5.4 MBdecode 5.4 MB, 2.1 scalculate: 17 ms at 10.15 sview: 156 ms at 10.17 sworkerthe worker, from the request to its result: 7.9 s, of which its evaluator 1.6 srequest 10 KB; its evaluator 1.6 s in all; the rest, making and sending 5.4 MB backfirst redraw, 0.3 sthe picture, 5.3 s0 s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s
Firefox on GitHub's machines, a release build (3ed2f31844, run 42445268287), Compiled views on, the page's own timeline (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.
the editcompiled viewsits request posted, sthe worker, from the request to the message that carries the picture, sof which its evaluator (the whole run), sthat messagedecoding it on the main thread, sthen the page, to the picture, sthe picture shows the edit, sthe main thread busy until then, s
neck 28 to 30on0.205.91.2the result, 5.4 MB2.40.298.83.4
neck 30 to 28on0.274.71.7the result, 5.4 MB2.10.237.43.2
arms (up / down) 55 to 65on0.183.61.6a stream, 4.4 MB1.30.195.32.1
arms (up / down) 65 to 55on0.224.42.6a stream, 4.4 MB1.50.236.32.5
neck 28 to 30off0.214.71.6the result, 5.4 MB2.40.567.93.6
neck 30 to 28off0.224.11.6the result, 5.4 MB2.20.426.93.3
arms (up / down) 55 to 65off0.183.51.6a stream, 4.4 MB1.40.415.52.4
arms (up / down) 65 to 55off0.233.82.5a stream, 4.4 MB1.40.415.92.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".)

A small edit on the Witch slide, in Firefox: the trip between the worker and the page (orange) against the evaluator (blue)neck 28 to 30, views onthe page, before the request: 0.20 sthe worker's evaluator: 1.14 s1.1 sthe worker's other work: 0.25 sthe result shared (worker): 1.45 s1.4 sencoded (worker): 1.23 s1.2 sdecoded (main thread): 1.90 s1.9 sthe page, to the picture: 0.24 sneck 30 to 28, views onthe page, before the request: 0.20 sthe worker's evaluator: 1.12 s1.1 sthe worker's other work: 0.11 sthe result shared (worker): 1.47 s1.5 sencoded (worker): 1.14 s1.1 sdecoded (main thread): 1.37 s1.4 sthe page, to the picture: 0.21 sneck 28 to 30, views offthe page, before the request: 0.18 sthe worker's evaluator: 1.25 s1.3 sthe worker's other work: 0.11 sthe result shared (worker): 1.44 s1.4 sencoded (worker): 1.18 s1.2 sdecoded (main thread): 2.01 s2.0 sthe page, to the picture: 0.34 sneck 30 to 28, views offthe page, before the request: 0.18 sthe worker's evaluator: 1.18 s1.2 sthe worker's other work: 0.12 sthe result shared (worker): 1.55 s1.6 sencoded (worker): 1.20 s1.2 sdecoded (main thread): 1.67 s1.7 sthe page, to the picture: 0.42 s0.4 sevaluator (worker)other (worker)shared (worker)encoded (worker)decoded (page)the page's own work0 s1 s2 s3 s4 s5 s6 s
Firefox on GitHub's machines, a release build (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).
the editcompiled viewsthe picture shows it, sthe worker's evaluator, sthe result shared (in the worker), sencoded (in the worker), sdecoded (on the main thread), sthe result's trip, sthe picture came withthe run done, the result handled, s
neck 28 to 30on6.41.11.41.21.94.6the result6.4
neck 30 to 28on5.61.11.51.11.44.0the result5.6
arms (up / down) 55 to 65on4.21.21.70.61.84.2a stream, 4.4 MB: shared 0.9, encoded 0.4, decoded 1.27.1
arms (up / down) 65 to 55on5.21.81.00.61.32.9a stream, 4.4 MB: shared 1.0, encoded 0.4, decoded 2.06.9
neck 28 to 30off6.51.31.41.22.04.6the result6.5
neck 30 to 28off6.31.21.61.21.74.4the result6.3
arms (up / down) 55 to 65off4.91.22.31.12.76.1a stream, 4.4 MB: shared 1.1, encoded 0.6, decoded 1.210.0
arms (up / down) 65 to 55off5.51.21.01.41.43.9a stream, 4.4 MB: shared 1.3, encoded 1.2, decoded 1.38.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.

the result, as it crossedneck 28 to 30, MBarms (up / down) 55 to 65, MB
the whole result5.385.40
the probes' samples: their environments4.144.15
the probes' samples: their values0.360.40
the probes' samples: call stacks, arguments and frames0.140.13
the observation trace (the final state's obs_trace)1.401.44
of it, the environments of its span-close events1.271.31
the program's value< 0.01< 0.01
the final stream's entries, besides their states0.010.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:

  1. Leave out a module, or any value holding functions, from a sample's environment, as a function is left out now: Drawing alone 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).
  2. 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).
  3. Clear the trace before the result leaves the worker, as the main thread's evaluation does: 1.4 MB. Done 11-08 (two cuts).
  4. 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".)

After two cuts: the same small edits on the Witch slide, in Firefox, on the same scaleneck 28 to 30, views onthe page, before the request: 0.32 sthe worker's evaluator: 1.05 s1.0 sthe worker's other work: 0.14 sthe result shared (worker): 0.19 sencoded (worker): 0.05 sdecoded (main thread): 0.16 sthe page, to the picture: 0.18 sbefore the cuts: 6.4 s2.1 s; before the cuts 6.4 s (dashed)neck 30 to 28, views onthe page, before the request: 0.21 sthe worker's evaluator: 1.01 s1.0 sthe worker's other work: 0.12 sthe result shared (worker): 0.23 sencoded (worker): 0.05 sdecoded (main thread): 0.13 sthe page, to the picture: 0.16 sbefore the cuts: 5.6 s1.9 s; before the cuts 5.6 s (dashed)arms (up / down) 55 to 65, views onthe page, before the request: 0.20 sthe worker's evaluator: 1.06 s1.1 sthe worker's other work: 0.14 sthe result shared (worker): 0.15 sencoded (worker): 0.05 sdecoded (main thread): 0.13 sthe page, to the picture: 0.17 sbefore the cuts: 4.2 s1.9 s; before the cuts 4.2 s (dashed)arms (up / down) 65 to 55, views onthe page, before the request: 0.19 sthe worker's evaluator: 1.04 s1.0 sthe worker's other work: 0.13 sthe result shared (worker): 0.15 sencoded (worker): 0.05 sdecoded (main thread): 0.12 sthe page, to the picture: 0.17 sbefore the cuts: 5.2 s1.9 s; before the cuts 5.2 s (dashed)neck 28 to 30, views offthe page, before the request: 0.30 sthe worker's evaluator: 1.06 s1.1 sthe worker's other work: 0.15 sthe result shared (worker): 0.21 sencoded (worker): 0.06 sdecoded (main thread): 0.12 sthe page, to the picture: 0.48 s0.5 sbefore the cuts: 6.5 s2.4 s; before the cuts 6.5 s (dashed)neck 30 to 28, views offthe page, before the request: 0.20 sthe worker's evaluator: 1.00 s1.0 sthe worker's other work: 0.12 sthe result shared (worker): 0.26 sencoded (worker): 0.05 sdecoded (main thread): 0.13 sthe page, to the picture: 0.25 sbefore the cuts: 6.3 s2.0 s; before the cuts 6.3 s (dashed)arms (up / down) 65 to 55, views offthe page, before the request: 0.18 sthe worker's evaluator: 1.05 s1.1 sthe worker's other work: 0.17 sthe result shared (worker): 0.16 sencoded (worker): 0.05 sdecoded (main thread): 0.11 sthe page, to the picture: 0.25 sbefore the cuts: 5.5 s2.0 s; before the cuts 5.5 s (dashed)evaluator (worker)other (worker)shared (worker)encoded (worker)decoded (page)the page's own work0 s1 s2 s3 s4 s5 s6 s
Firefox on GitHub's machines, a release build (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 editcompiled viewsthe picture shows it, before the cuts, safter, sthe evaluator, sthe trip of the picture's message, sthat messagefaster, times
neck 28 to 30on6.42.11.00.400.41 MB, the result3.1
neck 30 to 28on5.61.91.00.420.41 MB, the result2.9
arms (up / down) 55 to 65on4.21.91.10.330.41 MB, the result2.2
arms (up / down) 65 to 55on5.21.91.00.320.41 MB, the result2.8
neck 28 to 30off6.52.41.10.390.42 MB, the result2.7
neck 30 to 28off6.32.01.00.440.41 MB, the result3.2
arms (up / down) 55 to 65off4.92.11.10.350.36 MB, a stream2.3
arms (up / down) 65 to 55off5.52.01.10.330.41 MB, the result2.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".)

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 133the whole view, ms (today)the merge with the last picture, msthe kept view on the merged picture, msthe whole view over the kept one, timesthe same markup
10.2360.2310.00733yes
50.2310.2320.0279yes
170.2110.2120.0435yes
300.2190.2260.0713yes
600.2220.2390.1292yes

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:

The Witch, as hazel-jit's view drew her
hazel-jit
0.19 ms (5,200 fps)
The Witch, as ReScript's view drew her
ReScript
0.18 ms (5,600 fps)
The Witch, as Melange's view drew her
Melange
0.17 ms (5,800 fps)
The Witch, as js_of_ocaml's view drew her
js_of_ocaml
1.01 ms (990 fps)
The Witch, as by hand's view drew her
by hand
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.

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 stephazel-jitReScriptMelangejs_of_ocamlby 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 tooflat: {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 toFixedx.toFixed(1)x.toFixed(1)Printf.sprintf "%.1f": the format read at every call, then toFixedx.toFixed(1)
a function of a tuple, grow((b, v))one array argument, made at each callgrow(b, v)grow(b, v)inlinedinlined by hand
the walks, grow_all, bounds, framingrecursive callswhile loopswhile loopsnested for loops, inside the viewfor…of
the bounds, a 4-tuplea new array per pointa new array per pointa new array per pointa new block per pointfour variables
p.name == "frame"$R.eq(…)============
what the view returnsa command, Bind(EvalSplice(ref), k); k, called with the splice's value, makes the Htmlthe 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:

QuestionThe Petri ruleThe Witch's pictureWhat 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 ReScript2.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 interpreter225 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 handSlower 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_ocamlAmong 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 layoutsConstructors 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:

  1. 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 with f(n, i), keeping the one-argument form for a function passed as a value.
  2. 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).
  3. Turn tail calls into loops, as all three do for nth, pick and has: faster, and no stack to overflow on a long list.
  4. Unbox options, and make constructors flat. Some(t) as t and None as undefined saves an allocation per option; {TAG, _0, _1, _2} saves the inner array of {c, v: [a, b, k]}.
  5. 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.

What next#

  1. Try the first three changes in hazel-jit (arguments as JS arguments, === on Ints, tail calls as loops), and time the rule again.
  2. Then the layouts (flat constructors, unboxed options, objects for labeled tuples), with the trip in and out and HazelJitDOM changed to match.
  3. 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.
  4. 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).
  5. 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.
  6. 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.
  7. 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).
  8. 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.
  9. 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.
  10. The page's view: 117 ms each time on this slide; find what in it is redone when only a slider changed.
  11. Keep a drawn answer's node with the answer, so that a kept answer is not drawn from JS again on every render.
  12. Once the run is fast: share in the trip in, and keep the view's and the drawing's per-shape work (above).
  13. 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#

ReScript#

Melange#

js_of_ocaml#

BuckleScript#

Writing the JS by hand#

Hazel's interpreter#

How this was made#