Experimental "turn key" solution for wasm and rayon. This pretty much copies the pool.rs
from the official example, adding some stuff.
Note Currently the library assumes your application is generated with
wasm-pack
using --target web --out-name index
. This is because there is no
way to get import.meta.url
from the WASM module.
To see how to use this library, see the examples/simple/src/lib.rs
. In essence all rayon calls must return a JS Promise
to work correctly, so the API is:
#[wasmyon_promise]
pub fn sum_in_workers() -> i32 {
(0..100000 as i32).into_par_iter().sum::<i32>()
}
This creates a JS wrapper function:
function sum_in_workers(): Promise<any>
The attribute parameters are passed almost as is to the wasm_bindgen, however there is few extra parameters: #[wasmyon_promise(serde)]
means the result should be parsed by serde feature, and #[wasmyon_promise(serde_wasm_bindgen)]
means it should be parsed by serde-wasm-bindgen
crate.
Additionally, if you want to run something in a worker by yourself, you can do it like this:
run_in_worker(|| yourstuff)
It returns a Future
.
To test out, go to examples/simple
directory and do the following:
- Install wasm-pack
- Install deno for static File HTTP server, see file-server-deno.ts 1
- Run
wasm-pack build --target web --out-name index
- Run
deno run --allow-run --allow-net --allow-read ../file-server-deno.ts simple
- Navigate to
http://localhost:8000
- Open a DevTools to see the communication in console
It initalizes only one WebAssembly.Memory
object and shares it between the
workers. It also creates the thread workers within wasm-bindgen JS snippet.
- Auto scalable worker pool, so that it terminates workers when they are not being utilized for a while...
- Number of used threads (max and initial) should be configurable on the fly
- TypeScript requires work. Because
js_sys::Promise
isn't giving a way to type the output type. For now it's justPromise<any>
. To fix this, it probably requires a patch towasm_bindgen
.
1: If you don't want Deno, you still need a file server that is capable of setting headers Cross-Origin-Opener-Policy: same-origin
and Cross-Origin-Embedder-Policy: require-corp
, otherwise SharedArrayBuffer is not defined. See documentation.