Hazel reports · ^report(65) · hazel-jit · 2026-11-25, revised 2026-11-28 08:19 MST · work in progress · after reports 046, 054 and 063
hazel-jit’s PR, Described
What should the description of hazelgrove/hazel#2859, hazel-jit for dev, say to the people who decide on it and the people who review it? Matthew asked on 2026-11-25 for a report “to iterate on the content of the PR description there”, “esp the executive and engineering summaries”, with a section comparing the compilers from OCaml to JavaScript that ^report(46) looked at, by how they lay out values, and by speed on the games, “which we largely didn’t have before”. The description is drafted here, section by section; the sections marked as the PR’s are the text for GitHub, and the rest is this report’s. Nothing is posted until Matthew says so.
Outline#
- The opening line: what the PR is, in one sentence.
- Executive summary: what hazel-jit is, why, what it changes in
dev, how it is checked, who uses it, and what to review. - Engineering summary: what goes in and comes out, how the code is written, its values, what it leaves to the interpreter, mixed mode, and its speed on the tests.
- What it holds, how compiled values look, beside other compilers, where it comes from, where it differs from the interpreter and what is next: the PR’s details.
- Mixed mode, at its boundary and types: errors, type holes and unknowns (2026-11-27, from Matthew and Cyrus’s meeting): what a call into compiled code converts, costs and falls back on; that compiled code never calls interpreted code; holes, functions and polymorphic functions across; and where a cast through an unknown type nobody wrote lets compiled code part from the interpreter (
^ticket(898)). - Checked against the interpreter, in CI (2026-11-27, Cyrus’s question at the meeting): the interpreter is the specification; every evaluator program is also run compiled where it compiles and compared, and a different answer fails the build (289 checks, 234 compiled, none different); what it does not hold the jit to yet, and five steps to make the check systematic.
- Representations, then and now: three of
^report(46)’s five suggestions for hazel-jit’s output are done and the other two in part; records, calls of named functions and loops now look as ReScript’s and Melange’s do. - Loops and JS arguments, made on 2026-11-25: the games take 0.70 of the jit’s time,
^report(46)’s rule 0.67, every answer the same; nearly all of it from the JS arguments. - Four ways, on the games: hazel-jit now is 4,148 times as fast as the interpreter, 3.1 times as fast as on 2026-11-19, and 3.8 times plain JS’s time.
- Gnome world’s speed run, three ways: its 44,933 steps take 25.5 s with hazel-jit now, 24.7 s in plain JS, and about 4.2 minutes interpreted; with hazel-jit, the rules are 1.3% of the run.
- Speed beside them: on
^report(46)’s rule, ReScript and Melange are still about four times as fast; the games still need versions in OCaml and ReScript. - Conversion at the edges, over time: from most of a call to a fraction of one in two days; now the larger part again where a whole state crosses (on the Petri rule 71% of a call), and five ways to bring it down.
- What is still left: five steps in Matthew’s order, each a ticket: constructors made once, modules by name and
===(made, being timed), the cost of exact Ints and steps (measured), and options unboxed last. - Choices for the description: what to settle before it goes on GitHub.
- The text for GitHub, to copy, and its revisions.
- What next, and how this was put together.
The opening line#
This report’s
The description opens with one sentence saying what the PR is, and that it is the only PR that tracks the jit. The npm package’s branch, #2857, takes the jit by merging this branch, so there is one place to review it.
In the PR’s description: its opening line
🤖 hazel-jit, for dev: a compiler from Hazel to JavaScript, with the tests that hold it to the interpreter's answers. This is the one PR that tracks the jit. The npm package's branch, #2857 (hazeljs), merges it rather than keeping a copy of its own.
Executive summary#
In the PR’s description
- What it is. hazel-jit compiles a complete Hazel program to JavaScript and runs that in place of the interpreter. Where it can't (a hole, a test, an error the interpreter would show), the interpreter runs the program as it does today, so an answer never changes.
- Why. Speed, toward 60 frames a second for small edits. Compiled ahead of time, the games of hazeljs-examples run 2,392 times as fast as on the interpreter (the geometric mean over 45 operations in 17 games). Gnome dig's step takes 82 ms on the interpreter and 0.28 ms through the jit.
- **What it changes in
dev.** It adds new files, and 13 lines to two ofdev's,Builtins.reandTransition.re, for mixed mode. Nothing in the app turns hazel-jit on yet: the editor, the evaluator and the stepper run as before. - How it is checked. Every evaluator test also runs compiled, where it compiles, and must give the interpreter's answer. Of the 289 checks, 234 compile; 211 of those give the interpreter's answer and none differs. The other 23 end where the interpreter's result is not a value, which the sweep doesn't compare. Mixed mode is held to the interpreter the same way, as are 2,000 random programs, and a floor fails the build if fewer programs compile. hazel-jit also has 13 tests of its own.
- Who uses it. The hazeljs npm package (#2857): its
Hazel.jitandHazel.compileare hazel-jit. And the games of hazeljs-examples, which keep their rules compiled ahead of time. - What to review. The two hooks into the interpreter; the shapes compiled code gives values, which pages that keep compiled code depend on; and the list of what is left to the interpreter.
Engineering summary#
In the PR’s description
- In and out. The input is the elaborated term the interpreter would run, with the types the statics gave it. The output is JavaScript source: the body of a function of one argument,
$R, the runtime (about 23 KB of source: equality, lists, numbers, the builtins). The caller makes it a function withnew Functionand calls it. - How the code is written. Expressions all the way down. A
let's bindings are assigned, in a comma expression, to variables declared at the top of the enclosing function, so aletmakes no closure. Every binding gets a JS name of its own (x$12), so shadowing can't clash. A builtin written in OCaml is the runtime's JS version of it; one written in Hazel (List.map) is compiled once, before the program. - Values. Close to what a person would write in JavaScript: numbers, strings and booleans as themselves, a record as a plain object, a list as cells
{$h, $t}(so::is O(1)), a constructor as{$c, $v}(the table below). An Int is exact, as the interpreter's is: a JS number while it is a safe integer, a BigInt past that. An SInt is 32 bits, each result checked. A field reade.xis the property itself, behind a check. - Calls and loops. A function bound to a name whose parameter is a tuple pattern takes the parts as JS arguments, so a call with the tuple written out makes no array:
fire_at.$d(n, i). A recursive function's calls of itself in tail position are afor (;;)loop, so a long list doesn't overflow the stack. Every other caller still passes the tuple, to the function's one-argument form. A module's member is read as its own variable (Common.pickispick), a constructor without an argument is one constant made once, and==against a number, string or boolean written out is===. - What is left to the interpreter. By rule, not by gap: holes outside mixed mode (below), the stepper's filters, an operation the interpreter stops at with an error, undefined, and a run past its budget. For a form, the compiler raises
Unsupported(reason); at run time, the compiled run stops. Either way the interpreter's run is the answer. - Back to Hazel.
HazelJitValueturns a compiled value back into a Hazel value by the types the term records, or into a diff against the last answer. - Mixed mode. The interpreter runs a program around its holes and calls its fully typed functions (whole types declared) compiled, as builtins made at run time. A call that can't finish compiled falls back to the interpreter, for that call alone.
- Holes in mixed mode. A hole in an argument crosses into compiled code as an opaque value and comes back as the very term; a function with a hole in its code compiles, the hole an opaque value too. Code that would look inside one (match it, add to it, branch on it) runs in a checked version that stops there, and that call falls back. An answer holding a hole carries the interpreter's casts, made by the interpreter's own code (
Ascriptions). A list of 2,000 built with a hole, then its length, runs about 17 times as fast as on the interpreter, where before the hole sent the call back. - Speed on the tests. Over the 208 evaluator programs that compile and agree, the median program runs 395 times as fast compiled, run after run. On a first run, with its JS parsed, it runs 6.9 times as fast; compiling all 208 took 65 ms, against the interpreter's 143 ms for their first runs.
What it holds#
In the PR’s description
| Part | Files | What it does |
|---|---|---|
| The compiler | src/language/dynamics/HazelJit.re | An elaborated term as the body of a JavaScript function of its runtime ($R), and the runtime's source. A form it does not compile raises Unsupported with the reason, and the caller runs the interpreter. |
| The values across | src/language/dynamics/HazelJitValue.re | A Hazel value into compiled code's shapes and back, by the types the compiled term records; a compiled program instantiated; an answer carried back as a diff against the last one. |
| Mixed mode | src/language/dynamics/HazelJitMixed.re, 5 lines in Builtins.re, 8 in Transition.re | The interpreter calls a program's fully typed functions compiled, as builtins made at run time, holes and all. Nothing in the app turns it on yet. |
| Tests | test/Test_HazelJit.re, test/Test_HazelJit_Agree.re, a hook in test/evaluator/Test_Evaluator_Prelude.re | Every evaluator test also run compiled (and mixed) where it compiles, compared with the interpreter's answer shape by shape; agreement on the language's forms, big Ints, invalid operations, strings, partial and mutual functions, the step limit, undefined, labels, Unicode, field reads, records whose labels look like the runtime's own keys, loops and JS arguments; values carried back; diffs; what is not compiled and why; mixed mode, holes in it carried and fallen back. |
How compiled values look#
In the PR’s description
| Hazel | Compiled JavaScript |
|---|---|
A tuple whose every entry is labeled, (x = 1, y = 2) | A plain object, {x: 1, y: 2}, its keys in the type's order |
Any other tuple, (1, 2) or (1, y = 2) | An array, carrying its labels under Symbol.for("hazel-jit.labels") |
| A list | null, or a cell {$h, $t} |
A constructor, Some(3); None | {$c: "Some", $v: 3}; {$c: "None"}, one constant made once for each constructor without an argument |
| A function | A JS function of one argument; a function of a tuple takes an array. A named function of a tuple pattern also has a direct form, f.$d(a, b), its parts as JS arguments |
- A field read
e.xcompiles to the property itself,e["x"], behind a check thateis an object holdingxand not a list's cell. Anything else (a list of rows, the empty list, a value the interpreter would be stuck on) goes to the runtime's$R.dot, so the answer is the same. - A label named
lengthor__proto__, or starting with$, is kept as$and the label. So no record has the keys$h,$t,$cor$v, and a record is never taken for a cell or a constructor. ==is$R.eq, which is small enough for the engine to inline: its first line compares two numbers, strings or booleans with===, and objects go on to$R.eqo. Floats compare with===directly.
These shapes are Matthew's decisions of 2026-11-24. Measured on the games as their pages play them, records as objects take 0.63 of the time that records as arrays took (the geometric mean over 14 games), with every answer the same.
Beside other compilers to JavaScript#
In the PR’s description
| Value | hazel-jit | ReScript 12 | Melange 5 | js_of_ocaml 5.9 |
|---|---|---|---|---|
| A list cell; the empty list | {$h, $t}; null | {hd, tl}; 0 | {hd, tl}; 0 | [0, hd, tl]; 0 |
| A record | an object by label | an object by field | an object by field | [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] |
Some(t); None | {$c: "Some", $v: t}; {$c: "None"} | t; undefined | t; undefined | [0, t]; 0 |
| A function of two | a named one: f.$d(a, b); else one argument, an array | f(a, b) | f(a, b) | f(a, b) |
i == 0 on Ints | i === 0 against a literal; else $R.eq(a, b) | i === 0 | i === 0 | 0 === i |
| A tail call | of itself: a loop; else a call | a loop | a loop | a loop |
hazel-jit's names: $h and $t are a list cell's head and tail, $c and $v a constructor's name and value, $d a named function's direct form, and $R the runtime compiled code is given. They begin with $ because a record's label that does is kept under $ and the label, so no record has these keys, and one key tells a cell or a constructor from a record. ReScript and Melange name a cell's parts hd and tl, and a constructor's TAG, _0, _1; js_of_ocaml uses arrays, the tag first.
hazel-jit's records, its calls of named functions and its loops now look like ReScript's and Melange's; its constructors and options do not yet. On one small rule (a Petri net's step), ReScript's and Melange's code runs about 4 times as fast as hazel-jit's (measured 2026-11-25), where hazel-jit's Ints are exact and every function counts a step for the run's budget.
Where it comes from#
In the PR’s description
The compiler and its tests come from the livelits line's trial/794-games-aot (the jit of #2817), ported to Jane Street Core, with the arms for livelit-only forms (Quote, Unquote, Bind, Splice, a module's ModVal) taken out. The livelit views compiled (HazelJitRun, HazelJitDOM) and their tests stay on the livelits line.
Since it opened:
- Records as objects (9ec865976d), above.
- A recursive function's own capture, undefined when the function was made, is bound again by its
FixFon the way back (5ec4ff4307). - A pattern by position that meets a labeled entry leaves the run to the interpreter (e0a5925f3b).
- A recursive function's calls of itself in tail position are a loop (50b70d5a40), its variables declared in each pass, so a closure keeps its pass's values.
- A named function of a tuple pattern takes the parts as JS arguments (43828037c9).
- A constructor without an argument is one constant (ae29353ff0), a module's member is read as its own variable (9dd8fce210), and **
==against a literal is===** (0ce95a2e8f): together 0.95 of the jit's time on the games. With both, the games take 0.70 of the jit's time as their pages play them (the geometric mean over 14 games), every answer the same; nearly all of it is the arguments. - **Int division is
Math.trunc(a / b) + 0** (136fecdcaf): exact for safe integers, checked against BigInt on two million divisions, twice as fast, and never-0, where the old form gave-0for a negative divisor and no quotient (-1 / -2), which shows through a Float as-infinity. Int arithmetic on numbers written out is folded at compile time (664c49c49a). Together, Gnome world'sWorld.steptakes about 42% less time.
Mixed mode, at its boundary#
In the PR’s description
Mixed mode keeps the interpreter in charge, so holes, probes and the stepper work as they do, and calls a program's complete, fully typed functions compiled.
- What is compiled. A
letat the top of the program that binds a function whose whole type is declared (let f : A -> B = fun ...), with no unknown in it. Each is compiled with the accepted functions before it, so it may call them. A function that names anything else is not compiled, and the reason is kept. A module's members run on the interpreter for now (below). - How it hooks in. The program is rewritten before it runs: the interpreter still evaluates each such definition to its own closure, then hands it to a builtin made at run time, which gives back a second builtin, the compiled function. The interpreter's one change is to look builtins up in a table filled at run time, after its fixed one.
- A call. The argument is converted into compiled code's shapes. The compiled function runs under a budget (50 million calls or 1 s). The answer is read back by the declared result type, which is why the type must be declared: a JavaScript number does not say whether it was an Int or a Float. An answer that may hold a hole is cast to that type by the interpreter's own
Ascriptions, so mixed mode's answer is the interpreter's, casts included. - Falling back, one call at a time. If the argument will not convert, an operation is invalid (a division by zero), the budget runs out, or the answer will not convert back, the interpreter applies its own closure to the same argument. Evaluation has no effects, so running a call again is safe.
- What the boundary costs. Compiled functions call one another directly, so the crossing is paid only where interpreted code calls compiled code, in proportion to what crosses. On build 49:
fib(16), all compiled, 72.6 ms interpreted against 0.1 ms mixed; the Petri net rule 120.8 against 16.8 ms; but a one-line function called 500 times from an interpreted loop 22.4 against 27.9 ms, each crossing costing more than its work. - Compiled code never calls interpreted code. A function that calls one the interpreter keeps is itself left to the interpreter, so the boundary is crossed one way only. One function that cannot compile keeps every function that calls it interpreted.
- Holes cross unopened. A hole in an argument crosses as an opaque value, its term kept aside. Compiled code may move it (return it, store it in a pair or a record, count a list holding it), and it converts back as the very term that went in, with its cast. Code that would look inside it (match a constructor, branch on it,
x + ?) stops the call, which falls back, so the interpreter gives the indeterminate answer. - Functions as arguments. A compiled function that passes compiled functions, or lambdas of its own, calls them directly in JavaScript. An interpreter's closure passed in from interpreted code crosses as an opaque value, as a hole does: carried, but calling it stops the call, which falls back. A function in an answer comes back as a Hazel closure when compiled code made it; a recursive one, or one it did not make, makes that call fall back. No test has a function-typed parameter yet.
- Polymorphic functions. Compiled code erases types:
typfun a -> ecompiles ase, a type applicationf@<T>asf, and one compiled function serves every type it is used at, as compiled code never looks at a type at run time. Mixed mode does not compile one, since a candidate's declared type must be an arrow at the top, and aforallis not. Reading an answer back at a type variable has no type to go by: across a page's boundary, a whole JavaScript number comes back an Int and any other a Float, so a polymorphic function used at Float could give back2.0as the Int2. No test covers a polymorphic function across that boundary.
Types: errors, type holes and unknowns#
In the PR’s description
Hazel is gradually typed: the interpreter runs code with type errors and unknown types, inserting casts and stopping at one that fails. Compiled code erases casts, so what it leaves to the interpreter is decided by where a cast could fail.
- Static type errors. The interpreter runs ill-typed code and stops at the failed cast. The editor compiles no program with a static error (the evaluator sweep's 30 ill-typed programs). The package's
Hazel.jitandHazel.jitProgramcompile one anyway and return it withok: falseand the errors, leaving the choice to the page: Gnome world's page uses no rules with errors. - **A type hole written,
?, is a hole.** A program with one is not compiled ("a type hole"): a cast through it can fail at run time with no static error, asf(([1.5, 2.5] : ?))forftaking[Int]does, and compiled code, its types erased, cannot tell an Int from a Float. - An unknown type nobody wrote is compiled, and there compiled code can part from the interpreter.
let f = fun x -> x + 1 in f(true)has no static error, asx's type is unknown. The interpreter stops at the failed cast (trueis not an Int); compiled code answers2. The same happens inside a fully typed function,let f : Bool -> Int = fun b -> let g = fun x -> x + 1 in g(b), so mixed mode's candidates can meet it too. The evaluator sweep has no such program. A fix to decide: compile a check where a value is cast out of an unknown type into a ground type (a Bool, a String, a list, a constructor: atypeofor a key test), stopping the run so that the interpreter answers. An Int and a Float through an unknown type stay alike, as both are JavaScript numbers, unless Floats are tagged.
Checked against the interpreter, in CI#
In the PR’s description
The interpreter is the specification, and CI holds the jit to it on every push: a program whose compiled answer differs from the interpreter's fails the build.
- The sweep, over every evaluator program (
test/Test_HazelJit_Agree.re, hooked intoTest_Evaluator_Prelude.evaluation_test). Each program of the evaluator's own tests is elaborated, run by the interpreter, and, where hazel-jit compiles it, run compiled. The two answers are compared shape by shape: numbers by value (a float within a relative 1e-9); lists, tuples, labels and constructors by structure; a function only as a function. Where the interpreter stops at an error (a division by zero), the compiled run must stop at the same one. Where its answer is left indeterminate, the compiled run must stop as indeterminate. - Mixed mode, swept the same way. For every evaluator program with a top-level function of a declared type, those functions are compiled and the rest interpreted. A different answer from the interpreter's, or a run or rewrite that raises, fails that program's test.
- Random programs (Full tests). The generator of the evaluator's "Evaluator does not crash" property (
QCheck_Util.arb_exp) makes 2,000 programs at a fixed seed, so a failure can be reproduced. Each one with no static error that the interpreter finishes within 10,000 steps is compiled where it compiles, run, and compared as the sweep compares. - A floor on what compiles. At least 234 of the evaluator's programs must compile, and at least 6 in mixed mode, so a change that leaves more programs to the interpreter fails the build. The floor is checked when the whole sweep has run.
- The agreement tests (
test/Test_HazelJit.re). These are written by hand, each run both ways and compared the same way. They cover the language's forms, big Ints, invalid operations, the string builtins, partial and mutual functions, the step limit, labels and Unicode, plus values carried back and diffs.
On this branch (31c7c89de1, CI run 46157399352):
| programs | |
|---|---|
| evaluator checks (Fast checks; Full tests 290) | 289 |
| compiled and run (the floor: 234) | 234 |
| the same answer | 211 |
| a different answer, or the compiled run threw | 0 |
| the interpreter's answer is not a value (an error or a stuck term), checked as above | 23 |
| not compiled: holes 15, type holes 10, free variables 24, other forms 6 | 55 |
| mixed mode: programs with a function compiled, all the same answer | 6 (2,263 calls compiled, 8 fell back) |
| random programs made; with no static error; finished by the interpreter | 2,000; 80; 80 |
| random programs compiled; the same answer; not a value, not compared | 61; 60; 1 |
What it does not hold the jit to yet:
- Ill-typed programs. Thirty tests are listed as known (
known_not_a_value): the interpreter stops at a failed cast, while compiled code, its types erased, runs on. The editor compiles no program with a static error; see *Types: errors, type holes and unknowns*. - Programs that are not compiled. A program the jit does not compile runs on the interpreter, so it cannot disagree; the floor keeps that number from growing.
- Few random programs. The generator makes mostly ill-typed programs: of 2,000, 80 had no static error and 61 compiled.
- Only the evaluator's programs. The games' rules in hazeljs-examples are checked in that repository's CI against hand-written plain JavaScript twins: each piece of Gnome world against its twin, the speed run's world state step for step, and every game's answers three ways (an older interpreter, the jit's code compiled ahead of time, plain JavaScript). Plain JavaScript there is a second witness for the games and the baseline for speed, not the specification.
Systematically, from here:
1. More random programs that type: 10,000 a run, as "Evaluator does not crash" runs, or a generator of well-typed programs. 2. A check where a value leaves an unknown type for a ground one, so the thirty ill-typed programs agree and come off the known list. 3. The games' rules and their recorded runs as test programs here, each answer against the interpreter's.
Where it differs from the interpreter on dev#
In the PR’s description
- SInt overflow. hazel-jit stops a 32-bit SInt that overflows at
IntegerOverflow;dev's interpreter wraps it (2147483647 + 1 is -2147483648). SInt is to have 64 bits everywhere, with overflow an error (#2828). The jit's 64-bit SInt is ontrial/758-sint64-jitand comes here with #2828. The evaluator tests have no such program, so the sweep does not meet it. - A cast out of an unknown type nobody wrote is erased, so where the interpreter stops at the failed cast, compiled code runs on (
let f = fun x -> x + 1 in f(true): the interpreter's failed cast, compiled code's2). See *Types: errors, type holes and unknowns*. - A tuple with a label given twice is not compiled, since keying it by its labels would lose one entry. The interpreter runs it, keeping both entries as
devhas since #2472.
Next, on this branch#
In the PR’s description
- Mixed mode's types from the statics, not from annotations:
devlowers a module to lets whose patterns lose their annotations, so a module's members run on the interpreter in mixed mode for now. - Holes beyond mixed mode: a whole program, a livelit's view and the package's
Hazel.jitstill refuse them. Multiholes and error holes, with their environment, stay the interpreter's. - Values across without converting: in mixed mode, the interpreter keeps a compiled call's answer as compiled code holds it, converting only what it looks inside, so a value passed from one compiled function to another through interpreted code is never converted.
- The games' rules as test programs (hazeljs-examples, compiled whole).
- A check at each cast out of an unknown type into a ground type, so compiled code stops where the interpreter's cast fails.
- Polymorphic functions in mixed mode: the type at each use, from the type application or the statics, to read the answer back by; and a test of one across a page's boundary.
- Tests of a function-typed parameter in mixed mode, from compiled callers and from interpreted ones.
- 64-bit SInt, with #2828.
Tried and not kept: lists as cells plus array segments (hazel-jit-lists). On hazeljs-examples' seventeen games it cost about 8% ahead of time and 4% through the jit, all answers the same, so cells stay.
The design and the measurements are in Hazel reports: ^report(54) (records, lists and holes), ^report(55) (holes in mixed mode), ^report(63) (every game, four ways) and ^report(46) (beside ReScript, Melange and js_of_ocaml).
Representations, then and now#
This report’s
Since ^report(46) (2026-11-07), hazel-jit lays out records as ReScript and Melange do, as plain objects read by their labels, and does as they do with a named function’s arguments and a function’s calls of itself in tail position; its lists, constructors and options are still its own. ^report(46) compiled one small rule (a Petri net’s step, ^report(43)’s) with ReScript, Melange, js_of_ocaml and hazel-jit, and put the four layouts side by side. The table below is that table with hazel-jit as it is on #2859 now (be44756900, with all of today’s changes merged); the cells that changed since 2026-11-07 are shaded.
| Value | hazel-jit 2026-11-07 | hazel-jit now | ReScript 12 | Melange 5 | js_of_ocaml 5.9 |
|---|---|---|---|---|---|
| a list cell; the empty list | {h, t}; null | {$h, $t}; null | {hd, tl}; 0 | {hd, tl}; 0 | [0, hd, tl]; 0 |
| a record, or labeled tuple | an array, each label also a property | an object by label | an object by field | an object by field | [0, f1, f2, …] |
| an unlabeled tuple (a, b) | [a, b] | [a, b] | [a, b] | [a, b] | [0, a, b] |
| Take(a, b, k) | {c: "Take", v: [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"} | one constant made once, {$c: "Done"} | "Done" | 0 | 0 |
| Some(t); None | {c: "Some", v: t}; {c: "None"} | {$c: "Some", $v: t}; None one constant | t; undefined | t; undefined | [0, t]; 0 |
| a function of two | one argument, an array [a, b] | a named one: f.$d(a, b), its parts as JS arguments; else one argument, an array | f(a, b) | f(a, b) | f(a, b) |
| a field read r.x | $R.dot(r, "x") | r["x"] behind a check; a module's member, its own variable | r.x | r.x | r[1] |
| i == 0 on Ints | $R.eq(i, 0) | i === 0 against a literal; else $R.eq(a, b) | i === 0 | i === 0 | 0 === i |
| a tail call | a call | a call of itself: a turn of a for (;;) loop; else a call | a while loop | a while loop | a for loop |
The names in hazel-jit’s columns: $h and $t are a list cell’s head and tail, $c and $v a constructor’s name and value, $d a named function’s direct form (its tuple’s parts as JS arguments), and $R the runtime compiled code is given ($R.eq, $R.dot). They begin with $ because a record’s label that does is kept under $ and the label ($c as $$c), so no record has these keys, and one key tells a cell or a constructor from a record (decision 2 of ^ticket(847)). ReScript and Melange name a cell’s parts hd and tl and a constructor’s TAG, _0, _1; js_of_ocaml uses arrays, the tag first.
ReScript and Melange come from one family, BuckleScript’s: OCaml’s (or ReScript’s) front end and type checker, then a back end that writes the JavaScript a careful person would write, module by module. js_of_ocaml, which builds Hazel itself, compiles OCaml’s bytecode, a whole program at once, and keeps OCaml’s memory layout: every block an array with its tag first. hazel-jit is a third kind: it compiles Hazel’s elaborated terms in the page, while the program runs, with a runtime of its own.
^report(46) ended with five changes to hazel-jit’s output, each something these compilers already do, in the order they would likely help that rule (its section). Where each stands:
- done Pass a function's arguments as JS arguments. A function bound to a name, whose parameter is a tuple pattern, has a direct form with the parts as its JS parameters; a call with the tuple written out goes to it with no array (43828037c9). A function made where it is passed keeps one argument.
- in part Compare Ints with ===. == against a number, string or boolean written out is === (0ce95a2e8f, merged); between two Ints held in variables it is still $R.eq, which is small enough for the engine to inline.
- done Turn tail calls into loops. A recursive function's calls of itself in tail position are a for (;;) loop, its variables declared per pass (50b70d5a40). Calls of another function in tail position, and mutual recursion, are still calls, as in ReScript and Melange.
- in part Unbox options, and make constructors flat. Constructors without arguments, None among them, are one constant each (ae29353ff0, merged), so None is no longer made anew; Some(t) is still {$c: "Some", $v: t}, and Take(a, b, k) still holds its arguments in an inner array. Options unboxed were measured from a bench build only (
^ticket(858)). - done Lay out labeled tuples as objects only. A tuple whose every entry is labeled is a plain object (9ec865976d), and a field read is the property behind a check: 0.63 of the time on the games.
Two changes came from elsewhere, not from that list: field reads compiled in place (where they had gone through a call that read every label of every program), and the $ on the keys of cells and constructors, so that a record is never taken for either. Each change to the layout is a change on both sides of the boundary too: what HazelJitValue reads and writes, and what the npm package’s converters do (#2857).
Loops and JS arguments, made on 2026-11-25#
This report’s
With a recursive function’s calls of itself in tail position as a loop, and a named function’s tuple parts as JS arguments, the games take 0.70 of the jit’s time as their pages play them (the geometric mean over 14 games), every answer the same; ^report(46)’s Petri rule takes 0.67 of it. Matthew asked on 2026-11-25 for the three changes this report had filed as not yet: “let’s work on turning tail calls into loops next. followed by passing function arguments as JS arguments. Unboxing options is least important, but would also be nice.” The first two were made on a trial branch off hazel-jit, trial/853-jit-loops-args (50b70d5a40 for the loops, 43828037c9 for the arguments); with hazel-jit’s CI green on them, full tests included, they are merged on Matthew’s word (“Yes, merge it”): hazel-jit (#2859) is at 43828037c9, and hazeljs (#2857) merges it (4cbf7483bf). The third waits for the others (What is still left).
trial/853-pkg (6d041e9769: the hazeljs branch with the trial merged). GitHub run 45640411958, one runner a game: the base, the trial, then the base again; the base’s time is the mean of its two runs.The most changed are Gnome dig 0.50, Bricks 0.55, Star wave 0.59, Light trails 0.61; the least, Snake 0.90, Checkers 0.86, Solitaire 0.86. The runner’s noise: the base’s second run took 0.94 of its first (the geometric mean; from 0.76 to 1.22 by game), and each game’s own compiled.js, the same file every way, took 1.01 with the trial as with the base.
| Game | calls in its run | base | base, again | loops and JS arguments | share of the base’s mean | its own compiled.js, same file |
|---|---|---|---|---|---|---|
| Checkers | 46 | 19.5 µs | 23.8 µs | 18.7 µs | 0.86 | 0.95 |
| Go | 89 | 23.1 µs | 23.8 µs | 14.8 µs | 0.63 | 1.00 |
| Minesweeper | 319 | 8.08 µs | 7.82 µs | 6.60 µs | 0.83 | 1.03 |
| Mahjong | 137 | 19.9 µs | 17.5 µs | 14.5 µs | 0.77 | 0.85 |
| Solitaire | 200 | 6.78 µs | 7.24 µs | 6.02 µs | 0.86 | 1.01 |
| Mahjong solitaire | 72 | 18.9 µs | 18.2 µs | 13.8 µs | 0.74 | 1.01 |
| Pinball | 600 | 9.82 µs | 9.34 µs | 6.18 µs | 0.65 | 1.13 |
| Downhill | 600 | 4.35 µs | 4.52 µs | 3.48 µs | 0.78 | 1.00 |
| Bricks | 600 | 2.79 µs | 2.46 µs | 1.44 µs | 0.55 | 0.95 |
| Snake | 116 | 3.74 µs | 3.50 µs | 3.24 µs | 0.90 | 0.98 |
| Light trails | 22 | 7.67 µs | 5.92 µs | 4.12 µs | 0.61 | 1.27 |
| Star wave | 600 | 15.9 µs | 15.4 µs | 9.18 µs | 0.59 | 0.98 |
| Butterfly garden | 600 | 2.65 µs | 2.01 µs | 1.49 µs | 0.64 | 1.08 |
| Gnome dig | 600 | 29.6 µs | 26.4 µs | 13.9 µs | 0.50 | 0.97 |
What the compiler writes now#
The Petri rule’s Common.nth, fun (xs, i) -> case xs | [] => 0 | x :: rest => if i == 0 then x else nth(rest, i - 1) end, as each build compiles it. Before, one expression: the tuple checked and read from its array, and the call of itself a JS call with a new array:
return (nth$3 = $R.fn((a$4) => {
if (--$R.fuel < 0 || (($R.fuel & 16383) === 0 && performance.now() > $R.deadline)) $R.out();
let xs$5, i$6, m$7, x$8, rest$9;
return (((Array.isArray(a$4) ? (a$4[$R.L] === undefined || $R.nolab(a$4, [0, 1])) : $R.indet("a pattern by position against a labeled entry"))) || $R.nomatch("fun"), xs$5 = a$4[0], i$6 = a$4[1], (m$7 = xs$5, ((m$7 === null) ? (0) : ((m$7 !== null) ? (x$8 = m$7.$h, rest$9 = m$7.$t, ($R.eq(i$6, 0) ? x$8 : (nth$3)([rest$9, $R.isub(i$6, 1)]))) : $R.nomatch("case")))));
}, 0, [nth$3])
Now, the function of one argument checks the tuple and calls the direct form, $d, whose parameters are the parts; and the call of itself is a turn of the loop:
return (nth$3 = $R.fn(function w$4(a$4) {
(Array.isArray(a$4) ? (a$4[$R.L] === undefined || $R.nolab(a$4, [0, 1])) : $R.indet("a pattern by position against a labeled entry")) || $R.nomatch("fun");
return w$4.$d(a$4[0], a$4[1]);
}, 0, [nth$3], (a$4_0, a$4_1) => {
for (;;) {
let xs$5, i$6, m$7, x$8, rest$9;
if (--$R.fuel < 0 || (($R.fuel & 16383) === 0 && performance.now() > $R.deadline)) $R.out();
xs$5 = a$4_0;
i$6 = a$4_1;
m$7 = xs$5;
if ((m$7 === null)) {
return 0;
} else {
if ((m$7 !== null)) {
x$8 = m$7.$h;
rest$9 = m$7.$t;
if ($R.eq(i$6, 0)) {
return x$8;
} else {
a$4_0 = rest$9;
a$4_1 = $R.isub(i$6, 1);
continue;
} } else {
return $R.nomatch("case");
} } } }), nth$3);
})(), nth$10 = let$2, (let$11 = (() => {
let len$12;
return (len$12 = $R.fn((a$13) => {
- Loops. A recursive definition (
FixF) whose body is a function compiles it knowing its own name, and writes its body as statements: an if’s branches, a let’s and a sequence’s last part and a case’s arms pass the tail position on, and a call of itself there gives the parameters their new values and continues afor (;;)loop. A million-deep count now runs compiled, where a JS call each time would overflow the stack (a new test). - Each pass its own variables. The function’s variables are declared inside the loop, so a closure made in one pass keeps that pass’s values, as a call’s would (a new test builds three closures in a loop and calls them). Each pass counts a step for the run’s budget, as each call did.
- JS arguments, for named functions. A function bound to a name (a let, a module’s member, a recursive definition, a builtin written in Hazel) whose parameter is a tuple pattern of unlabeled parts has a direct form, its parts as JS parameters. A call with the tuple written out goes to it with no array: by name where the function is known (
fire_at$81.$d(n$84, i$93)), or through$R.c2to$R.c6, which use$dwhen the function has one. A function made where it is passed keeps one form, since a builtin calls it asf(x)and making it then costs one function, not two. - Everyone else still passes the tuple. The function’s value is still a function of one argument, for a page, the interpreter, a builtin: it checks what the pattern by position checked (a labeled entry read by position is the interpreter’s,
^ticket(851)) and calls the direct form. So nothing outside the compiled code changes, and the package’s converters and the games’compiled.jsfiles work as they are. - As ReScript and Melange do, and no further. Mutual recursion, and a call of another function in tail position, are still calls.
Which change gives what#
Nearly all of it is the JS arguments: loops alone take 0.96 of the base’s time on the games, within the runner’s noise, and both together 0.72. A second run on GitHub (bench/853-split, run 45641693384) played each game three ways on one runner: the base, loops alone (trial/853-pkg-loops, db27761a79: hazeljs with only 50b70d5a40 merged), and both; every answer the same. Loops alone went from 0.82 to 1.14 by game; the games’ own compiled.js took 1.01 and 0.99 of the base’s time on those runners. On ^report(46)’s Petri rule, on the laptop, the base takes 2.77 µs a call, loops alone 2.88, and both 1.95. The games’ recursive functions mostly walk short lists, so a call of itself saved is little; what loops buy is that a long list no longer overflows the stack. Every call with its tuple written out, though, made an array, and now does not. A game’s number moves between the two runs (Bricks took 0.55 of the base’s time in the first and 0.93 in this one), while the geometric mean holds (0.70 and 0.72).
| Game | base | loops alone | share | both | share |
|---|---|---|---|---|---|
| Checkers | 20.8 µs | 19.3 µs | 0.92 | 16.7 µs | 0.80 |
| Go | 20.3 µs | 23.3 µs | 1.14 | 12.7 µs | 0.62 |
| Minesweeper | 10.6 µs | 10.7 µs | 1.02 | 8.84 µs | 0.84 |
| Mahjong | 20.3 µs | 18.8 µs | 0.92 | 14.5 µs | 0.71 |
| Solitaire | 4.55 µs | 5.04 µs | 1.11 | 3.96 µs | 0.87 |
| Mahjong solitaire | 15.5 µs | 14.8 µs | 0.95 | 10.6 µs | 0.68 |
| Pinball | 15.5 µs | 14.1 µs | 0.91 | 10.1 µs | 0.65 |
| Downhill | 3.53 µs | 3.55 µs | 1.01 | 2.60 µs | 0.74 |
| Bricks | 2.52 µs | 2.74 µs | 1.09 | 2.34 µs | 0.93 |
| Snake | 2.87 µs | 2.77 µs | 0.97 | 2.48 µs | 0.86 |
| Light trails | 5.39 µs | 4.99 µs | 0.93 | 3.96 µs | 0.73 |
| Star wave | 8.89 µs | 8.02 µs | 0.90 | 5.71 µs | 0.64 |
| Butterfly garden | 2.00 µs | 1.63 µs | 0.82 | 1.27 µs | 0.64 |
| Gnome dig | 24.6 µs | 21.1 µs | 0.86 | 11.3 µs | 0.46 |
^report(46)’s rule, again#
On this laptop today, the trial takes 2.00 µs a call where the base takes 2.97; ReScript’s code takes 0.51 and Melange’s 0.57, about as on 2026-11-07, so the laptop compares. Five rounds, one way per process, each answering the same 2,000 events and ending on the same marking:
| Code | µs a call, median | the fastest call |
|---|---|---|
| Melange 5.0.0 (046’s output) | 0.57 | 0.38 |
| ReScript 12.3.1 (046’s output) | 0.51 | 0.36 |
| JS written by hand | 0.83 | 0.71 |
| hazel-jit, base (hazeljs 0.0.13, 20febee548) | 2.97 | 1.07 |
| hazel-jit, loops and JS arguments (trial/853-pkg, 6d041e9769) | 2.00 | 1.44 |
hazel-jit’s base is slower than its 1.59 µs of 2026-11-07, and two things since then account for most of it, both the price of Hazel’s meaning: an Int is exact ($R.isub(i$6, 1), which checks the result is still a safe integer, where 11-07’s code wrote i$6 - 1), and every function counts a step against the run’s budget. With the trial it is 3.9 times ReScript’s time, where the base is 5.9 times.
Four ways, on the games#
This report’s
On the games of hazeljs-examples, each played through its own run of states, hazel-jit now runs their rules 4,148 times as fast as Hazel’s interpreter and 3.1 times as fast as hazel-jit did on Thursday 2026-11-19; where a game also has a version written by hand in JavaScript, hazel-jit now takes 3.8 times its time, where it took 11.9 times on 11-19. Every answer is the same all four ways. Matthew asked on 2026-11-25 for this comparison, on a common set of the examples: the interpreter, hazel-jit as of last Thursday, hazel-jit now, and plain JS.
Hazel.runProgram. hazel-jit, 2026-11-19: the game’s own compiled.js, compiled ahead of time with the converters a person wrote. hazel-jit now: Hazel.jitProgram, with all of today’s changes (hazeljs at 2c4a61560f). Plain JS: the game written by hand, for Checkers, Go, Minesweeper, Mahjong, Solitaire, Mahjong solitaire, Pinball, Downhill, Bricks, Snake, Light trails, Star wave, Butterfly garden, Gnome dig. One GitHub runner a game, all four one after another (bench/4way, run 45666570615).- Hazel-jit as of last Thursday is each game’s own
compiled.js: compiled ahead of time between 11-20 and 11-23, by hazel-jit ontrial/794-games-aot, whose compiler last changed on 11-17 (and its value boundary on 11-18), so it is hazel-jit as it was on Thursday; and it is what each game’s page ran then. Since then: records as objects, the converters by declared type and their memory, the loops and JS arguments, and today’s three smaller changes. - Hazel-jit now goes through
Hazel.jit, converting each call’s values from the page’s shapes and back, where the ahead-of-time files had converters written by hand; so it is faster with the conversion included. Most changed since Thursday: Butterfly garden 11.1×, Minesweeper 10.4×, Gnome dig 4.8×; least: Checkers 1.0×, Snake 1.5×, Mahjong 1.7×. - The common set, the 14 games with all four ways (Checkers, Go, Minesweeper, Mahjong, Solitaire, Mahjong solitaire, Pinball, Downhill, Bricks, Snake, Light trails, Star wave, Butterfly garden, Gnome dig): a call takes 24.2 ms interpreted, 18.1 µs by hazel-jit of 11-19, 5.83 µs by hazel-jit now, and 1.52 µs in plain JS (geometric means). The interpreter takes 15,948 times plain JS’s time there.
- What is still between hazel-jit and plain JS is in this report’s other sections: the step count (about 21% of a call) and exact Ints (about 12%), the conversion where a whole state crosses (Conversion at the edges), constructors and options in hazel-jit’s own shapes, and the calls a person would not write (a game written by hand also chooses its own data structures).
The plain JS column (Matthew, 2026-11-25: “Why don’t we have plain JS data for the later part of the table? Can we get it?”): nine of the games had none, as no one had written them in JavaScript. Light trails, Snake, Bricks, Downhill, Star wave, Butterfly garden, Mahjong solitaire, Pinball and Gnome dig now have one, each written as a JavaScript programmer would (arrays, loops, the Floats in the rules’ order) and checked against its compiled.js at every step of many games, from 6,961 answers (Mahjong solitaire) to 600,020 (Butterfly garden), none different. Snake here is the game as it is now (its levels and its newer robot), compiled by the same hazel-jit of 11-19.
| Game | calls in its run | the interpreter | hazel-jit, 2026-11-19 | hazel-jit now | plain JS | now against the interpreter | now against 11-19 |
|---|---|---|---|---|---|---|---|
| Checkers | 46 | 16.4 ms | 13.0 µs | 12.4 µs | 2.06 µs | 1,320× | 1.0× |
| Go | 89 | 71.7 ms | 24.6 µs | 13.0 µs | 4.26 µs | 5,533× | 1.9× |
| Minesweeper | 319 | 110.5 ms | 127.1 µs | 12.2 µs | 1.28 µs | 9,049× | 10.4× |
| Mahjong | 137 | 22.0 ms | 21.4 µs | 12.9 µs | 8.11 µs | 1,705× | 1.7× |
| Solitaire | 200 | 51.7 ms | 10.7 µs | 3.94 µs | 0.97 µs | 13,112× | 2.7× |
| Mahjong solitaire | 72 | 54.4 ms | 38.3 µs | 8.10 µs | 3.06 µs | 6,719× | 4.7× |
| Pinball | 600 | 130.9 ms | 47.0 µs | 10.5 µs | 7.47 µs | 12,520× | 4.5× |
| Downhill | 600 | 9.8 ms | 10.3 µs | 2.81 µs | 0.59 µs | 3,479× | 3.7× |
| Bricks | 600 | 8.3 ms | 4.23 µs | 1.40 µs | 0.25 µs | 5,933× | 3.0× |
| Snake | 105 | 1.6 ms | 4.37 µs | 2.89 µs | 1.05 µs | 570× | 1.5× |
| Light trails | 22 | 7.7 ms | 7.24 µs | 3.91 µs | 0.67 µs | 1,976× | 1.9× |
| Star wave | 600 | 27.3 ms | 15.4 µs | 6.49 µs | 0.62 µs | 4,208× | 2.4× |
| Butterfly garden | 600 | 6.1 ms | 20.6 µs | 1.85 µs | 0.68 µs | 3,271× | 11.1× |
| Gnome dig | 600 | 129.1 ms | 55.4 µs | 11.5 µs | 3.76 µs | 11,233× | 4.8× |
Gnome world’s speed run, three ways#
This report’s
Gnome world’s 🏃 speed run, from a fresh world to its last gem, 44,933 steps, takes 25.5 s of steps with the rules compiled by hazel-jit as the page loads, 24.7 s with the same rules written by hand in plain JavaScript, and about 4.2 minutes interpreted, estimated from the 42,000 steps it ran in its 228 s. With hazel-jit the rules are a small part of the run: 0.34 s of its 25.5 s (1.3%; plain JS 0.09 s, 0.4%). The rest is the page’s own work at each step, the same in all three ways: the water seeping and flowing, the speed run’s search for its way, the beavers’ trees, the creatures. Interpreted, the rules are 89% of the steps’ time.
- The run:
node/world-speedrun.mjsin headless Chrome, three pages one after another: each a fresh world (seed 7, Dad), the speed run started, its steps run by the page’s owntickin chunks of 2,000 with no drawing (the drawing is the same in all three ways), timed, until the speed run ends at its last gem (the dig after it not run). Hazel-jit: the hazeljs branch’s library from build.hazel.org, as the page loads it, hazel-jit as of today. Plain JS:world/plain.jsput in place of World.step, World.beaver and World.water_row (what else the run calls stays Hazel’s: 15 villagers’ greetings, and 65 trees’ and mushrooms’ roots grown as it came near them; with no drawing, none grow by the view). The interpreter: the same library’sHazel.runProgram, for 220 s, so that its run, its replay and its two builds keep within five minutes. - What “plain JS” was in this run (said 2026-11-27,
^ticket(896)): the run put the gnome’s step, the beavers’ and the river’s level in plain JavaScript (world/plain.js); the pieces already moved into Hazel by then, the boulders, picking things up and the wind, stayed Hazel’s in it, as the page never read the run’s?rules=plain. The run now uses the page’s own ⚡, every piece in JavaScript: on 0.0.148, every piece Hazel’s took about 6% longer than every piece in JavaScript (^report(64), Then and now). - A step takes 0.57 ms with hazel-jit (about 1,765 steps a second), 0.55 ms in plain JS, and 5.4 ms interpreted (about 185 a second). The page runs the speed run as fast as it can, about 12 ms of steps a frame: with hazel-jit, about 21 steps a frame at 60 frames a second, on this runner; interpreted, about 2. A slower computer takes fewer a frame, down to one, and the speed run goes no faster than play (Gnome world’s ⏱ measures it there, both ways).
- The rules alone: World.step replayed on every tenth of the run’s own 44,933 calls (4,493 of them): hazel-jit takes 2.58 µs a call, plain JS 0.51 µs, the interpreter 4.6 ms. Hazel-jit takes 5.0 times plain JS’s time, and the interpreter 1,767 times hazel-jit’s. Every answer is the same as hazel-jit’s.
- The same world, step for step: a fingerprint of the world every 2,000 steps (the frame, the gnome, the beavers, the things and the gems found): plain JS against hazel-jit, 23 of 23; the interpreter against hazel-jit, 0 of 21: plain JS the same world as hazel-jit at every one; the interpreter’s world parts from the first, by design: interpreted, the page grows the trees’ roots its own way (Hazel’s roots only with compiled rules), and the roots guide the water. (Getting there: each run’s page now begins from the same world, step for step: the same frame count, weather and clocks, the view the same size, and Gnome world 0.0.132 sets the water’s direction as a world is made; before, the runs parted by step 14,000 though every answer was the same.)
- The page’s own work at each step is the water, nearly all of it: under ⚙️ Advanced, Gnome world’s ⏱ (0.0.130) times a step’s parts on any device; on the laptop, the water was 96% of a step, the gnome’s own step 1%. Gnome world 0.0.131 steps the water only where it moves, and the speed run plans its order and dashes (0.0.130): this run’s step is half the time of the same run on 0.0.127, and its run 44,933 steps where it was 55,537.
| Way | steps run | the steps’ time | a step | in the rules | the rules’ share | World.step alone | answers the same |
|---|---|---|---|---|---|---|---|
| plain JS | 44,933 | 24.7 s | 0.55 ms | 0.09 s | 0.4% | 0.51 µs | 4,493 of 4,493 |
| hazel-jit now | 44,933 | 25.5 s | 0.57 ms | 0.34 s | 1.3% | 2.58 µs | 4,493 of 4,493 |
| the interpreter | 42,000 (of 44,933) | 227.6 s | 5.42 ms | 201.58 s | 88.6% | 4.6 ms | 3,280 of 3,280 |
Speed beside them#
This report’s
Only one small rule has been measured with all of them: measured again today, ReScript’s and Melange’s code runs it about four times as fast as hazel-jit’s with the trial’s changes, and the hand-written JavaScript about 2.4 times (today’s numbers). On 2026-11-07 it was about three times and twice; hazel-jit’s Ints have become exact since, and its functions count steps. ^report(46)’s numbers, in Node 22 on the same laptop:
| Compiler | µs a call, median | the fastest call |
|---|---|---|
| Melange 5.0.0 | 0.46 | 0.35 |
| ReScript 12.3.1 | 0.55 | 0.38 |
| js_of_ocaml 5.9.1 (the rule inlined into the loop) | 0.71 | 0.40 |
| JS written by hand | 0.90 | 0.71 |
| hazel-jit, 2026-11-07 | 1.59 | 0.66 |
| Hazel's interpreter | 358 |
That hazel-jit had records as arrays, field reads through a call, and an equality too big to inline; and its Ints were plain JS numbers, with no step counted.
The games and activities of hazeljs-examples have hazel-jit’s numbers but none for the other compilers. ^report(63) timed every game four ways (on the interpreter, through the jit, compiled ahead of time, and, for five games, in JavaScript written by hand), and ^report(54) timed each game’s own run of states as its page plays it. Neither has ReScript, Melange or js_of_ocaml: for them each game’s rules must be written again, in OCaml (for Melange and js_of_ocaml) or in ReScript. Since 2026-11-25 every one of the 14 games has a version in plain JavaScript, written by hand: Checkers, Go, Minesweeper, Mahjong and Solitaire from before, and nine more at Matthew’s ask (“Why don’t we have plain JS data for the later part of the table? Can we get it?”), each checked to give the same answers as its compiled.js at every step of many games. They come first (What next), so that each has all six: the interpreter, hazel-jit, ReScript, Melange, js_of_ocaml and the hand-written JavaScript, each playing the game’s own run of states in its own layout.
Conversion at the edges, over time#
This report’s
Converting values where compiled code meets the rest went from most of a call to a fraction of one in two days, and it is the larger part again where a whole state crosses: on ^report(46)’s Petri rule, called as a page calls it, 4.7 µs of a 6.67 µs call is conversion, the same as before today’s changes (4.8 µs), so its share grew from 58% to 71% as the compiled code got faster. Matthew asked on 2026-11-25 for this section: conversion’s cost over time, and ways to bring down what is left, among them one he and Cyrus had talked about, the interpreter keeping values as compiled code holds them, so that mixed mode does not convert them at every call.
Build by build#
Each step below changed what happens where values cross, or, last, only the compiled code. The yardstick is the same throughout: each game’s own compiled.js, compiled ahead of time by an older hazel-jit, with the converters a person wrote, unchanged since ^report(63)’s runs. The jit’s ratio to it falls for two reasons, less conversion and better compiled code; the steps say which.
compiled.js, on each game’s own run of states as its page plays it (the geometric mean over 14 games). From ^report(63) (builds 5 and 6), ^report(54) (records as objects) and today’s runs (0.0.13 and the loops and JS arguments). Before the converters, on one position again and again rather than on runs, the jit took 7.5 times the ahead-of-time time (^report(63)).| When | Build | What changed at the edge | the jit / compiled.js |
|---|---|---|---|
| 2026-11-23 | hazeljs 0.0.11 (build 2, 98c482395d) | through Hazel's own values: a page's value into Hazel's, then into compiled code's shapes, and back the same two steps | 7.50 |
| 2026-11-24 | build 5 (a97b6f7465) | converters by declared type: a page's values straight into compiled code's shapes and back | 0.91 |
| 2026-11-24 | build 6 (ca851b422d) | what was converted remembered by links on the objects, not in WeakMaps | 0.76 |
| 2026-11-24 | records as objects (f311f4e408) | a page's record crosses as it is | 0.48 |
| 2026-11-25 | hazeljs 0.0.13 (20febee548) | the same as above, timed again today | 0.52 |
| 2026-11-25 | loops and JS arguments (6d041e9769) | none at the edge: the compiled code alone | 0.35 |
| 2026-11-25 | all of today’s changes (2c4a61560f) | none at the edge: constructors made once, modules by name, === against a literal | 0.34 |
What a call still converts#
^report(46)’s Petri rule on the laptop, called 2,000 times, five rounds, the median µs a call. Direct: the compiled program called with values already in compiled code’s shapes (petri-bench.mjs hazel). Through the jit: Hazel.jitProgram’s function, called with the page’s shapes and answering in them (petri-bench.mjs jit); the difference is converting. 0.0.13: 3.45 direct, 8.29 through the jit. Today’s, with steps 1 to 3 of What is still left: 1.92 and 6.67.What each Petri call converts: the net, whose marking is a new array of five Ints every call (five list cells in), whose transitions are the same object every call (remembered, so nothing), and the event; and the command it answers, a chain such as Say(_, Take(_, _, Put(_, _, Done))), built new each call and so converted node by node, from {$c, $v} to the page’s {tag, value}. Where the rest of the cost lies, in general:
- A list crosses cell by cell, unless it was handed back unchanged: about 6 µs for 1,000 Ints, 62 ms for a million (
^report(54), A page’s list in and back out). - A constructor crosses node by node, since the page holds
{tag, value}and compiled code{$c, $v}; a record now crosses as it is. - Whatever is new each call misses the converters’ memory: given a fresh copy of every state, the jit took 2.2 times its own time (
^report(63), A game’s own run of states). - Mixed mode, where the interpreter calls compiled functions, converts every call’s argument from the interpreter’s terms into JavaScript and its answer back by the declared type, with no memory between calls. In mixed mode’s first measurements, a one-line function called 500 times from interpreted code was slower than not compiling it (
^report(54), Mixed mode).
Ways to bring it down, soon#
- The interpreter keeps compiled values as they are. In mixed mode, a compiled call’s answer stays the JavaScript value, held by the interpreter as a value of a new kind: the JS value and its type. Passed to another compiled function, it goes as it is. Only when the interpreter looks inside it (a case, a projection, an operator, a probe, the answer shown) is it converted, one layer at a time, each layer’s parts kept as they are until they are looked at. A game’s state handed from step to step through interpreted code would then never be converted. The cost: a new form of value in the interpreter, which its steps, its matching and its printing must know, and the way back for the parts it shows.
- Mixed mode remembers what it converted. As the package’s converters do for a page: under js_of_ocaml an OCaml value is a JavaScript object, so a
WeakMapcan lead from the interpreter’s term to its JavaScript value, and a link on the JavaScript value back to the term. A state the interpreter hands back unchanged then crosses with no walk, and only what changed is converted. Less to build than the first, and it covers the case where the interpreter only passes values along. - Constructors in one shape. If the page and compiled code held a constructor the same way, it would cross for nothing, as a record does now. Either way round, pages that keep compiled code would see the change, as they saw the records’.
- Lists as arrays at the edge (
^ticket(846), how Seq enters Hazel): a page’s array read where it is, not turned into cells first. Lists as cells plus array segments cost 8% in compiled code (^report(54)), so a type the program asks for, not every list. - The converters call the direct form. A page’s call with several arguments is gathered into one array, which the function’s one-argument form takes apart again; the converters could call
$dwith the parts.
The first two would show in mixed mode, which nothing in the app turns on yet; the last three in every page’s call through Hazel.jit.
What is still left#
This report’s
Five steps, in the order Matthew set on 2026-11-25, each a ticket on the work dashboard; the first three are made, timed and merged, the fourth is measured, and options unboxed, last, are measured from a bench build and wait on a decision. Matthew: “let’s keep going, skipping 1 (unboxed options) until all others are done”, with the priority below; and “please document our plan for the day as a ticket, or several that link to each other”. The day’s plan is ^ticket(853), whose first two steps (the loops and the JS arguments, above) are merged; each ticket below links to it and to its neighbours. Steps 1 to 3 were one wave on a trial branch, trial/853-jit-more, each its own commit and its own build of the package, timed one after another on the games’ runners, and merged together on Matthew’s word (“Yes, merge them”): hazel-jit is at be44756900, and hazeljs merges it (2c4a61560f).
- Constructors without arguments made once (
^ticket(854)).{$c: "None"}and{$c: "Done"}were made anew at each use; now each name is one constant before the program’s code (k$None$26), since a Hazel value has no identity and a page does not change what it is given. Committed (ae29353ff0). - A module’s members by name (
^ticket(855)).Common.readywas a guarded property read on every call. dev lowers a module to lets ending in a record of its members, so a let bound to a record written out whose entries are all variables is remembered, andCommon.readycompiles to the member’s own variable; a call with its tuple written out then reaches the member’s direct form by name (ready$51.$d(...)). Committed (9dd8fce210). ==against a literal as===(^ticket(856)).$R.eq(i$6, 0)becomesi$6 === 0wherever one side is a number, string or boolean written out:$R.eq’s first test is that comparison, and its answer for anything else is false, as===’s is. Constructors keep$R.eq. Committed (0ce95a2e8f), with tests for all three (6801508ed4).- What exact Ints and the step count each cost (
^ticket(857)). Both are Hazel’s meaning, so they are measured, not removed: two builds only for the bench, on top of step 3’s, one with Ints as plain JS numbers and one with no step counted. On the Petri rule, on the laptop, exact Ints cost about 7% of a call (1.77 µs against 1.64 without them) and the step count about 21% (1.40 without it); the games’ numbers are coming. - Options unboxed, last (
^ticket(858)):Some(t)astandNoneasundefined, as ReScript and Melange have them (with a box forSome(None)). None of the 14 games uses an option (only the Petri rule’spickdoes), so it would not show on the games; and it changes compiled code’s shapes, which pages that keep compiled code read, as the records did, while the way back would read an option only by its type, which it does not always have. A first measurement, from a build only for the bench that changes the compiled code alone (trial/853-pkg-opt, bee76969b5:Noneasundefined,Some(x)asx, boxed when nested): on the Petri rule, 1.82 µs a call against step 3’s 1.95 (seven rounds each, their ranges overlapping), oneSomesaved a call. Merging it would also need the way back, the package’s converters and the agreement tests to read the new shape; for Matthew to decide.
Alongside, ^ticket(859): this report’s draft of #2859’s description, until Matthew says to post it.
Steps 1 to 4, timed#
Together, steps 1 to 3 take 0.95 of the base’s time on the games, about as much as the runner’s noise (1.04 for the same file); exact Ints cost about 12% of a call, and the step count about 21%. Each game’s own run of states, six builds one after another on one runner (bench/853-more, GitHub run 45645748545): the base (the loops and JS arguments, as merged), then each of steps 1 to 3 on top of the one before, then two builds only for the bench on top of step 3, one with Ints as plain JS numbers and one with no step counted. Every answer the same. Each step against the one before: constructors made once 0.95, modules by name 0.98, === 1.01. A single game moves far more than that between runs (Light trails’ own compiled.js took 1.49 of its first time in a later build on the same runner), so none of the three is told from noise on its own. Plain Ints helped most where the rules do arithmetic: Downhill 0.37, Star wave 0.67, Bricks 0.71. With no step counted: Checkers 0.59, Star wave 0.69, Mahjong 0.70. (Those two are shares of step 3’s time; in the table below every build is a share of the base’s, and the last two include steps 1 to 3.)
| Game | base | constructors once | modules by name | === too | Ints plain | no steps |
|---|---|---|---|---|---|---|
| Checkers | 13.5 µs | 0.88 | 0.85 | 1.08 | 0.89 | 0.64 |
| Go | 11.2 µs | 1.00 | 0.99 | 0.92 | 0.98 | 0.74 |
| Minesweeper | 8.27 µs | 1.25 | 1.00 | 0.97 | 0.99 | 0.90 |
| Mahjong | 13.3 µs | 1.07 | 0.97 | 1.10 | 0.90 | 0.77 |
| Solitaire | 3.67 µs | 0.98 | 0.90 | 0.88 | 1.05 | 0.69 |
| Mahjong solitaire | 10.9 µs | 0.97 | 0.97 | 0.98 | 0.98 | 0.84 |
| Pinball | 9.61 µs | 0.94 | 1.02 | 1.00 | 0.90 | 0.81 |
| Downhill | 2.97 µs | 0.95 | 0.92 | 0.97 | 0.36 | 0.87 |
| Bricks | 1.74 µs | 1.07 | 0.86 | 1.02 | 0.72 | 0.78 |
| Snake | 4.66 µs | 0.90 | 0.90 | 0.88 | 0.87 | 0.76 |
| Light trails | 9.95 µs | 0.53 | 0.54 | 0.59 | 0.89 | 0.85 |
| Star wave | 6.95 µs | 1.04 | 1.00 | 1.00 | 0.67 | 0.69 |
| Butterfly garden | 1.77 µs | 0.99 | 1.34 | 1.06 | 1.20 | 0.86 |
| Gnome dig | 10.9 µs | 1.00 | 1.07 | 0.97 | 0.88 | 0.80 |
| geometric mean | 0.95 | 0.94 | 0.95 | 0.85 | 0.78 |
On ^report(46)’s Petri rule, on the laptop (five rounds, the median µs a call): the base 1.97, constructors once 2.00, modules by name 2.22 (its five rounds went from 1.88 to 2.87), === 1.77; on top of that, Ints plain 1.64 and no steps 1.40. The three changes do show in the compiled code: k$None$26 for every None, pick$27.$d(...) for Common.pick(...), and === 0 fifteen times in the rule.
What follows from it: the step count is the largest single cost left in a call that the compiler could make cheaper without changing the meaning, for instance by counting in a variable of the program’s rather than a property of $R, or once a loop’s pass rather than once a call; and exact Ints might keep their meaning at less cost with the safe-integer check written in place, (r = a + b, r <= M && r >= -M ? r : $R.iadd(a, b)). Both would be measured as these were.
Choices for the description#
This report’s
Six things to settle before the description goes on GitHub. Each is a choice for Matthew; the draft above takes the first option of each.
- How long the summaries are. Six points each now. Or shorter: three sentences for the executive summary, and the engineering summary’s points as one paragraph.
- Which speed to quote first. The draft quotes the games compiled ahead of time against the interpreter (2,392 times as fast, the geometric mean over 45 operations, measured with hazeljs 0.0.11 for
^report(63)). The other candidates: the evaluator tests’ median, 395 times; or one game’s step, Gnome dig’s 82 ms against 0.28 ms through the jit. The first is the broadest, but its build is two days older than the layout this PR has. - Whether the comparison with ReScript, Melange and js_of_ocaml belongs in the PR. It helps a reviewer who knows those compilers; it also invites the question of why hazel-jit doesn’t use one of them (
^report(46)’s answer: none compiles Hazel, and hazel-jit compiles in the page while the program runs). Or leave it to this report and link it. - Whether to say what the sweep costs CI. Every evaluator test runs a second time, compiled, and a third, mixed. Not measured yet: the Fast checks’ time on
devagainsthazel-jit. - Whether the reports are linked from the PR. The draft links reports 046, 054 and 063 at its end. Another choice is to link this report once, for the details and the history.
- The compiler’s header comment, out of date.
HazelJit.reopens with the layout of values, and on #2859 it still says a tuple is an array with its labels as properties. Fixed on today’s trial (50b70d5a40), to come with it.
The text for GitHub#
This report’s
The whole description as GitHub will show it, 4,307 words, in one file: pr-2859.md, the same file the sections above are rendered from. It is posted only when Matthew says so, with gh pr edit --body-file.
The Markdown
🤖 hazel-jit, for `dev`: a compiler from Hazel to JavaScript, with the tests that hold it to the interpreter's answers. This is the one PR that tracks the jit. The npm package's branch, #2857 (`hazeljs`), merges it rather than keeping a copy of its own.
## Executive summary
- **What it is.** hazel-jit compiles a complete Hazel program to JavaScript and runs that in place of the interpreter. Where it can't (a hole, a test, an error the interpreter would show), the interpreter runs the program as it does today, so an answer never changes.
- **Why.** Speed, toward 60 frames a second for small edits. Compiled ahead of time, the games of hazeljs-examples run 2,392 times as fast as on the interpreter (the geometric mean over 45 operations in 17 games). Gnome dig's step takes 82 ms on the interpreter and 0.28 ms through the jit.
- **What it changes in `dev`.** It adds new files, and 13 lines to two of `dev`'s, `Builtins.re` and `Transition.re`, for mixed mode. Nothing in the app turns hazel-jit on yet: the editor, the evaluator and the stepper run as before.
- **How it is checked.** Every evaluator test also runs compiled, where it compiles, and must give the interpreter's answer. Of the 289 checks, 234 compile; 211 of those give the interpreter's answer and none differs. The other 23 end where the interpreter's result is not a value, which the sweep doesn't compare. Mixed mode is held to the interpreter the same way, as are 2,000 random programs, and a floor fails the build if fewer programs compile. hazel-jit also has 13 tests of its own.
- **Who uses it.** The hazeljs npm package (#2857): its `Hazel.jit` and `Hazel.compile` are hazel-jit. And the games of hazeljs-examples, which keep their rules compiled ahead of time.
- **What to review.** The two hooks into the interpreter; the shapes compiled code gives values, which pages that keep compiled code depend on; and the list of what is left to the interpreter.
## Engineering summary
- **In and out.** The input is the elaborated term the interpreter would run, with the types the statics gave it. The output is JavaScript source: the body of a function of one argument, `$R`, the runtime (about 23 KB of source: equality, lists, numbers, the builtins). The caller makes it a function with `new Function` and calls it.
- **How the code is written.** Expressions all the way down. A `let`'s bindings are assigned, in a comma expression, to variables declared at the top of the enclosing function, so a `let` makes no closure. Every binding gets a JS name of its own (`x$12`), so shadowing can't clash. A builtin written in OCaml is the runtime's JS version of it; one written in Hazel (`List.map`) is compiled once, before the program.
- **Values.** Close to what a person would write in JavaScript: numbers, strings and booleans as themselves, a record as a plain object, a list as cells `{$h, $t}` (so `::` is O(1)), a constructor as `{$c, $v}` (the table below). An Int is exact, as the interpreter's is: a JS number while it is a safe integer, a BigInt past that. An SInt is 32 bits, each result checked. A field read `e.x` is the property itself, behind a check.
- **Calls and loops.** A function bound to a name whose parameter is a tuple pattern takes the parts as JS arguments, so a call with the tuple written out makes no array: `fire_at.$d(n, i)`. A recursive function's calls of itself in tail position are a `for (;;)` loop, so a long list doesn't overflow the stack. Every other caller still passes the tuple, to the function's one-argument form. A module's member is read as its own variable (`Common.pick` is `pick`), a constructor without an argument is one constant made once, and `==` against a number, string or boolean written out is `===`.
- **What is left to the interpreter.** By rule, not by gap: holes outside mixed mode (below), the stepper's filters, an operation the interpreter stops at with an error, undefined, and a run past its budget. For a form, the compiler raises `Unsupported(reason)`; at run time, the compiled run stops. Either way the interpreter's run is the answer.
- **Back to Hazel.** `HazelJitValue` turns a compiled value back into a Hazel value by the types the term records, or into a diff against the last answer.
- **Mixed mode.** The interpreter runs a program around its holes and calls its fully typed functions (whole types declared) compiled, as builtins made at run time. A call that can't finish compiled falls back to the interpreter, for that call alone.
- **Holes in mixed mode.** A hole in an argument crosses into compiled code as an opaque value and comes back as the very term; a function with a hole in its code compiles, the hole an opaque value too. Code that would look inside one (match it, add to it, branch on it) runs in a checked version that stops there, and that call falls back. An answer holding a hole carries the interpreter's casts, made by the interpreter's own code (`Ascriptions`). A list of 2,000 built with a hole, then its length, runs about 17 times as fast as on the interpreter, where before the hole sent the call back.
- **Speed on the tests.** Over the 208 evaluator programs that compile and agree, the median program runs 395 times as fast compiled, run after run. On a first run, with its JS parsed, it runs 6.9 times as fast; compiling all 208 took 65 ms, against the interpreter's 143 ms for their first runs.
## What it holds
| Part | Files | What it does |
|---|---|---|
| The compiler | `src/language/dynamics/HazelJit.re` | An elaborated term as the body of a JavaScript function of its runtime (`$R`), and the runtime's source. A form it does not compile raises `Unsupported` with the reason, and the caller runs the interpreter. |
| The values across | `src/language/dynamics/HazelJitValue.re` | A Hazel value into compiled code's shapes and back, by the types the compiled term records; a compiled program instantiated; an answer carried back as a diff against the last one. |
| Mixed mode | `src/language/dynamics/HazelJitMixed.re`, 5 lines in `Builtins.re`, 8 in `Transition.re` | The interpreter calls a program's fully typed functions compiled, as builtins made at run time, holes and all. Nothing in the app turns it on yet. |
| Tests | `test/Test_HazelJit.re`, `test/Test_HazelJit_Agree.re`, a hook in `test/evaluator/Test_Evaluator_Prelude.re` | Every evaluator test also run compiled (and mixed) where it compiles, compared with the interpreter's answer shape by shape; agreement on the language's forms, big Ints, invalid operations, strings, partial and mutual functions, the step limit, undefined, labels, Unicode, field reads, records whose labels look like the runtime's own keys, loops and JS arguments; values carried back; diffs; what is not compiled and why; mixed mode, holes in it carried and fallen back. |
## How compiled values look
| Hazel | Compiled JavaScript |
|---|---|
| A tuple whose every entry is labeled, `(x = 1, y = 2)` | A plain object, `{x: 1, y: 2}`, its keys in the type's order |
| Any other tuple, `(1, 2)` or `(1, y = 2)` | An array, carrying its labels under `Symbol.for("hazel-jit.labels")` |
| A list | `null`, or a cell `{$h, $t}` |
| A constructor, `Some(3)`; `None` | `{$c: "Some", $v: 3}`; `{$c: "None"}`, one constant made once for each constructor without an argument |
| A function | A JS function of one argument; a function of a tuple takes an array. A named function of a tuple pattern also has a direct form, `f.$d(a, b)`, its parts as JS arguments |
- A field read `e.x` compiles to the property itself, `e["x"]`, behind a check that `e` is an object holding `x` and not a list's cell. Anything else (a list of rows, the empty list, a value the interpreter would be stuck on) goes to the runtime's `$R.dot`, so the answer is the same.
- A label named `length` or `__proto__`, or starting with `$`, is kept as `$` and the label. So no record has the keys `$h`, `$t`, `$c` or `$v`, and a record is never taken for a cell or a constructor.
- `==` is `$R.eq`, which is small enough for the engine to inline: its first line compares two numbers, strings or booleans with `===`, and objects go on to `$R.eqo`. Floats compare with `===` directly.
These shapes are Matthew's decisions of 2026-11-24. Measured on the games as their pages play them, records as objects take 0.63 of the time that records as arrays took (the geometric mean over 14 games), with every answer the same.
## Beside other compilers to JavaScript
| Value | hazel-jit | ReScript 12 | Melange 5 | js_of_ocaml 5.9 |
|---|---|---|---|---|
| A list cell; the empty list | `{$h, $t}`; `null` | `{hd, tl}`; `0` | `{hd, tl}`; `0` | `[0, hd, tl]`; `0` |
| A record | an object by label | an object by field | an object by field | `[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]` |
| `Some(t)`; `None` | `{$c: "Some", $v: t}`; `{$c: "None"}` | `t`; `undefined` | `t`; `undefined` | `[0, t]`; `0` |
| A function of two | a named one: `f.$d(a, b)`; else one argument, an array | `f(a, b)` | `f(a, b)` | `f(a, b)` |
| `i == 0` on Ints | `i === 0` against a literal; else `$R.eq(a, b)` | `i === 0` | `i === 0` | `0 === i` |
| A tail call | of itself: a loop; else a call | a loop | a loop | a loop |
hazel-jit's names: `$h` and `$t` are a list cell's head and tail, `$c` and `$v` a constructor's name and value, `$d` a named function's direct form, and `$R` the runtime compiled code is given. They begin with `$` because a record's label that does is kept under `$` and the label, so no record has these keys, and one key tells a cell or a constructor from a record. ReScript and Melange name a cell's parts `hd` and `tl`, and a constructor's `TAG`, `_0`, `_1`; js_of_ocaml uses arrays, the tag first.
hazel-jit's records, its calls of named functions and its loops now look like ReScript's and Melange's; its constructors and options do not yet. On one small rule (a Petri net's step), ReScript's and Melange's code runs about 4 times as fast as hazel-jit's (measured 2026-11-25), where hazel-jit's Ints are exact and every function counts a step for the run's budget.
## Where it comes from
The compiler and its tests come from the livelits line's `trial/794-games-aot` (the jit of #2817), ported to Jane Street Core, with the arms for livelit-only forms (`Quote`, `Unquote`, `Bind`, `Splice`, a module's `ModVal`) taken out. The livelit views compiled (`HazelJitRun`, `HazelJitDOM`) and their tests stay on the livelits line.
Since it opened:
- **Records as objects** (9ec865976d), above.
- **A recursive function's own capture**, undefined when the function was made, is bound again by its `FixF` on the way back (5ec4ff4307).
- **A pattern by position that meets a labeled entry** leaves the run to the interpreter (e0a5925f3b).
- **A recursive function's calls of itself in tail position are a loop** (50b70d5a40), its variables declared in each pass, so a closure keeps its pass's values.
- **A named function of a tuple pattern takes the parts as JS arguments** (43828037c9).
- **A constructor without an argument is one constant** (ae29353ff0), **a module's member is read as its own variable** (9dd8fce210), and **`==` against a literal is `===`** (0ce95a2e8f): together 0.95 of the jit's time on the games. With both, the games take 0.70 of the jit's time as their pages play them (the geometric mean over 14 games), every answer the same; nearly all of it is the arguments.
- **Int division is `Math.trunc(a / b) + 0`** (136fecdcaf): exact for safe integers, checked against BigInt on two million divisions, twice as fast, and never `-0`, where the old form gave `-0` for a negative divisor and no quotient (`-1 / -2`), which shows through a Float as `-infinity`. **Int arithmetic on numbers written out is folded at compile time** (664c49c49a). Together, Gnome world's `World.step` takes about 42% less time.
## Mixed mode, at its boundary
Mixed mode keeps the interpreter in charge, so holes, probes and the stepper work as they do, and calls a program's complete, fully typed functions compiled.
- **What is compiled.** A `let` at the top of the program that binds a function whose whole type is declared (`let f : A -> B = fun ...`), with no unknown in it. Each is compiled with the accepted functions before it, so it may call them. A function that names anything else is not compiled, and the reason is kept. A module's members run on the interpreter for now (below).
- **How it hooks in.** The program is rewritten before it runs: the interpreter still evaluates each such definition to its own closure, then hands it to a builtin made at run time, which gives back a second builtin, the compiled function. The interpreter's one change is to look builtins up in a table filled at run time, after its fixed one.
- **A call.** The argument is converted into compiled code's shapes. The compiled function runs under a budget (50 million calls or 1 s). The answer is read back by the declared result type, which is why the type must be declared: a JavaScript number does not say whether it was an Int or a Float. An answer that may hold a hole is cast to that type by the interpreter's own `Ascriptions`, so mixed mode's answer is the interpreter's, casts included.
- **Falling back, one call at a time.** If the argument will not convert, an operation is invalid (a division by zero), the budget runs out, or the answer will not convert back, the interpreter applies its own closure to the same argument. Evaluation has no effects, so running a call again is safe.
- **What the boundary costs.** Compiled functions call one another directly, so the crossing is paid only where interpreted code calls compiled code, in proportion to what crosses. On build 49: `fib(16)`, all compiled, 72.6 ms interpreted against 0.1 ms mixed; the Petri net rule 120.8 against 16.8 ms; but a one-line function called 500 times from an interpreted loop 22.4 against 27.9 ms, each crossing costing more than its work.
- **Compiled code never calls interpreted code.** A function that calls one the interpreter keeps is itself left to the interpreter, so the boundary is crossed one way only. One function that cannot compile keeps every function that calls it interpreted.
- **Holes cross unopened.** A hole in an argument crosses as an opaque value, its term kept aside. Compiled code may move it (return it, store it in a pair or a record, count a list holding it), and it converts back as the very term that went in, with its cast. Code that would look inside it (match a constructor, branch on it, `x + ?`) stops the call, which falls back, so the interpreter gives the indeterminate answer.
- **Functions as arguments.** A compiled function that passes compiled functions, or lambdas of its own, calls them directly in JavaScript. An interpreter's closure passed in from interpreted code crosses as an opaque value, as a hole does: carried, but calling it stops the call, which falls back. A function in an answer comes back as a Hazel closure when compiled code made it; a recursive one, or one it did not make, makes that call fall back. No test has a function-typed parameter yet.
- **Polymorphic functions.** Compiled code erases types: `typfun a -> e` compiles as `e`, a type application `f@<T>` as `f`, and one compiled function serves every type it is used at, as compiled code never looks at a type at run time. Mixed mode does not compile one, since a candidate's declared type must be an arrow at the top, and a `forall` is not. Reading an answer back at a type variable has no type to go by: across a page's boundary, a whole JavaScript number comes back an Int and any other a Float, so a polymorphic function used at Float could give back `2.0` as the Int `2`. No test covers a polymorphic function across that boundary.
## Types: errors, type holes and unknowns
Hazel is gradually typed: the interpreter runs code with type errors and unknown types, inserting casts and stopping at one that fails. Compiled code erases casts, so what it leaves to the interpreter is decided by where a cast could fail.
- **Static type errors.** The interpreter runs ill-typed code and stops at the failed cast. The editor compiles no program with a static error (the evaluator sweep's 30 ill-typed programs). The package's `Hazel.jit` and `Hazel.jitProgram` compile one anyway and return it with `ok: false` and the errors, leaving the choice to the page: Gnome world's page uses no rules with errors.
- **A type hole written, `?`, is a hole.** A program with one is not compiled ("a type hole"): a cast through it can fail at run time with no static error, as `f(([1.5, 2.5] : ?))` for `f` taking `[Int]` does, and compiled code, its types erased, cannot tell an Int from a Float.
- **An unknown type nobody wrote is compiled, and there compiled code can part from the interpreter.** `let f = fun x -> x + 1 in f(true)` has no static error, as `x`'s type is unknown. The interpreter stops at the failed cast (`true` is not an Int); compiled code answers `2`. The same happens inside a fully typed function, `let f : Bool -> Int = fun b -> let g = fun x -> x + 1 in g(b)`, so mixed mode's candidates can meet it too. The evaluator sweep has no such program. A fix to decide: compile a check where a value is cast out of an unknown type into a ground type (a Bool, a String, a list, a constructor: a `typeof` or a key test), stopping the run so that the interpreter answers. An Int and a Float through an unknown type stay alike, as both are JavaScript numbers, unless Floats are tagged.
## Checked against the interpreter, in CI
The interpreter is the specification, and CI holds the jit to it on every push: a program whose compiled answer differs from the interpreter's fails the build.
- **The sweep, over every evaluator program** (`test/Test_HazelJit_Agree.re`, hooked into `Test_Evaluator_Prelude.evaluation_test`). Each program of the evaluator's own tests is elaborated, run by the interpreter, and, where hazel-jit compiles it, run compiled. The two answers are compared shape by shape: numbers by value (a float within a relative 1e-9); lists, tuples, labels and constructors by structure; a function only as a function. Where the interpreter stops at an error (a division by zero), the compiled run must stop at the same one. Where its answer is left indeterminate, the compiled run must stop as indeterminate.
- **Mixed mode, swept the same way.** For every evaluator program with a top-level function of a declared type, those functions are compiled and the rest interpreted. A different answer from the interpreter's, or a run or rewrite that raises, fails that program's test.
- **Random programs** (Full tests). The generator of the evaluator's "Evaluator does not crash" property (`QCheck_Util.arb_exp`) makes 2,000 programs at a fixed seed, so a failure can be reproduced. Each one with no static error that the interpreter finishes within 10,000 steps is compiled where it compiles, run, and compared as the sweep compares.
- **A floor on what compiles.** At least 234 of the evaluator's programs must compile, and at least 6 in mixed mode, so a change that leaves more programs to the interpreter fails the build. The floor is checked when the whole sweep has run.
- **The agreement tests** (`test/Test_HazelJit.re`). These are written by hand, each run both ways and compared the same way. They cover the language's forms, big Ints, invalid operations, the string builtins, partial and mutual functions, the step limit, labels and Unicode, plus values carried back and diffs.
On this branch (31c7c89de1, CI run 46157399352):
| | programs |
|---|---|
| evaluator checks (Fast checks; Full tests 290) | 289 |
| compiled and run (the floor: 234) | 234 |
| the same answer | 211 |
| a different answer, or the compiled run threw | 0 |
| the interpreter's answer is not a value (an error or a stuck term), checked as above | 23 |
| not compiled: holes 15, type holes 10, free variables 24, other forms 6 | 55 |
| mixed mode: programs with a function compiled, all the same answer | 6 (2,263 calls compiled, 8 fell back) |
| random programs made; with no static error; finished by the interpreter | 2,000; 80; 80 |
| random programs compiled; the same answer; not a value, not compared | 61; 60; 1 |
**What it does not hold the jit to yet:**
- **Ill-typed programs.** Thirty tests are listed as known (`known_not_a_value`): the interpreter stops at a failed cast, while compiled code, its types erased, runs on. The editor compiles no program with a static error; see *Types: errors, type holes and unknowns*.
- **Programs that are not compiled.** A program the jit does not compile runs on the interpreter, so it cannot disagree; the floor keeps that number from growing.
- **Few random programs.** The generator makes mostly ill-typed programs: of 2,000, 80 had no static error and 61 compiled.
- **Only the evaluator's programs.** The games' rules in hazeljs-examples are checked in that repository's CI against hand-written plain JavaScript twins: each piece of Gnome world against its twin, the speed run's world state step for step, and every game's answers three ways (an older interpreter, the jit's code compiled ahead of time, plain JavaScript). Plain JavaScript there is a second witness for the games and the baseline for speed, not the specification.
**Systematically, from here:**
1. More random programs that type: 10,000 a run, as "Evaluator does not crash" runs, or a generator of well-typed programs.
2. A check where a value leaves an unknown type for a ground one, so the thirty ill-typed programs agree and come off the known list.
3. The games' rules and their recorded runs as test programs here, each answer against the interpreter's.
## Where it differs from the interpreter on `dev`
- **SInt overflow.** hazel-jit stops a 32-bit SInt that overflows at `IntegerOverflow`; `dev`'s interpreter wraps it (2147483647 + 1 is -2147483648). SInt is to have 64 bits everywhere, with overflow an error (#2828). The jit's 64-bit SInt is on `trial/758-sint64-jit` and comes here with #2828. The evaluator tests have no such program, so the sweep does not meet it.
- **A cast out of an unknown type nobody wrote** is erased, so where the interpreter stops at the failed cast, compiled code runs on (`let f = fun x -> x + 1 in f(true)`: the interpreter's failed cast, compiled code's `2`). See *Types: errors, type holes and unknowns*.
- **A tuple with a label given twice** is not compiled, since keying it by its labels would lose one entry. The interpreter runs it, keeping both entries as `dev` has since #2472.
## Next, on this branch
- Mixed mode's types from the statics, not from annotations: `dev` lowers a module to lets whose patterns lose their annotations, so a module's members run on the interpreter in mixed mode for now.
- Holes beyond mixed mode: a whole program, a livelit's view and the package's `Hazel.jit` still refuse them. Multiholes and error holes, with their environment, stay the interpreter's.
- Values across without converting: in mixed mode, the interpreter keeps a compiled call's answer as compiled code holds it, converting only what it looks inside, so a value passed from one compiled function to another through interpreted code is never converted.
- The games' rules as test programs (hazeljs-examples, compiled whole).
- A check at each cast out of an unknown type into a ground type, so compiled code stops where the interpreter's cast fails.
- Polymorphic functions in mixed mode: the type at each use, from the type application or the statics, to read the answer back by; and a test of one across a page's boundary.
- Tests of a function-typed parameter in mixed mode, from compiled callers and from interpreted ones.
- 64-bit SInt, with #2828.
Tried and not kept: lists as cells plus array segments (`hazel-jit-lists`). On hazeljs-examples' seventeen games it cost about 8% ahead of time and 4% through the jit, all answers the same, so cells stay.
The design and the measurements are in Hazel reports: [report 54](https://matthewhammer.org/hazel/reports/054/#records-decide) (records, lists and holes), [report 55](https://matthewhammer.org/hazel/reports/055/) (holes in mixed mode), [report 63](https://matthewhammer.org/hazel/reports/063/) (every game, four ways) and [report 46](https://matthewhammer.org/hazel/reports/046/) (beside ReScript, Melange and js_of_ocaml).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Revisions#
This report’s
- 2026-11-27, from Matthew and Cyrus’s meeting. Two new sections in the description, answering what they asked: Mixed mode, at its boundary (what a call converts and costs, compiled code never calling interpreted code, holes carried unopened, functions as arguments, polymorphic functions) and Types: errors, type holes and unknowns. Checking the second found a parting: a cast out of an unknown type nobody wrote is erased, so
let f = fun x -> x + 1 in f(true)stops at the failed cast in the interpreter and answers 2 compiled (^ticket(898)); and the package compiles programs with static errors, marking themok: false. Polymorphic functions in mixed mode are^ticket(897). - 2026-11-25, four ways. The interpreter, hazel-jit as of 2026-11-19, hazel-jit now and plain JS, on each game’s own run (Four ways, on the games), at Matthew’s ask.
- 2026-11-25, steps 1 to 3 merged. Constructors made once, a module’s members by name and
===against a literal, merged intohazel-jit(be44756900) andhazeljs(2c4a61560f); Representations, then and now, and the description’s draft say so. - 2026-11-25, conversion at the edges. A new section, at Matthew’s ask: conversion’s cost build by build, what a call still converts (the Petri rule direct and through the jit), and five ways to bring it down, the interpreter keeping compiled values as they are first.
- 2026-11-25, steps 1 to 4 timed. Steps 1 to 4, timed: the three changes together 0.95 on the games, exact Ints about 12% of a call, the step count about 21%.
- 2026-11-25, now is now. Representations, then and now: the table’s “now” is hazel-jit as merged today (43828037c9), and the intermediate columns are gone (Matthew: “replace the "now" column to reflect the current "now" --- no need to keep all intermediate steps”).
- 2026-11-25, the plan as tickets. What is still left is now the day’s plan in Matthew’s order, each step a ticket (
^ticket(853)to^ticket(859)). - 2026-11-25, later still. The loops and JS arguments merged into
hazel-jitandhazeljs; the description’s draft says so (its engineering summary, its tables of values and of the other compilers, what came since it opened). - 2026-11-25, later. Today’s two changes, made and timed (Loops and JS arguments), and
^report(46)’s rule timed again; the representations and the suggestions updated; what is still left. The description itself waits for the trial to be merged intohazel-jit. - 2026-11-25, first version. From the draft sent to Matthew in chat that morning: the executive and engineering summaries are new, and so is the section beside ReScript, Melange and js_of_ocaml. Checked against the code, the draft’s “Ints compare with
===” was wrong: an Int’s==is$R.eq, and only Floats compare with===directly. The sweep’s numbers are from hazel-jit’s own CI.
What next#
- Iterate on the executive and engineering summaries with Matthew, in this report, until he is happy with them; then post the description on #2859.
- Write the rules of Checkers, Go, Minesweeper, Mahjong and Solitaire in OCaml and in ReScript, and time each game’s own run of states six ways: the interpreter, hazel-jit, ReScript, Melange, js_of_ocaml and the hand-written JavaScript, on GitHub’s runners.
- Time steps 1 to 4 of What is still left on the games, one change at a time (
^ticket(854)to^ticket(857)), and merge steps 1 to 3 intohazel-jitandhazeljson Matthew’s word. - Try options unboxed on a trial of their own, timed on the Petri rule, and set out what pages would have to change, for Matthew to decide (
^ticket(858)). - In mixed mode, keep compiled values as they are in the interpreter, converting only what it looks inside (Conversion at the edges, way 1), or first remember what was converted (way 2); measured on a program that hands a state from step to step through interpreted code.
- Constructors in one shape for pages and compiled code, and the converters calling the direct form (Conversion at the edges, ways 3 and 5), timed through
Hazel.jiton the games and the Petri rule. - Measure what the sweep costs CI: the Fast checks and the Full tests on
devagainsthazel-jit. - Bring
HazelJit.re’s header comment up to date with the layout of values.
How this was put together#
The description is one Markdown file, 065/pr-2859.md, in GitHub’s Markdown; 065/gen065.py renders its sections into this page between the report’s own, and carries the file in the page to copy. The sweep’s numbers are the summary lines the agreement tests print at the end of hazel-jit’s Fast checks (hazelgrove/hazel CI run 45540053889, at 9ec865976d): 289 evaluator checks, 234 compiled, 211 agreed, none differed, 23 with an interpreter result that is not a value. Gnome dig’s step is hazeljs 0.0.13 in Node 22 on one laptop, 20 steps, the same answers both ways. The games’ numbers are ^report(63)’s and ^report(54)’s, and the other compilers’ are ^report(46)’s, each named where it is quoted. The layout of values is read from HazelJit.re at 9ec865976d. The numbers the text quotes are in 065/data/, and in the page as JSON.
The data#
- 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 in065/data/.