Abstract
WebAssembly's structured control flow, while beneficial for safety and performance, complicates the implementation of non-local control flow features such as coroutines, generators, and lightweight threads. Current workarounds, such as the Asyncify tool, rely on heavy code transformations that result in significant binary bloat and performance degradation. This paper introduces WasmFX, an extension to WebAssembly that provides native support for effect handlers. By utilizing typed, single-shot delimited continuations, WasmFX enables efficient and modular execution of complex control flow patterns. Our evaluation demonstrates that WasmFX can reduce binary sizes by up to 75% compared to current transformation-based approaches, while providing a clear roadmap for high-performance native execution.
Introduction
WebAssembly (Wasm) was originally designed with a rigid, structured control flow to ensure safety and predictable performance across diverse environments. While this model is well-suited for standard C-style imperative code, it lacks direct support for "non-local" control flow—scenarios where a program must pause its current execution context, jump to a different context, and potentially return later. Modern programming languages increasingly rely on such features for asynchronous programming (async/await), green threads, and generators.
The Problem: "Ceremonious" Transformations
Currently, compilers targeting WebAssembly must perform complex and often inefficient transformations to simulate non-local control flow. The most common approach, exemplified by the Asyncify tool, involves transforming the entire program into a giant state machine. This "ceremonious" approach introduces three primary issues:
- Binary Bloat: Code size often increases by 2x to 10x, which is particularly detrimental for web-based delivery.
- Performance Penalties: Significant overhead is added to every function call to manage state transitions.
- Broken Modularity: These transformations often require whole-program analysis, preventing local or incremental compilation.
The WasmFX Proposal
WasmFX addresses these limitations by adding a minimal set of instructions to WebAssembly based on the theory of delimited continuations and effect handlers.
Core Instructions
cont.new: Creates a new continuation (essentially a new stack) from a function.suspend: Pauses the current execution and yields control to a designated "handler."resume: Restarts a previously suspended continuation.cont.bind: Allows for the partial application of arguments to a continuation.
Type Safety and Linearity
Unlike low-level primitives like setjmp/longjmp in C, WasmFX continuations are fully typed. The Wasm type system is extended to ensure that when a continuation is resumed, it receives exactly the types of values it expects, maintaining Wasm's core safety guarantees.
Furthermore, the design focuses on single-shot continuations (resumed only once). This linearity allows for efficient implementation using stacks that do not require copying and avoids the complexities of garbage collection cycles, making WasmFX compatible with environments that lack a native garbage collector.
Implementation and Evaluation
The authors developed a formal specification and a reference interpreter for WasmFX, alongside a prototype in Wasmtime, a production-grade Wasm engine. The prototype was evaluated using two primary methods:
- Micro-benchmarking: A C-based web server simulation managing 10,000 concurrent coroutines performing 10 million total requests. This compared WasmFX against Asyncify and hand-written state machines.
- Compiler Integration: The TinyGo compiler was modified to use WasmFX as a backend for Go's goroutines, comparing the resulting binary sizes against standard TinyGo output.
Key Results
- Binary Size Reduction: For TinyGo benchmarks, WasmFX reduced the binary size from 597 KB to 156 KB—a nearly 75% reduction compared to the state-of-the-art transformation approach. In smaller C benchmarks, the WasmFX binary was only 0.8 KB, compared to 9.2 KB for the Asyncify version.
- Prototype Performance: The current Wasmtime prototype is roughly 3.8x slower than Asyncify. However, this overhead is a known consequence of the prototype's reliance on "libcalls" (calling out from Wasm to host Rust code) to switch stacks. The authors have established a roadmap for a native JIT implementation using direct assembly for stack switching, which is expected to significantly outperform current methods.
Conclusion and Future Work
WasmFX demonstrates that native effect handlers are a natural fit for WebAssembly's stack-based architecture. By treating "the rest of the stack" as a first-class, typed object, WasmFX provides a universal mechanism for non-local control flow without the penalties of current transformation tools. Future work will focus on moving stack-switching logic directly into the Wasmtime JIT compiler, integrating with the ongoing Wasm Exception Handling proposal, and exploring the potential for multi-shot continuations in specialized domains like probabilistic programming.
Summary updated