Skip to content

bun pre build #4

Closed
Closed

Description

@xz-dev

简短结论:严格意义上的"预先 JIT"(把优化后的机器码在分发前生成)两者都做不到,因为 JIT 产物本质上依赖运行时信息;但"把解析 + 字节码编译提前到构建期"是可以的——Bun 有真正的构建期字节码预编译(--bytecode),而 Deno 只有运行时生成的 V8 code cache,无法在分发前嵌入。

为什么真正的 JIT 不能提前

两个引擎的执行管线都是分层的:

  • Bun (JavaScriptCore):源码 → 解析 → LLInt 字节码 → Baseline JIT → DFG JIT → FTL/B3 JIT
  • Deno (V8):源码 → 解析 → Ignition 字节码 → Sparkplug → Maglev → TurboFan

其中只有"解析 → 字节码"这一段是确定性的、与运行时状态无关的,可以安全地提前做。后面的优化层(DFG/FTL、Maglev/TurboFan)生成的机器码依赖:

  • 运行时收集的类型反馈(inline cache、hidden class/shape 的实际形态)
  • 堆上对象的真实地址、pointer compression cage 的基址
  • 目标 CPU 的特性检测和 deopt 元数据

这些东西在构建机器上根本不存在,所以优化后的机器码无法离线生成再分发。Deno 官方在 issue #8820 里也明确回复过:deno compile 不会编译成原生指令,"that would be a monumental feat",且即使引入 V8 code cache 也仍然需要附带源码 。 github

Bun:构建期字节码预编译

Bun v1.1.30 起提供 --bytecode,在 bundle 阶段把 JS/TS 预编译成 JavaScriptCore 字节码,官方数据是启动快约 2 倍(例如编译后的 tsc): bun

# 单文件可执行二进制,字节码内嵌
bun build --compile --minify --bytecode ./app.ts --outfile myapp

# 不打成二进制:生成 index.js + index.js.jsc,运行时自动识别
bun build ./index.ts --target=bun --bytecode --outdir=./dist

几个关键细节 : bun

  • CommonJS 模式下 .jsc 可以脱离 --compile 使用;ESM 模式必须配合 --compile,因为 Bun 会把模块元数据(import/export 信息)一起嵌入二进制,让引擎在运行时完全跳过解析
  • 字节码与 Bun/JSC 版本绑定,跨版本分发 .jsc 会失效
  • 它只省掉了"解析 + 字节码生成",Baseline/DFG/FTL 的 JIT 仍然在运行时发生——所以这是"预热启动",不是 AOT

Deno:只有运行时 code cache

Deno 这边没有等价的构建期预编译机制:

  • deno compile 只是把源码(经 eszip 打包)嵌进 denort 精简运行时,二进制里是源码而非字节码 github
  • Deno 2.1 起编译产物支持 V8 code cache,但缓存是首次运行时才生成、写到临时目录,第二次启动才受益(官方基准约 2x 提升)。可以用 --no-code-cache 关闭 。所以严格来说这不是"分发前 JIT",而是"首次运行后自热" docs.deno
  • Deno 自身的运行时初始化确实用了 V8 startup snapshot(构建 Deno 时执行一次初始化 JS 并序列化堆状态,启动时直接恢复),但这个能力只覆盖 runtime builtins,不开放给用户代码 letsbuildsolutions
  • 实验性的 deno compile --engine quickjs(2.9.4 之后的 canary)走的是另一个方向:QuickJS 是纯解释器、完全没有 JIT,换来更小的二进制和更低的内存,计算密集型代码更慢 docs.deno

两者对比与替代方案

维度 Bun --bytecode Deno code cache
预编译时机 构建期,可随二进制分发 首次运行时才生成
产物 JSC 字节码(嵌入二进制或 .jsc 文件) V8 字节码缓存(临时目录)
首次启动加速 有(约 2x) 无,第二次起约 2x
优化层 JIT 仍在运行时 仍在运行时
可移植性 绑定 Bun/JSC 版本 跟随二进制,无需管理

如果你的真实目标是彻底消灭运行时 JIT(比如冷启动极致优化或代码保护),JS 生态里能走的路大致是:Static Hermes(Facebook 实验性的 JS→原生 AOT,需要类型标注)、Porffor(实验性 JS→原生/Wasm AOT)、或者把热点路径用 Rust/Go 写成原生 addon / Wasm。但这些都不在 Bun/Deno 的能力范围内——对这两个运行时来说,"构建期字节码 + 运行时 JIT"就是目前能拿到的最优组合。

Activity

  1. xz-dev commented on Aug 19, 2026

    @xz-dev
    OwnerAuthor

    Done. Release builds now precompile to Bun bytecode.

    • patch/bun-bytecode-entrypoint: the Bun entrypoint wraps its dynamic imports in an async bootstrap (Bun 1.3.14 rejects --bytecode with top-level await), and build:binary uses --bytecode --format=esm. ESM is required: Bun 1.3.14 fails bytecode generation for this module graph in its default CJS format.
    • ci release pipeline: BUN_BUILD_FLAGS is now --minify --bytecode --format=esm and scripts/build-binaries.sh reads per-target flags from the authoritative descriptor.
    • Exception: Windows targets keep plain --minify builds because Bun 1.3.14 cross-compiled --bytecode executables segfault on the target OS (Executables cross-compiled with --bytecode segfault on launch if running on target OS oven-sh/bun#18416); Windows binaries are cross-compiled from Ubuntu runners.

    Verified end-to-end by Publish GitHub Release run 32251930364: all 12 targets built and passed acceptance smoke (cold-version/version/help), published as xz-v0.84.2-xz.111.1.g1bcdaaab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions