WebAssembly advanced in 2023 – but .NET cannot yet use Wasm garbage collection

WebAssembly advanced in 2023 – but .NET cannot yet use Wasm garbage collection
garbage collection rubbish garbage bin

WebAssembly added key features in 2023 and usage in the browser grew by 70 percent – but Microsoft has no current plan to use Wasm garbage collection for the .NET runtime.

Gerard Gallant, CIO at Dovico and author of WebAssembly in Action, has posted on the Uno site on the state of WebAssembly in 2023 and hopes for 2024. The Uno platform makes use of WebAssembly when running in the browser. “This past year was incredible for WebAssembly,” Gallant claims, thanks to major new features and growing adoption.

Regarding usage, Gallant points to a Google Chrome usage page, which tracks the number of page loads in Chrome that use specific features. When set to WebAssemblyInstantiation, it shows usage up from 1.95 percent on January 1st 2023 to 3.32 percent a year later – an increase of 70 percent, albeit from a small base. Given that WebAssembly is for applications rather than general web pages those numbers are significant.

Growing use of WebAssembly in the browser, as reported by a Google Chrome stats page

As for features, perhaps the biggest is garbage collection, released in Chrome and Firefox by November 2023, though not yet in Apple’s Safari. Built-in garbage collection has the potential to enable smaller Wasm runtimes for languages that use it, such as Java, Kotlin, Dart (used by Flutter), and C#. The size of the compiled code is a big deal since it causes slow page loading.

Microsoft uses WebAssembly for its Blazor framework, and as noted in the documentation, this works by compiling C# to .NET assemblies and then downloading a Wasm version of the .NET runtime to the browser. “The size of the published app … is a critical performance factor for an app’s usability,” the documents state.

That makes using native Wasm garbage collection desirable, but Aleksey Kliger, a Principal Software Engineer on the .NET team, says on GitHub that “we will continue to monitor the evolution of the post-v1 WasmGC spec, but at this time we are not planning to adopt it.” 

The issue is that using the current spec would require either “altering the semantics of existing .NET code,” or limiting the use of various features, not only in user code but also in the C# base library. Wasm garbage collection works better with Google’s Dart.

Gallant does note that the recently released .NET 8 has a new JIT (just-in-time) compiler for WebAssembly informally called a Jiterpreter – because it creates WebAssembly code on the fly. The trade-off here is that Blazor WebAssembly can work either by AOT (ahead of time) compilation to WebAssembly, which performs well but with large file size, or by compiling to .NET IL (intermediate language) executed by an interpreter implemented in WebAssembly, which performs more slowly but is around half the size. The Jiterpreter adds partial JIT support so that Blazor WebAssembly that is not AOT compiled can get some performance benefits at runtime.

Other new features that came to WebAssembly in 2023 include Tail Calls, which optimize recursive code, and multiple memory blocks for modules, now supported in Chrome and behind a flag in Firefox.