State of wasm with rust

What's the current state of wasm as a Rust target? I've seen the Rust WASM book but I'm not sure how current that is. Goal here is to run graphics code that maxes out gamer desktops.

Specific areas:

  • "std" won't work, and "core" is too limited. Is there something usable in place of "std" that offers at least allocation and such basics, and works on all targets? I found "pwasm-std", but that's apparently obsolete.

  • Does threading work in wasm yet? Not "async", real threads for compute-bound work. Back in 2018, it was supposed to be getting close.

  • On a somewhat related note, anyone know when Firefox is supposed to support WebGPU? That feature was in test in 2020.

std mostly works, but there are some unsupported things that either panic or unconditionally return errors. If you just need allocations, you could use core with alloc instead.

You can still use std. It's just that anything requiring an OS (the file system, subprocesses, etc.) will return an error at runtime.

Alternatively, you can use alloc to get nice things like Vec and String, then anything required for interacting with the outside world gets provided by the runtime in the form of host functions.

My understanding is that WebAssembly is still single-threaded. Or at least, the runtimes I have used (Wasmer and wasmtime) are both !Sync and use unsynchronised access to linear memory.

One major limitation is that WebAssembly is meant to interoperate with JavaScript in the browser, and JavaScript is probably too ingrained in its ways to add multi-threading support. Instead, you are meant to use web workers for parallelism, which is roughly equivalent to multiprocessing.

The wasm-bindgen book contains some multithreading example.

https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html

I believe that example uses the web workers that @Michael-F-Bryan mentioned

Well the demo uses rayon.

Indeed it does, using a custom worker pool in place of a thread pool

The wasm32-wasi target has some basic support for the file system and other environmental features of the WASI runtime in std: rust/library/std/src/sys/wasi at master · rust-lang/rust · GitHub

The WASI tutorial shows an example using the FS.

Spawning threads is not supported in std for this target. There is a lot of ongoing discussion in the Webassembly Threads proposal issues section, and specifically in Multi-threading and Atomics · Issue #296 · WebAssembly/WASI · GitHub. But I haven't been through all of it. At least I found that the Wasmer runtime has support for shared linear memory and atomic types with the --enable-threads arg.

I'll ping @sunfishcode here, maybe they can provide some extra context on the current state of things.

3 Likes

Thanks.

Unfortunately, I just rewrote my program to use fine-grained locks, which is great for native performance but not for WASM.

(The application is a client for a virtual world. Huge 3D state. One thread is frantically redrawing, and we don't want to ever slow that one down or we take a frame rate hit. Other threads are handling messages from the network, fetching content, and updating the state. This uses Vulkan, so you can mess with the GPU memory while rendering is underway. Hence the need for real concurrency.)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.