Repository navigation
QuickJS bytecode for an app's modules - #12
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):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 bymake_bytecode_report.py.What it does
calyx --compile <dist>compiles every module of the app folder with the host binary itself and writesx.js.qbcnext tox.js.The adapter and
sepal()run it after every build, dev included (compile_bytecodeinvite/app.js, usingfind_host()'s binary).--compilegets its own message.The QuickJS loader uses a
.qbconly when its header names:.jsit 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
.jsand 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.--bundlecompiles again inside the bundle with its own binary, so a bundle's bytecode always fits its host.V8 ignores
.qbc. Both engines take the sameset_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.toStringon app code[native code]form; added to README's Limitations. Of the libraries, only Bits UI'sgetTabbableOptionsreads it, and the DOM layer gives the same result either way.jsfiles ship too, for now.qbcinstead of.js)Tests
tests/bytecode.rs(hand-written page intests/pages/bytecode):import.meta.url, dynamicimport(), a thrown error'smath.js:7:.qbccut at 7 points or with one byte flipped loads the source.jsnext to old bytecode loads the source (by size; by contents in dev).qbcoutside the app folder is refused.qbcbytecode.rs.bytecode.test.js(stand-in calyx scripts), plusadapter.test.js(it compiles, and the last build goes whole).bundle.rs: the bundle carries bytecode for every module.pnpm testand 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, andtests/bytecode.rsexercises bytecode.Left for later
.jsfrom bundles (530 KB for the Tracker).🤖 Generated with Claude Code