wasm-runner: add pipe/2 for chaining WASM function calls#4
Open
wasm-runner: add pipe/2 for chaining WASM function calls#4
Conversation
Adds the missing WasmRunner.run/2 and run!/2 functions for ergonomic
one-shot WASM execution with a keyword-list calling convention:
{:ok, 8} = WasmRunner.run("math.wasm", add: [5, 3])
{:ok, [8, 55]} = WasmRunner.run("math.wasm", add: [5, 3], fibonacci: [10])
8 = WasmRunner.run!("math.wasm", add: [5, 3])
Features:
- Single call returns unwrapped value: {:ok, 8} not {:ok, [8]}
- Multiple calls return list: {:ok, [8, 55]}
- Options (wasi, cache) mixed into keyword list
- Works with precompiled modules for 53x speedup
- Auto WASI detection for Go modules
- Instance always cleaned up (even on error)
Performance characteristics (from benchmark):
- Cold run/2: ~2ms (dominated by WASM compilation)
- Precompiled run/2: ~40μs (53x faster)
- Pre-loaded call_single: ~15μs (call overhead only)
- WASM fibonacci(30) is 5x faster than pure Elixir
Includes:
- 22 tests covering single/multi calls, errors, cleanup, precompiled
- Benchmark: bench/wasm_runner_run.exs
- Performance docs in docs/PERFORMANCE_GUIDE.md
Adds Pool.call_many/2, call_many!/2, and call_many_unwrapped/2 for executing multiple WASM function calls in a single checkout/checkin cycle. This eliminates N-1 GenServer round-trips when batching calls. Performance (fixtures/math.wasm, pool size 4): - 5 calls: 1.27x faster than repeated Pool.call - 20 calls: 1.18x faster - 100 calls: 1.12x faster Includes: - 16 tests covering correctness, ordering, errors, concurrency - Benchmark comparing call_many vs repeated call vs native Elixir - Performance documentation in docs/POOL_CALL_MANY.md
Add WasmRunner.pipe/3 and pipe!/3 that chain WASM function calls,
feeding the output of each call as input to the next. The module is
loaded once, all calls execute on a single instance, and cleanup is
automatic.
Features:
- Output of each call becomes first arg of the next by default
- :pipe placeholder for explicit argument positioning
- Works with precompiled modules, WASI auto-detection
- Includes 18 tests covering chaining, placeholders, errors, cleanup
- Benchmark script comparing pipe vs separate run calls
Example:
{:ok, 42} = WasmRunner.pipe("math.wasm", [
{:add, [5, 3]}, # => 8
{:fibonacci, []}, # => fibonacci(8) = 21
{:multiply, [:pipe, 2]} # => multiply(21, 2) = 42
])
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
Adds
WasmRunner.pipe/3andpipe!/3that chain WASM function calls, feeding the output of each call as input to the next. The module is loaded once, all calls execute on a single instance, and cleanup is automatic.Features:
:pipeplaceholder for explicit argument positioningExample:
Note:
test/firebird/test_case_test.exshas a pre-existing compilation error on master (CaseClauseError in TestCase macro expansion) — not related to this PR.