3 × C + 6: an instruction budget for every JavaScript operation
Every primitive operation in Quench's interpreter now has a ceiling on the machine instructions it may execute, derived from the same operation written in C and enforced by a gate that only lets the number go down.
Wall-clock time is a bad unit for deciding whether a change to an interpreter is good. On our shared build machine the same binary moves by 15 to 20% between runs. A 3% improvement is invisible, and so is a 3% regression, which is worse.
Instructions executed do not have that problem. Under callgrind, with address randomization off, the same binary on the same input gives the same count. So Quench now decides interpreter changes by counting.
The budget
For each primitive operation there is a JavaScript loop that performs it and a C function that does the same thing. The C version is compiled and counted. The budget for the interpreter is then:
budget = 3 × C + 6 (machine instructions per operation)
The 3 says an interpreter may spend about three times what C does on the operation itself: testing a tag, doing the work, storing the result with its tag. The 6 is the fixed cost of fetching and dispatching one bytecode instruction.
Measuring one operation takes a single callgrind run of about 16 seconds. To remove startup and loop overhead, the harness runs the loop for n and 2n iterations and takes the difference:
cost per operation = (instructions(2n) − instructions(n)) / n
The first run
over budget at baseline
string compare, budget 18
one operation, exactly
Fifty-five of 59 operations were over. The worst offenders:
Some of these are startling. Testing whether an integer is truthy, if (n), took 304 instructions. In C that is a compare and a branch.
What 304 instructions of "if (n)" looked like
Integers were falling through to a fully generic conversion. The generic path looked the value up as if it might be an object with a custom conversion, a string, a BigInt, and so on, before concluding that a non-zero integer is true.
The fix handles integers directly, ahead of the generic path.
How the gate works
Budgets live in one file in the repository, one line per operation. A gate step runs every operation under callgrind and fails if any count is above its line.
A new operation starts with its measured count as the ceiling, even if that is ten times the budget. From then on the ceiling can only be lowered. Improve an operation and you lower its line in the same change. Nobody can make an operation slower without the gate failing, and no improvement can be lost later.
This is the same shrink-only idea the project uses for skipped tests, oversized functions and hand-written opcode logic. It turns "we should fix that someday" into a number that can never get worse.
Budgets at every level
Single operations are the bottom of a ladder. The same method applies upward:
| Level | Unit | Reference |
|---|---|---|
| Operations | one bytecode handler | C twin, 3 × C + 6 |
| Primitives per tier | one language feature, in each tier | interpreter ≥ b-jit ≥ u-jit, u-jit within 5x of C |
| Kernels | a small program such as nbody | C program, instructions per iteration |
| Benchmarks | a JetStream 3 test | best of Node and Bun |
At the kernel level the gaps are larger and more interesting. A regular-expression kernel ran 1,371 instructions per iteration against 40 in C. A string-building kernel ran 1,332 against 14. The nbody pair computation ran 1,479 against 21 in C. Those have ceilings now too.
Where instruction counts mislead
Instructions are not time, and it would be dishonest to pretend otherwise.
On one SHA-256 benchmark Quench came out 1.72 times better than Bun by instruction count and only 1.26 times better by wall clock. The difference was the instruction cache: Quench took 26.6 million first-level cache misses where Bun took 7.5 million. The interpreter was executing fewer instructions spread over more code.
So the rule has two parts. Instruction counts decide whether an interpreter change is kept, because they are exact. Wall-clock time, measured as the median of interleaved pinned runs, decides whether we claim anything in public. And code layout is treated as part of the result, not as noise.
What it found on the first day
The budget run also turned up a real bug. With a forced collection between measurements, a captured variable in three of the tests read back as the wrong kind of value. It reproduced on the trunk with the JIT off. Counting instructions had nothing to do with it, except that it made us run 59 small programs in a way nobody had run them before.
That is the usual pattern with measurement. You build it to answer one question and it answers a different one first.