首页›V8›文章

Turbocharging V8 with mutable heap numbers

Victor Gomes, the bit shifter · 2025年2月25日

AI 摘要 AI 生成,可能有误

V8 团队优化了 JetStream2 的 async-fs 基准测试。该测试此前因 Math.random 的自定义实现而变慢——该实现将种子变量存储在 ScriptContext 插槽中,导致每次更新时都会产生代价高昂的 HeapNumber 分配以及较慢的浮点数运算。他们为脚本上下文插槽引入了可变堆数字插槽和 Int32 类型跟踪,使种子可以通过高效的整数指令就地更新,并且仅在插槽类型发生变化时才进行反优化。这些优化使 async-fs 基准测试的速度提升了约 2.5 倍,并使 JetStream2 的整体得分提高了约 1.6%。

At V8, we're constantly striving to improve JavaScript performance. As part of this effort, we recently revisited the JetStream2 benchmark suite to eliminate performance cliffs. This post details a specific optimization we made that yielded a significant 2.5x improvement in the async-fs benchmark, contributing to a noticeable boost in the overall score. The optimization was inspired by the benchmark, but such patterns do appear in real-world code.

The target async-fs and a peculiar Math.random #

The async-fs benchmark, as its name suggests, is a JavaScript file system implementation, focusing on asynchronous operations. However, a surprising performance bottleneck exists: the implementation of Math.random. It uses a custom, deterministic implementation of Math.random for consistent results across runs. The implementation is:

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 V8

How we made JSON.stringify more than twice as fast

AI 摘要A recent V8 engineering effort made JSON.stringify more than twice as fast through a side-effect-free fast path that is iterative rather than recursive, templatized handling of one-byte and two-byte strings, SIMD and SWAR-accelerated character scanning, an 'express lane' that skips property checks for fast-json-iterable hidden classes, the Dragonbox algorithm replacing Grisu3 for number-to-string conversion, and a segmented output buffer that avoids costly reallocation and copying. The fast path applies only when serialization uses plain data objects with no replacer, space argument, custom toJSON methods, or indexed properties, and falls back to the general-purpose serializer otherwise. These optimizations are available starting with V8 13.8 (Chrome 138), and were validated on the JetStream2 json-stringify-inspector benchmark.

Speculative Optimizations for WebAssembly using Deopts and Inlining

AI 摘要The article describes two optimizations recently implemented in V8 and shipped with Chrome M137: speculative call_indirect inlining and deoptimization (deopt) support for WebAssembly. By recording runtime call-target feedback to inline up to four likely targets and falling back to unoptimized code when assumptions are violated, the compiler can generate much better machine code, especially benefiting WasmGC programs. These optimizations yield more than 50% average speedup on Dart microbenchmarks and 1–8% improvements on larger, realistic applications, and deopts also lay the groundwork for future optimizations.

Giving V8 a Heads-Up: Faster JavaScript Startup with Explicit Compile Hints

AI 摘要The article describes V8's new Explicit Compile Hints feature, which lets web developers mark JavaScript files or functions for eager compilation instead of lazy on-demand compilation, reducing page load bottlenecks. Experiments on popular web pages showed that 17 out of 20 improved, with an average reduction of 630 ms in foreground parse and compile times. Chrome 136 ships file-level eager compilation via the magic comment //# allFunctionsCalledOnLoad, with per-function hints planned for the future.

Land ahoy: leaving the Sea of Nodes

AI 摘要V8's optimizing compiler Turbofan, long known for using the Sea of Nodes intermediate representation, has over the past three years been migrated to Turboshaft, a more traditional control-flow-graph-based IR, which now covers the entire JavaScript backend and the whole WebAssembly pipeline. The article recounts how Turbofan was created in 2013 to replace Crankshaft, explains how Sea of Nodes differs from a CFG by letting operations float freely via value, control, and effect edges, and details why the V8 team concluded its downsides outweigh its benefits. Key problems include that Sea of Nodes graphs are hard to visually inspect and reason about, and that too many nodes end up tied to the effect chain or requiring control inputs, undermining the flexibility the IR was supposed to provide.

Introducing the WebAssembly JavaScript Promise Integration API

AI 摘要The JavaScript Promise Integration (JSPI) API enables WebAssembly applications written with synchronous assumptions to work smoothly with asynchronous Web APIs by intercepting Promises, suspending the WebAssembly code, and resuming it once the asynchronous operation completes. The article explains how JSPI works through wrapping imports and exports during module instantiation, notes its low constant-time overhead, and states that it is now standardized (phase 4) and available in Chrome 137 and Firefox 139. Practical examples using Emscripten demonstrate computing Fibonacci via Promises and lazily loading WebAssembly code on a function's first call.