Stream: wasm

Topic: Slices in WIT


view this post on Zulip i509VCB (Oct 09 2023 at 00:07):

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.

view this post on Zulip i509VCB (Oct 09 2023 at 00:09):

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?

view this post on Zulip i509VCB (Oct 09 2023 at 00:20):

Wasm does appear to have untyped memory, but I imagine that exposing that raw memory is a huge footgun

view this post on Zulip Alex Crichton (Oct 09 2023 at 03:06):

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

view this post on Zulip i509VCB (Oct 09 2023 at 03:24):

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?

view this post on Zulip Alex Crichton (Oct 09 2023 at 04:43):

Wasmtime has a WasmVec type for this but that's the closest you'll get and it's not easy to use

view this post on Zulip Dan Gohman (Oct 09 2023 at 15:54):

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.

view this post on Zulip i509VCB (Oct 09 2023 at 16:07):

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: Oct 23 2024 at 20:03 UTC