Skip to content

QuickJS bytecode for an app's modules - #12

Merged
theetherGit merged 5 commits into
mainfrom
bytecode
Nov 26, 2026
Merged

theetherGit merged 5 commits into
mainfrom
bytecode

Conversation

@theetherGit

Copy link
Copy Markdown
Contributor

An app's modules now load as QuickJS bytecode, compiled when the app is built.

Tracker, 1,000 issues (session bc-1, main against this branch, interleaved, 1 warm-up and 3 runs, Stage Manager off):

  • The page starts 19 to 20 ms sooner.
  • CPU from launch to settled is 23 ms lower.
  • RAM 5 s after load is 5.5 MB lower.
  • Idle CPU over 20 s goes from 11.4 to 7.0 ms.

First visits to a route move by 1 to 4 ms in either direction. A route's own chunk is small, so its first visit is spent rendering, not compiling. Those gaps go to the link-scan job.

The full report is compare/results/bytecode.md, generated by make_bytecode_report.py.

What it does

  • calyx --compile <dist> compiles every module of the app folder with the host binary itself and writes x.js.qbc next to x.js.

    • The bytecode keeps debug info but no source text, so error stacks keep their file, line and column.
    • Nothing is evaluated.
    • A file that doesn't compile as a module stays JavaScript, with one line saying why.
    • On a V8 host it says there is nothing to compile.
  • The adapter and sepal() run it after every build, dev included (compile_bytecode in vite/app.js, using find_host()'s binary).

    • Every build compiles every module: 38–41 ms for the Tracker's 45.
    • With no calyx for the machine, the build warns and the app loads its JavaScript.
    • A calyx from before --compile gets its own message.
  • The QuickJS loader uses a .qbc only when its header names:

    • this calyx version
    • this QuickJS-ng version
    • this target triple
    • the module's URL
    • the byte length of the .js it came from, compared with the file's size on every launch; in dev, the .js's hash too.

    Anything else, a cut or damaged file included, loads the .js and prints why: one line per launch for another host's bytecode, one per file otherwise.

  • Bytecode is read only through AppFolder, exactly as modules are, and never from a cache. QuickJS doesn't check bytecode, so a planted file could run anything.

  • --bundle compiles again inside the bundle with its own binary, so a bundle's bytecode always fits its host.

  • V8 ignores .qbc. Both engines take the same set_modules(source, bytecode).

The target is in the header because QuickJS-ng writes native-endian and nothing in a module's bytecode depends on word size, so the four targets should write the same bytes. Only arm64 macOS has been checked, though, and a wrong guess would be memory corruption rather than an error.

What changes for apps

Function.prototype.toString on app code returns the [native code] form; added to README's Limitations. Of the libraries, only Bits UI's getTabbableOptions reads it, and the DOM layer gives the same result either way
the app folder (Tracker) 60 → 105 files, 972 → 1,964 KB on disk; the .js files ship too, for now
a build (Tracker) 1.91 → 1.97 s from nothing, 2.03 → 2.09 s after editing one component
files read for the first screen 38, as before (.qbc instead of .js)
dist host, darwin-arm64 +82,800 bytes

Tests

  • tests/bytecode.rs (hand-written page in tests/pages/bytecode):
    • from bytecode as from source: imports, import.meta.url, dynamic import(), a thrown error's math.js:7:
    • a mismatched magic, calyx version, QuickJS version, target, URL or size loads the source
    • a .qbc cut at 7 points or with one byte flipped loads the source
    • a rebuilt .js next to old bytecode loads the source (by size; by contents in dev)
    • a symlink to a valid .qbc outside the app folder is refused
    • the compiler compiles every module, and a module that stops compiling loses its old .qbc
    • the real Tracker runs from bytecode
    • V8 loads the JavaScript next to bytecode
  • Header unit tests in bytecode.rs.
  • JS: bytecode.test.js (stand-in calyx scripts), plus adapter.test.js (it compiles, and the last build goes whole).
  • bundle.rs: the bundle carries bytecode for every module.
  • pnpm test and the V8 host tests pass locally. On this Mac the examples now build with bytecode, so every QuickJS host test runs the examples from it. Linux CI has no calyx package, so there the examples build as JavaScript with a warning, and tests/bytecode.rs exercises bytecode.

Left for later

  • Dropping the .js from bundles (530 KB for the Tracker).
  • The host's own page script (0.30 ms per launch) stays JavaScript.

🤖 Generated with Claude Code

theetherGit and others added 5 commits November 26, 2026 21:58
`calyx --compile <dist>` compiles every module of the app folder with the
host binary itself and writes `x.js.qbc` next to each `x.js`: QuickJS
bytecode without source text, with debug info, so error stacks keep file,
line and column. Only what changed is compiled, and the bytecode of
modules that are gone is removed. Nothing is evaluated; a file that does
not compile as a module stays JavaScript.

The QuickJS loader takes the bytecode over the source when its header
says this host wrote it for this module: magic and format, the calyx
version, the QuickJS-ng version, the target triple and the module's URL,
and the .js's byte length, compared with the file's size on every launch.
In dev the .js's hash is compared too. Anything else, a cut or damaged
file included, loads the source, with one line saying why. Bytecode is
read only from the app folder, through AppFolder, as modules are: QuickJS
does not check bytecode, so a planted file could run anything.

`--bundle` compiles again inside the bundle with its own binary. V8
compiles JavaScript, and `--compile` on a V8 host says so.

The adapter and sepal() run it after every build, dev included. The
adapter now empties the app folder but for its bytecode, and sepal() puts
back what Vite empties, so a rebuild compiles only the modules that
changed. Without a calyx that can compile, the build warns and the app
loads its JavaScript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The adapter and sepal() keep the last build's bytecode so that calyx
compiles only what changed. When there is no calyx that can compile, it
stayed next to the new build's modules. Outside dev the host compares only
a module's size with what its bytecode came from, so a rebuilt module of
the same size would have run its old bytecode. Now it is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A calyx from before `--compile` takes it for the app folder and refuses
it: "--compile is not an app folder: give the directory the build wrote".
Passed on as it was, that read as if the build had given a wrong folder.
The build now names the binary and says it predates `--compile`. Other
failures are passed on as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A full compile of the Tracker takes 38 ms; compiling only what changed
saved about 12 ms a dev rebuild. It needed the adapter to empty the app
folder but for its bytecode, and sepal() to put back what Vite empties,
and it left a hole: a build stopped after writing its modules and before
`--compile` left old bytecode next to new modules, and outside dev a
change that kept a module's size would have run the old bytecode.

The adapter empties the whole folder again, as on main, sepal() lets Vite
empty it, and `--compile` compiles every module. It no longer compares
bytecode with its module or removes bytecode whose module is gone. The
host still checks the module's size on every launch, and its hash in dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README: the app folder's bytecode and when it is used, the bundle
compiling again, the start-up line under Performance, and under
Limitations that Function.prototype.toString returns the [native code]
form for app code. CLAUDE.md: bytecode.rs, compile_bytecode, and the rule
that bytecode is read from the app folder only. HANDOVER and CHANGELOG:
the job, what it changes for apps, and what is left.

compare/results/bytecode.md, from make_bytecode_report.py: session bc-1,
main against this branch at 1,000 issues, with the per-module breakdown
on main, bytecode sizes, the app folder, build times and the host's size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@theetherGit
theetherGit merged commit 51d4598 into main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant