wasmfx / wasmfxtime

A fork of wasmtime (a fast and secure runtime for WebAssembly) supporting the WasmFX instruction set
https://wasmfx.dev/
Apache License 2.0
19 stars 1 forks source link

Initiate execution of continuations without use of closures #147

Closed frank-emrich closed 8 months ago

frank-emrich commented 8 months ago

Currently, we inherit the following behavior from the wasmtime_fiber crate: The toplevel function executed inside a Fiber is a Rust closure. However, this flexibility is not required for our use case: The function we want to run inside a continuation is always the array call trampoline of a wasm function.

This PR specializes our module wasmtime_runtime::fibre to our particular use case, getting rid of the involved closures. Instead, all the required data is passed around explicitly in function arguments, instead of being implicitly stored in a closure. This has the following advantages:

  1. We do not need to allocate a Box holding the closure anymore.
  2. The function wasmtime_runtime::fibre::unix::fiber_start, which is the Rust entry point for execution inside a Fiber, is no longer parameterized over a closure type F. Thus, we can call it directly from wasmtime_fibre_start, instead of needing to pass it around.
  3. Since the internal layout of closures is not specified by Rust, their usage stands in the way of replacing wasmtime_runtime::fibre::unix::fiber_start with generated code eventually.

In a sense, what this PR does is some kind of manual closure conversion: Instead of creating a closure in wasmtime_runtime::continuation::cont_new and passing it to Fiber::new, we pass all of the necessary environment to Fiber::new. It is then stored on the Fiber stack by wasmtime_fibre_init, where it is read off and passed to fiber_start when resume-ing a continuation for the first time.