Parallel computation in WASM

A vanilla TSP demo demonstrating where a use case where memory allocation becomes the bottleneck and this makes parallel computation slower than single-threaded when we use the default WASM allocator.

Allocation-heavy computation

This is an intentionally very memory-inefficient implementation. The algorithm allocates a large number of vectors over and over throughout the search. Using the default WASM allocator this makes parallel computation slower than single-threaded.

Observe the fix

Run the algorithm with 1 thread. In a reference computer, this takes 6 seconds.

Then Run the algorithm with 8 threads. On the same computer this takes 1.5 seconds.

The fix

The fix is convenient.

We use a parallel workload friendly allocator instead.

We keep the changes limited, only on the wasm bindings layer, as demonstrated on the right →


You may see the live demo of the version before the fix, using default WASM allocator here.

Enable wasm-allocator feature in wasm_bindings/Cargol.toml

orx-parallel = { version = "4.1",
    features = ["wasm", "wasm-allocator"] }

Use parallel-friendly allocator in wasm_bindings/src/lib.rs

#[cfg(target_arch = "wasm32")]
#[global_allocator]
static GLOBAL_ALLOCATOR: orx_parallel::WasmParallelAllocator<32> =
    orx_parallel::WasmParallelAllocator::new();
Initializing...

Best Distance

-

Elapsed

-

Iterations/s

-