In my current WIP wasm component to manage a wayland compositor I've been thinking of integrating wgpu under the hood but I realize some things like Buffer::map
will require a copy between the runtime and the host. I was wondering if there was any proposal to allow WIT to describe a host owned slice (or block of memory) that the runtime can write into?
I'd think something like a slice<u8>
. In my use case with wgpu I'd probably make the buffer resource have a function like func slice() -> option<slice<u8>>
to get the slice.
However I do realize that there might be a use case for the runtime owning a slice vs always having it borrowed out like in Rust.
Actually it might be a footgun to have a function the runtime calls to get a slice from inside the host? In rust this would probably double borrow the state?
Wasm does appear to have untyped memory, but I imagine that exposing that raw memory is a huge footgun
As you've seen WIT doesn't currently have a slice
type and there currently isn't a design/proposal to add one. If you're interested in this though opening an issue on the component-model repository with more information might be a good next step. It's unlikely this will be added in the near future though
Okay I will make an issue when I can.
I guess for now is there an unsafe way of sorts to have the host give the runtime raw memory via the component model?
Wasmtime has a WasmVec type for this but that's the closest you'll get and it's not easy to use
Another option is to model the buffer as a resource
, which lets you pass around handles. That avoids the copying, but the downside is that you can't directly load/store to the buffer from the API; all accesses must go through function calls.
Dan Gohman said:
Another option is to model the buffer as a
resource
, which lets you pass around handles. That avoids the copying, but the downside is that you can't directly load/store to the buffer from the API; all accesses must go through function calls.
I'm not sure that this would solve the issue of copies
resource buffer {
get: func(offset: u64) -> u8
set: func(offset: u64, write: u8)
}
If a buffer looked like this, you'd be limited to single reads and writes which would definitely result in a huge amount of context switches in and out of the wasm runtime
Last updated: Nov 22 2024 at 16:03 UTC