Wasm Builders

Matt
Matt

Posted on

WASM Threading and Exception support is coming to C# developers

WebAssembly Performance Updates

The latest release from Uno Platform packs a punch, but focusing on the performance side it includes support for the most recent WebAssembly advances available in .NET 7 – Wasm Threading and Exception handling.

Exciting new updates are coming to WebAssembly, and the .NET team has been very busy getting performance updates ready for .NET 7. Uno Platform is able to activate some WebAssembly-related features ahead of the official .NET 7 support, so that you can experiment with it, or as some do – use it in production as support is there if needed.

As mentioned, the biggest improvements are in the area of WebAssembly Exceptions and Threading support.

1. WebAssembly Exceptions support

Enabling WebAssembly exceptions in your projects allows for your code to stay entirely in WebAssembly for exception handling, improving the performance as a result. In addition, the generated code size is smaller too.

Support for this feature has been added in .NET 7 previews, and it is now included in Uno Platform Wasm projects and so your Uno-Wasm projects now automatically benefit from performance improvement and file size reduction that this feature brings. All you need to do is upgrade to the latest 4.4 bits and update Uno.Wasm.Bootstrap to the latest preview. This new feature is enabled by default in Uno.Wasm.Bootstrap 4.0-dev builds, and makes use of the WebAssembly specifications for Exceptions handling.

Since WebAssembly was introduced, exceptions had to be emulated by Emscripten, causing each try/catch/finally block to force the “decoration” of invocations through JavaScript. This had a significant performance impact, as JavaScript/Wasm boundaries crossing is expensive.

For example, here’s a before-and-after comparison of call stacks, captured using the Chromium profiler:

Image description

Image description

You’ll notice that the “invoke_viiii” and “js-to-wasm:iiii:” frames have been removed, causing this synthetic benchmark to be 1.75x faster.

Because the Exception Handling feature has been available for a while in major browsers, starting from the Uno Bootstrapper 4.0 previews which use this feature, older browsers versions are not supported anymore. If an app needs to be compatible with earlier browsers, it is best to use the Uno Wasm Bootstrapper 3.x builds.

2. WebAssembly Threading support

Another exciting update is experimental Threading support in .NET 7. The Uno Platform team has been working with the .NET team on providing early feedback for the hard work they’ve been putting into enabling Wasm threading for the past few months. This is another great open source win for everyone, as ultimately both .NET and Uno Platform communities will benefit from this feature. They contribute to this journey by being one of the first to take Wasm threading to their community.

Both Uno.UI and the Uno Bootstrapper have been updated to include threading support. On the Uno.UI side, this means that you can create Threads or Tasks and use the CoreDispatcher or DispatcherQueue to come back to the main thread. This is enables Web Apps to perform expensive tasks off the UI thread and avoid freezing the UI.

If you have been following Uno Platform, you may remember that they experimented with threads two years ago with the mono runtime. They’ve since been able to reactivate the code they developed back then to enable threads very quickly as a result.

For more information on WASM Threading, Exception handling and Uno Platform 4.4 check out their latest release.

Discussion (0)