wasm-runner: add lazy stream/4 for resource-managed WASM streaming#18
Open
wasm-runner: add lazy stream/4 for resource-managed WASM streaming#18
Conversation
added 4 commits
February 28, 2026 22:02
…lpers Add ergonomic collection operations to WasmRunner that load a WASM module once and apply a function across multiple inputs: - run/4, run!/4: One-shot load+call+stop with WASI auto-detect and cache support, returning a single unwrapped value (vs Firebird.run/4 which returns a list) - map/4: Apply a WASM function over a list of argument lists using a single instance. Supports parallel execution via :concurrency option. - flat_map/5: Map + filter in a single pass over inputs - reduce/6: Fold WASM function results through an Elixir accumulator All functions accept file paths, raw bytes, or precompiled modules. By Sleepy
- Run mix format across all files (Elixir 1.16 formatting rules) - Fix unused generate_expr/3 in wat_gen.ex - Fix unused module_name variable in firebird.compile.ex - Register @wasm attribute with accumulate: true in wasm_modules - Suppress noisy FastNif load warning (expected in dev)
- Fix @wasm warning in physics.ex (missed in previous commit) - Compile deps separately to avoid --warnings-as-errors on rustler - Replace charlist literal with integer list for cross-version compat
Add WasmRunner.stream/4 which returns an Elixir Stream that: - Loads the WASM instance lazily on first element consumed - Calls the function one-at-a-time as elements are pulled - Automatically stops the instance when the stream ends or halts - Composes with all standard Stream/Enum functions Unlike map/4 which eagerly evaluates everything into memory, stream/4 enables lazy processing of large/unbounded input sets with proper resource cleanup via Stream.resource/3. Includes 16 tests covering: basic usage, early termination, Stream.filter/map/chunk_every/take_while/with_index/zip composition, Enum.take/find/reduce/count, cleanup guarantees, and equivalence with map/4 for full consumption.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
By Sleepy
What
Adds
WasmRunner.stream/4— a lazy stream that applies a WASM function over argument lists with automatic instance lifecycle management.Why
map/4eagerly evaluates all calls and collects results into a list. For large or unbounded input sets, this wastes memory.Firebird.Streamrequires manual instance management.stream/4fills this gap.How
Uses
Stream.resource/3to:Composes naturally with all Elixir
StreamandEnumfunctions.Tests
16 new tests covering:
Enum.take,Enum.find,Stream.take_while)filter,map,chunk_every,with_index,zip)Enum.reduce,Enum.count)map/4for full consumption