Skip to content

feat: streaming refill buffers for WASM state transfer#63

Merged
iduartgomez merged 1 commit intomainfrom
feat/streaming-refill-buffers
Mar 24, 2026
Merged

feat: streaming refill buffers for WASM state transfer#63
iduartgomez merged 1 commit intomainfrom
feat/streaming-refill-buffers

Conversation

@iduartgomez
Copy link
Copy Markdown
Contributor

Summary

  • Add StreamingBuffer with std::io::Read impl for streaming large state data from host to contract
  • Host writes [total_len: u32][data...] header into small 64KB buffers
  • When buffer is exhausted, contract calls __frnt__fill_buffer host import to refill
  • Reduces WASM memory usage from O(state_size) to O(64KB) for large contracts
  • Small states (<64KB) fit in one buffer — zero host callbacks

Changes

  • memory/buf.rs: StreamingBuffer, BufferBuilder accessors (bytes_read, last_read_ptr, last_write_ptr), compute_ptr made public, host import declaration, 7 unit tests
  • memory.rs: wasm_interface updated to use StreamingBuffer for all contract operations
  • Version bump to 0.3.4

Test plan

  • 7 unit tests for StreamingBuffer::Read (basic, exact, chunks, empty, EOF, roundtrip)
  • cargo test --features contract,net,testing,trace passes (matches CI config)
  • freenet-core E2E: 2003 lib tests pass with local path dep
  • 100KB large state round-trip test exercises refill path end-to-end

Instead of allocating the full state size in WASM linear memory upfront,
the host now writes data into small 64KB buffers with a [total_len: u32]
header. The contract reads via StreamingBuffer, which calls a host import
(__frnt__fill_buffer) to refill the buffer when exhausted.

This reduces WASM memory usage from O(state_size) to O(64KB) for large
contracts. For small states (<64KB), all data fits in one buffer with
zero host callbacks.

Changes:
- Add StreamingBuffer with std::io::Read impl (contract-side)
- Add __frnt__fill_buffer host import declaration
- Add bytes_read(), reset_pointers(), last_read_ptr(), last_write_ptr()
  to BufferBuilder
- Make compute_ptr public for host-side use
- Update wasm_interface to use streaming reads for all contract operations
- Add 7 unit tests for StreamingBuffer (non-refill path)
- Bump version to 0.3.4
@iduartgomez iduartgomez merged commit 0af23c0 into main Mar 24, 2026
8 checks passed
@iduartgomez iduartgomez deleted the feat/streaming-refill-buffers branch March 24, 2026 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant