https://github.com/zion-sati/NetWasm
Been a month since I last updated here and a lot of improvements have gone into NetWasm. Let's start with a little intro for those who are new.
NetWasm compiles C# straight to one standalone WebAssembly file: no Mono, no dotnet.js, no CLR inside.
Version 0.16 compiles .NET 11's runtime async, which Microsoft's own WebAssembly runtime can't run yet, and on async workloads it beats desktop .NET.
Runtime async, in WASM, before Microsoft
For as long as C# has had async, Roslyn has turned every async method into a state machine class: fields for every local, a MoveNext switch, a builder. .NET 11 changes that. With runtime async, the runtime itself suspends a method and resumes it in place, and the compiler emits the method almost as written.
On .NET 11, Blazor WebAssembly runs on Mono, and Mono can't run runtime-async methods (https://github.com/dotnet/runtime/issues/137984). NetWasm 0.16 compiles every async method in your project with runtime async, .NET's own libraries included, on its own suspension runtime, in the browser and under WASI.
NetWasm async faster than desktop .NET
I measured 23 async scenarios in three groups. The first is async patterns: awaits, Task.Yield, WhenAll, IValueTaskSource, async iterators, and exceptions thrown ten frames deep. The second is CoreLib's own stream loops and SemaphoreSlim. The third is libraries: System.Text.Json over a stream that suspends on every read, pipes, channels and DeflateStream.
| Share of the other runtime's time NetWasm takes (geometric mean, lower is faster) | Mono AOT | Desktop .NET 11 JIT | Desktop .NET 11 NativeAOT |
|---|
| Async patterns (11) | 0.09 | 0.49 | 0.57 |
| CoreLib streams, SemaphoreSlim (6) | 0.31 | 0.84 | 0.98 |
| JSON, pipes, channels, Deflate (6) | 0.39 | 0.80 | 1.14 |
| All 23 | 0.18 | 0.64 | 0.79 |
This is all a fair fight. Desktop runs runtime async too: its libraries ship compiled that way, and the benchmark turns it on for the workloads' own code. The desktop JIT runs with tiering off, so both sides run optimized code from the first call. The numbers are medians of 15 runs on an Apple M1 Max, with NetWasm under Wasmtime.
NetWasm doesn't win everywhere though. It beats the JIT in 15 of the 23 scenarios and NativeAOT in 13. On JSON, pipes, channels and Deflate, NativeAOT's still ahead.
For more details, see https://www.netwasm.com/micro-benchmarks/ (filter family by Async workloads)
Avalonia 12, unmodified
While runtime async is the headline, the bigger change for anyone shipping an app is that NetWasm now compiles packages you find on nuget.org.
Some of you here have requested for this specifically -- Avalonia 12, with Skia, compiled XAML and the Fluent theme, compiles straight to WebAssembly with no changes to Avalonia. Its headless platform renders the same frame as desktop .NET, pixel for pixel, on wasm32 and wasm64, in Debug and Release. SkiaSharp and HarfBuzzSharp ship native static archives for .NET's WebAssembly SDK, and those link into the program exactly as they ship. Multi-assembly Avalonia applications compile too.
A NetWasm project targets net10.0-netwasm, a .NET platform target framework like Blazor's net10.0-browser. NuGet restores each package's normal `net10.0`, `net8.0` or .NET Standard build, so a package doesn't need a NetWasm build of its own.
I took the 100 most-downloaded nuget.org packages that ship a .NET Standard build and ran each one's README quick start. 56 run unchanged. Most of the rest stop on run-time reflection, which NetWasm leaves out on purpose. Several of those have a source-generated replacement that works.
First to WASI 0.3 ahead of Rust
NetWasm targets WASI 0.3, so the same compiler builds backend services. A project can read files, open sockets, and serve HTTP that wasmtime serve, Spin and wasmCloud run as-is. SslStream runs over wasi:tls, so HttpClient does pooled HTTP/2 over TLS, and wss:// WebSockets work. The HTTP service template, with .NET's whole HTTP stack, is a single 1 MB component, 251 KB with Brotli.
Debug it by C# line
A Debug build now carries DWARF. When a Wasmtime run traps, the backtrace names your C# frames as App.Program.Fail at Program.cs:17. LLDB under Wasmtime sets breakpoints on C# lines and steps through them.
Still small, still fast
The template app that prints 42 publishes to 26,444 bytes, precise GC included. Rust's size profile builds the same WASI 0.3 component at 63,528. Across 2,389 micro-benchmark cases, from dotnet/performance and NetWasm's own, NetWasm runs more than three times as fast as Mono AOT on the same Wasmtime. David Fowler's DoomSharp renders about 1.4 times as fast as its Blazor AOT build, from a ninth of the download.
Thank you to this amazing subreddit
Thank you all for shaping NetWasm as a product and providing me with a lot of useful pointers such as using SatoriGC, testing it out and filing bug reports. Please continue to do so! I'm very grateful and highly value all your feedback.