BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News NetWasm 0.10 Speeds Up C# on WebAssembly, Now Faster Than Mono AOT

NetWasm 0.10 Speeds Up C# on WebAssembly, Now Faster Than Mono AOT

Listen to this article -  0:00

The team behind NetWasm has published a write-up of its 0.10 optimisation work, spanning releases 0.10.1 to 0.10.8. The changes touch exception handling, array bounds checks, WebAssembly SIMD and integer division. On the project’s own benchmark set, NetWasm now comes out ahead of Mono AOT and gets close to desktop .NET on some workloads.

NetWasm is a compiler and runtime that compiles C# programs ahead of time into small, standalone WebAssembly files. It takes the IL that Roslyn produces and emits Wasm, using its own runtime and a CoreLib ported from .NET. The yardstick is the Mono AOT build for WASI, which runs .NET on the same engine. A new garbage collector had already put NetWasm in front on allocation-heavy code, but plain loops were still slower: a quicksort of 100,000 integers took 77.2 ms, against 58.0 ms for Mono AOT.

Much of the early gain came from exception handling in 0.10.1. Wasmtime adds extra register save-and-restore code around any function that can throw or catch, floating-point registers included. Methods with null, bounds or checked-arithmetic checks paid that price on every call. Throws now go through a shared function and the catching handler releases the frames, which cut UI churn from 17.6 to 7.9 ms.

Version 0.10.2 inlines small straight-line methods in loops, tuple swaps included, and skips checks already performed on the same array and index. The quicksort fell to 54.0 ms. DoomSharp, a C# software renderer for Doom that ships a NetWasm build, served as a second test case. Value-type accessors now compile to plain field loads, and at O3 small branching helpers are inlined inside loops. Together these brought a 120-frame render from 3,595 down to 1,934 ms.

SIMD arrived in 0.10.7. Vector128, Vector<T> and PackedSimd lower to 128-bit Wasm instructions, and element-wise array loops handle four or more elements per iteration, with a scalar fallback. Hoisting loop-invariant Math calls allowed a solver for Burgers’ equation, a fundamental partial differential equation from fluid mechanics, to vectorise: its first test dropped from 497 ms to 71 ms, against 186 ms for Mono AOT and 93 ms for desktop .NET.

Version 0.10.8 targets division and range-checked arithmetic. A remainder that follows a division now reuses the quotient and skips repeated zero-divisor and int.MinValue / -1 checks. A trial-division test fell from 1,376 to 765 ms, while Mono AOT needed 862 ms.

Across the 2,213 cases, 0.10.8 at O3 with NetWasmGC takes 0.41 times Mono AOT’s time as a geometric mean, excluding five cases where Mono’s LLVM removes the work. Against desktop .NET 11 RC3 JIT the ratio is 0.87, down from 2.15 for 0.10.6. By family, the ratio to Mono AOT runs from 0.34 for intrinsics to 0.81 for math. The 120-frame DoomSharp render takes 1,623 ms on NetWasmGC, compared with 2,371 ms for Mono AOT and 1,053 ms for desktop .NET JIT.

Those figures come from Wasmtime 49.0.3 on an Apple M1 Max, using cases from dotnet/performance plus five NetWasm workloads. The authors also found that identical loops at different addresses could differ by up to 15 per cent, so their A/B tests align every function to 4 KiB.

The article is open about what is left. Desktop .NET is about 3.1 times faster on vector benchmarks, because it can use host SIMD directly while Wasm is limited to fixed 128-bit operations. Math.Pow is 1.4 times slower than on Mono AOT, and CopyBlock64 is 3.3 times slower. One workload, NewtR, regressed to 1.9 times slower than in 0.10.6, which the authors blame on Wasmtime’s register allocator keeping a value in a stack slot around the loop.

Since the numbers are self-reported and come from one machine and one runtime, results elsewhere may differ. NetWasm can be tried in the online playground, and local setup is covered in the compile and publish guide. Developers can find the documentation is in the official GitHub repository.

About the Author

Rate this Article

Adoption
Style

BT