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:
- Design of \name: They define \name to extend WebAssembly with delimited continuations, adding five new instructions:
control
,restore
,continuation_copy
,continuation_delete
, andprompt
. These instructions integrate with existing WebAssembly features without altering the underlying type system. - 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.
- 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.
- 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.