bytecodealliance / bytecodealliance/wasmtime

c-api: component-model: Byte array

Open
#11,617 1 comment 3 reactions 0 assignees View on GitHub
wasm-proposal:component-model wasmtime:c-api
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 18h
Merged PRs (30d)
126

Description

Currently `list<..>` is being passed through as `WASMTIME_COMPONENT_LIST` with `wasmtime_component_vallist_t` as union type.

In `wasmtime_component_vallist_t` each item is a [`wasmtime_component_val`](https://github.com/bytecodealliance/wasmtime/blob/db7b44d223d1d337894a3e0dd95b28f6c75874c1/crates/c-api/include/wasmtime/component/val.h#L104C48-L104C70). This struct has a size of 32 bytes.

In WASI, `list` is used as an byte array, for example in [`wasi:sockets/udp`](https://github.com/WebAssembly/WASI/blob/8a69f1ed6ce7bfd3cfe72270b787d4d4598b721d/wasip2/sockets/udp.wit#L14).
With the implementation in C-API would this need way more memory than needed.

For example, lets say you pass 512 bytes to the component.
Instead of 512 bytes you need to create a `wasmtime_component_vallist_t` of 512 items, that use 16384 bytes in total.

You also need to transform the bytes to `wasmtime_component_val` items, which is an additional step you need to do before passing the byte array.

#### Suggestion

Create a new type, e.g. `WASMTIME_COMPONENT_LIST_U8` that uses the union type `wasm_byte_vec_t`.

Whenever `list` is used, the new value type should be used: `WASMTIME_COMPONENT_LIST_U8` with `wasm_byte_vec_t`
For other types, the current type should be used: `WASMTIME_COMPONENT_LIST` with `wasmtime_component_vallist_t`

#### Benefit

Less memory is being passed through C-API, and less overhead (by not transforming the bytes to `wasmtime_component_val`'s)

#### Implementation

N/A

#### Alternatives

Accept the fact that every byte is actually 32 bytes when using `list`

Or use `string` instead of `list` in WIT, since this uses `wasm_byte_vec_t` as union type, where the data is a simple `byte`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.