C and Wasm benchmark
This folder contains a self-contained side-by-side benchmark for the SWAR detector in:
- native C
- AssemblyScript compiled to a WASI-style Wasm module
Files:
bench.cNative C throughput benchmarkwasm-bench.tsAssemblyScript throughput benchmark using@assemblyscript/wasi-shim,performance.now(), andconsole.log()
Install dependencies
bash
make installThat installs the local AssemblyScript compiler and @assemblyscript/wasi-shim.
You still need these tools on your machine:
ccwasmer- optionally
wasm2watfor inspection
Build everything
bash
make buildRun both benchmarks
bash
make runOr run each side separately:
bash
make run-c
make run-wasmThe C and Wasm harnesses intentionally use the same payload sizes, warmup policy, iteration count, and output labels so the comparison stays as close as possible.
The default build targets are intentionally aggressive:
- C uses host-tuned native flags plus LTO
- AssemblyScript uses
-O3,--converge, and the incremental runtime - Wasmer runs with LLVM plus
--enable-pass-params-opt
Inspect native assembly
bash
make inspect-c
sed -n '/detect_escapable_u64_swar_safe/,/ret/p' build/bench.s
sed -n '/detect_escapable_u64_swar_unsafe/,/ret/p' build/bench.sIf your compiler inlines everything away, temporarily remove inline or add __attribute__((noinline)) to the detector functions before regenerating build/bench.s.
Inspect Wasm
bash
make inspect-wasm
less build/wasm-bench.wat
wasm-objdump -d build/wasm-bench.wasm | less