NetWasm Yields 37KB WASI Components, Beating Rust on Binary Footprint
NetWasm compiles C# CIL directly to WebAssembly components, achieving a 37,789-byte raw payload with an embedded precise garbage collector—outperforming Rust 1.95.0 size-optimized builds by 29.5%. By bypassing the desktop CLR and Mono runtimes in favor of a closed-world AOT pipeline, the toolchain simultaneously delivers superior allocation throughput compared to Mono AOT and desktop .NET JIT execution.
Architecture and Compilation Pipeline
NetWasm diverges from canonical .NET WebAssembly deployments by discarding the Mono interpreter/AOT runner, `dotnet.js`, and application assembly DLLs. The toolchain ingests CIL emitted by Roslyn and performs closed-world Ahead-of-Time (AOT) compilation directly to WebAssembly code. Reachable program paths are linked alongside specialized generic implementations and an isolated, purpose-built runtime tailored specifically to the module's requirements.
To lower the binary surface, NetWasm enforces a reduced platform API surface area. Broad runtime reflection—including dynamic assembly loading, runtime type names, and the C# `dynamic` keyword—is excluded from the executable profile. Cross-boundary subsystems like JSON serialization, dependency injection, WIT host interfaces, and C-ABI native bindings rely exclusively on static source code contracts generation. Binaryen's `wasm-opt` optimization pipeline is embedded directly into the compiler, running automatically during build orchestration without requiring explicit post-processing passes.
Binary Footprint Benchmark Analysis
In a standard print-42 workload compiled as a standalone WASI Preview 2 Component, NetWasm using its default Compact GC achieved a binary size of 37,789 bytes raw (12,887 bytes under Brotli-11 compression). This result includes both the compiled application and the runtime execution engine with a precise collector.
By comparison, a matching WASI Preview 2 reference artifact built under Rust 1.95.0 configured with standard size-reduction settings (`opt-level = "z"`, LTO enabled, single codegen unit, `panic = "abort"`, and stripped symbols) generated a binary of 53,635 bytes raw (16,963 bytes Brotli-11). NetWasm's default build produced an output 29.5% smaller raw and 24.0% smaller under Brotli compression than the Rust reference build.
For broader ecosystem context, equivalent workloads targeting Preview 1 contexts resulted in 79,293 bytes raw for Kotlin 2.4.0 (`wasmWasi`) and 93,043 bytes raw for TinyGo 0.39.0. Because Rust produces WASI P2 components natively, applying `wasm-opt` to Rust outputs requires manually extracting core modules, optimizing them, and re-encoding the outer component structure, whereas NetWasm executes integrated optimization passes during standard `dotnet publish` operations.
Memory Allocation and Execution Performance
Execution benchmarks evaluated on Apple M1 Max hardware under Wasmtime 49.0.3 demonstrate that NetWasm's precise Compact collector outperforms Mono AOT (compiled via Microsoft's .NET 11 RC3 `wasi-wasm` target) and desktop .NET 11 JIT across memory-heavy execution paths:
- Full Transaction: NetWasm completed in 29.7 ms, compared to 56.6 ms under Mono AOT, 50.1 ms under desktop .NET 11 JIT, and 32.1 ms under desktop NativeAOT.
- Retained Object Graph: NetWasm registered 4.9 ms, outperforming Mono AOT (14.4 ms), desktop JIT (9.7 ms), and desktop NativeAOT (7.5 ms).
- UI Churn: NetWasm required 7.8 ms, beating Mono AOT (26.9 ms) and desktop JIT (8.6 ms), while slightly trailing desktop NativeAOT (6.6 ms).
Across allocation-bound workloads, NetWasm GC reduced execution times by 48% to 71% compared to Mono AOT. In non-allocating execution paths, NetWasm demonstrated a 31% speedup over Mono AOT on core library routines and a 7% improvement on quicksort, while matching performance on compute-bound n-body and CRC-32 kernel tests.
Ecosystem and Tooling Integration
NetWasm supports modern C# 15 features, reflection-free dependency injection, expression trees, and standalone libraries covering LINQ, HTTP, JSON, XML, Regex, and cryptographic hashing. The toolchain abstracts native integration by allowing static `.a` libraries to be linked with bounded C-ABI marshalling, alongside generating C# bindings directly from WebAssembly Interface Type (WIT) definitions.
The SDK operates via standard `dotnet CLI` workflows and provisions pinned dependencies—including Node, LLD, and Binaryen native binaries—through NuGet package resolution. Target output options allow publishing standalone WASI P2 components, dual-targeted `netwasm0.1`/desktop libraries, or typed JavaScript modules tailored for DOM threads and Web Worker contexts.