Skip to content

No-alloc stream binding: replace Vec<T> in StreamWriter with static buffer / AbiBuffer #1

@avrabe

Description

@avrabe

Context

wit-bindgen 0.55.0's StreamWriter::write() takes Vec<T>, requiring heap allocation. But the canonical ABI level uses caller-supplied linear memory buffers: stream.write(handle, memory, ptr, count). No allocation needed at the ABI level.

The verified engine is no_std/no_alloc. The WASM binding layer currently uses Vec<T> — this is a wit-bindgen convenience, not fundamental.

Three paths to no_alloc streams

1. write_buf(AbiBuffer) (available now)

wit-bindgen 0.55.0 has StreamWriter::write_buf() taking AbiBuffer<T> instead of Vec<T>. Investigate whether AbiBuffer can be backed by a static buffer.

2. Custom cabi_realloc (proven in POC)

Luke Wagner's approach (component-model#314): store buffer pointer in WASM global, implement cabi_realloc to return pre-allocated buffer. No spec changes. Already proven in POC by Marcin Kolny.

3. Flat-types zero-copy (experimental)

Christof Petig's proposal (component-model#398): zero-copy pub-sub using host-managed shared memory. Working WASI 0.3 prototype exists. Not standardized.

What to implement

Start with approach #2: custom cabi_realloc returning from Kiln's safe_managed_alloc! or a static buffer pool. The binding layer becomes:

static mut STREAM_BUF: [Violation; MAX_VIOLATIONS_PER_CYCLE] = [...];
// cabi_realloc returns &STREAM_BUF when size matches

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions