I have successfully integrated Wasm for modding in a game that I am creating. Thank you all for developing wasmtime, wit-bindgen.
Last problem that I am facing is with improving parallelization.
For couple of days I have been researching state of Wasm with threading support (https://github.com/RReverser/wasm-bindgen-rayon).
In my project I need to execute code that accesses/reads the same data. Currently in native code to improve calculation speed I am parallelizing calculations on multiple threads. Would love to move this into Wasm to allow project users to modify calculation logic, but this would require calling the same Wasm logic from multiple host threads to keep comparable performance.
I am trying to set up Wasm instances with the same Wasm module that all use the same shared memory.
For Wasm guest code Rust is used and I try to compile it with following command:
cargo rustc \
--package=wasm_package \
--target=wasm32-wasip1-threads \
-Z build-std=panic_abort,std \
--release \
--crate-type=cdylib \
-- \
-C target-feature='+atomics,+bulk-memory,+mutable-globals' \
-C link-arg=--shared-memory
Using wasm-objdump -x wasm_package.wasm
Import
section contains line:
memory[0] pages: initial=17 max=16384 shared <- env.memory
So I assume that module is correctly compiled with shared memory.
This compiled module successfully executes using wasmtime, wit-bindgen.
Then I would like to initialize multiple Wasm instances with the same shared memory.
Thank you for your help, I greatly appreciate it.
It would be important to correctly initialize instances and their virtual "threads" like allocating separate stack and correctly using that stack when wasm code is called.
https://github.com/bytecodealliance/wasmtime/issues/5
One more project that can spawn multiple threads on wasm and execute tasks:
https://cjycode.com/flutter_rust_bridge/guides/concurrency/sync-rust
Wasm threads on Web: https://web.dev/articles/webassembly-threads with Thread pools ?
Found a good example how Thread Local Storage and Shadow Stack in linear memory can be initialized:
https://blog.stackblitz.com/posts/thread-destroyer/
Good overview from 2018: https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html
Looks like module already contains logic to allocate stack in wasm global variable ($allocate_stack). Will try to initialize component with shared memory and see what happens.
Hi, I'm a bit new to cranelift, I want to use it to build a frontend for my compiler, do you have a tutorial or examples in rust that I can use? Thanks in advance
I created a feature request issue: https://github.com/bytecodealliance/wasmtime/issues/10491
I think that there shouldn't be large problems with initializing stack
Main pain point is how to pass SharedMemory to Wasm Instance under Component Model.
Last updated: Apr 07 2025 at 03:17 UTC