Papers
Topics
Authors
Recent
Gemini 2.5 Flash
Gemini 2.5 Flash
119 tokens/sec
GPT-4o
56 tokens/sec
Gemini 2.5 Pro Pro
43 tokens/sec
o3 Pro
6 tokens/sec
GPT-4.1 Pro
47 tokens/sec
DeepSeek R1 via Azure Pro
28 tokens/sec
2000 character limit reached

Wasm/k: Delimited Continuations for WebAssembly (2010.01723v1)

Published 5 Oct 2020 in cs.PL

Abstract: WebAssembly is designed to be an alternative to JavaScript that is a safe, portable, and efficient compilation target for a variety of languages. The performance of high-level languages depends not only on the underlying performance of WebAssembly, but also on the quality of the generated WebAssembly code. In this paper, we identify several features of high-level languages that current approaches can only compile to WebAssembly by generating complex and inefficient code. We argue that these problems could be addressed if WebAssembly natively supported first-class continuations. We then present Wasm/k, which extends WebAssembly with delimited continuations. Wasm/k introduces no new value types, and thus does not require significant changes to the WebAssembly type system (validation). Wasm/k is safe, even in the presence of foreign function calls (e.g., to and from JavaScript). Finally, Wasm/k is amenable to efficient implementation: we implement Wasm/k as a local change to Wasmtime, an existing WebAssembly JIT. We evaluate Wasm/k by implementing C/k, which adds delimited continuations to C/C++. C/k uses Emscripten and its implementation serves as a case study on how to use Wasm/k in a compiler that targets WebAssembly. We present several case studies using C/k, and show that on implementing green threads, it can outperform the state-of-the-art approach Asyncify with an 18% improvement in performance and a 30% improvement in code size.

User Edit Pencil Streamline Icon: https://streamlinehq.com
Authors (3)
  1. Donald Pinckney (6 papers)
  2. Arjun Guha (44 papers)
  3. Yuriy Brun (21 papers)
Citations (7)

Summary

Delimited Continuations for WebAssembly: A Formal Approach and Evaluation

The paper presents \name, an extension to WebAssembly that incorporates first-class, delimited continuations. The authors address inefficiencies in compiling high-level language constructs to WebAssembly, proposing that native support for first-class continuations can mitigate these issues. \name is designed to extend WebAssembly with delimited continuations, which do not introduce new value types and are safe within the WebAssembly environment, especially in the presence of foreign function interfaces (FFIs) like JavaScript.

Core Contributions

The authors make several key contributions:

  1. Design of \name: They define \name to extend WebAssembly with delimited continuations, adding five new instructions: control, restore, continuation_copy, continuation_delete, and prompt. These instructions integrate with existing WebAssembly features without altering the underlying type system.
  2. Implementation in Wasmtime: A practical implementation is provided through a modest extension to Wasmtime, showcasing the feasibility of integrating \name into an existing WebAssembly runtime environment. This includes managing native stack and register states efficiently during the operations of capture and restoration of continuations.
  3. Development of \cname: They develop \cname, a C/C++ extension utilizing \name to offer delimited continuations for C/C++. This allows the use of existing compilation strategies while optimizing control-flow constructs.
  4. Performance Evaluation: They perform case studies demonstrating that \cname, particularly in implementing green threads, outperforms the existing state-of-the-art approach Asyncify by 18% in performance and 30% in code size reduction.

Theoretical and Practical Implications

The theoretical framework of \name demonstrates that WebAssembly can be expanded to include powerful control mechanisms that facilitate the compilation of advanced high-level language features. The introduction of delimited continuations effectively bridges the gap between WebAssembly's current limitations and the needs of modern languages that rely on advanced control flow manipulation, such as green threads and coroutines.

From a practical standpoint, the integration of \name into Wasmtime and its use in a real-world compiler like Emscripten illustrates a pathway for more performant WebAssembly code generation. This has potential implications for a wide array of applications, particularly those running in web browsers where performance and code size are critical.

Future Developments

The success of \name could steer future developments in WebAssembly towards a more comprehensive support for diverse control structures, potentially integrating concepts from other low-level languages or further expanding the capacity for safe and efficient FFI usage. Additionally, this foundational work on continuations could inspire further research in optimizing runtime environments beyond Wasmtime.

In essence, this research represents a critical step in making WebAssembly a more robust compilation target, offering solid theoretical foundations and practical methodologies for enhancing performance and interoperability with high-level language constructs.

X Twitter Logo Streamline Icon: https://streamlinehq.com
Youtube Logo Streamline Icon: https://streamlinehq.com