首页›V8›文章

Speculative Optimizations for WebAssembly using Deopts and Inlining

Daniel Lehmann and Matthias Liedtke · 2025年6月24日

AI 摘要 AI 生成,可能有误

本文介绍了最近在 V8 中实现并随 Chrome M137 一起发布的两项优化:投机性 call_indirect 内联以及对 WebAssembly 的反优化(deopt)支持。通过记录运行时的调用目标反馈以最多内联四个可能的目标,并在假设被违反时回退到未优化的代码,编译器可以生成质量高得多的机器代码,尤其有利于 WasmGC 程序。这些优化在 Dart 微基准测试中带来了平均超过 50% 的加速,在更大、更贴近实际的应用程序中带来了 1–8% 的性能提升;此外,反优化(deopt)也为未来的优化奠定了基础。

In this blog post, we explain two optimizations for WebAssembly that we recently implemented in V8 and that shipped with Google Chrome M137, namely speculative call_indirect inlining and deoptimization support for WebAssembly. In combination, they allow us to generate better machine code by making assumptions based on runtime feedback. This speeds up WebAssembly execution, in particular for WasmGC programs. On a set of Dart microbenchmarks for example, the speedup by the combination of both optimizations is more than 50% on average, and on larger, realistic applications and benchmarks shown below the speedup is between 1% and 8%. Deoptimizations are also an important building block for further optimizations in the future.

Background #

Fast execution of JavaScript relies heavily on speculative optimizations. That is, JIT-compilers make assumptions when generating machine code based on feedback that was collected during earlier executions. For example, given the expression a + b, the compiler can generate machine code for an integer addition if past feedback indicates that a and b are integers (and not strings, floating point numbers, or other objects). Without making such assumptions, the compiler would have to emit generic code that handles the full behavior of the + operator in JavaScript, which is complex and thus much slower. If the program later behaves differently and thus violates assumptions made when generating the optimized code, V8 performs a deoptimization (or deopt, for short). That means throwing away the optimized code and continuing execution in unoptimized code (and collecting more feedback to possibly tier-up again later).

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 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.

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.

Turbocharging V8 with mutable heap numbers

AI 摘要The V8 team optimized the JetStream2 async-fs benchmark, which was slowed by a custom Math.random implementation that stored its seed variable in a ScriptContext slot, causing costly HeapNumber allocations and slower floating-point arithmetic on every update. They introduced mutable heap number slots and Int32 type tracking for script context slots, allowing the seed to be updated in place with efficient integer instructions and deoptimizing only if the slot's type changes. These optimizations resulted in a ~2.5x speedup on the async-fs benchmark and a ~1.6% improvement in the overall JetStream2 score.

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.