DEV Community

Cover image for ๐Ÿงต Run Multi Threads With Rust On Browser โ€” Rayon And 2 Layers Web Worker Mechanism
Toshiya Matsumoto
Toshiya Matsumoto

Posted on

๐Ÿงต Run Multi Threads With Rust On Browser โ€” Rayon And 2 Layers Web Worker Mechanism

There are no OS threads in the browser โ€” so how does Rust's parallel processing library Rayon actually work?
Here's a summary.

๐Ÿค” Why Do We Need This?

When you run heavy computation (taking seconds to tens of seconds) on the browser's main thread, the UI completely freezes. Buttons become unclickable, scrolling stops, and you get the dreaded "This page is not responding" warning.

The solution is multithreading โ€” offload heavy computation to the background, and split the work across multiple threads.


๐Ÿ—๏ธ Two-Layer Web Worker Architecture

Parallel computation in the browser requires Web Workers serving two distinct roles.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ๐Ÿ–ฅ๏ธ Main Thread (UI Thread)          โ”‚
โ”‚                                      โ”‚
โ”‚  User interactions, screen rendering โ”‚
โ”‚  โ†’ Always responsive                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚ ๐Ÿ“จ postMessage
           โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ๐Ÿ‘ท Layer 1: Worker Thread (1)       โ”‚
โ”‚                                      โ”‚
โ”‚  Takes on heavy computation          โ”‚
โ”‚  โ†’ Zero impact on the main thread    โ”‚
โ”‚                                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚ ๐Ÿƒ #1   โ”‚ ๐Ÿƒ #2   โ”‚ ๐Ÿƒ #3   โ”‚     โ”‚
โ”‚  โ”‚ rayon   โ”‚ rayon   โ”‚ rayon   โ”‚     โ”‚  โ† Layer 2
โ”‚  โ”‚ Worker  โ”‚ Worker  โ”‚ Worker  โ”‚     โ”‚    N Web Workers
โ”‚  โ”‚         โ”‚         โ”‚         โ”‚     โ”‚
โ”‚  โ”‚ Part of โ”‚ Part of โ”‚ Part of โ”‚     โ”‚
โ”‚  โ”‚ the workโ”‚ the workโ”‚ the workโ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›ก๏ธ Layer 1 โ€” Protecting the UI

The role of the Layer 1 Web Worker is to move heavy computation out of the main thread.

Rayon doesn't come into play here yet โ€” the purpose of this worker thread is simply to decouple from the main thread and keep it free.

An important note: even if you call a Rust async function from WASM, the internal CPU work synchronously blocks the thread. Being async doesn't prevent UI freezing. It's the Web Worker that sets the UI thread free.

โšก Layer 2 โ€” Splitting the Work

With Layer 1 alone, computation still runs sequentially on a single thread. Layer 2 splits the computation itself across multiple threads for actual speedup.


๐Ÿฆ€ Rayon โ€” Parallelise with .par_iter()

Rayon is Rust's parallel computation library. Usage is simple:

// ๐Ÿ˜ด Normal (single-threaded, sequential)
let results: Vec<_> = data.iter()
    .map(|x| heavy_computation(x))
    .collect();

// โšก Rayon (multi-threaded, parallel)
let results: Vec<_> = data.par_iter()    // โ† just add par_
    .map(|x| heavy_computation(x))
    .collect();
Enter fullscreen mode Exit fullscreen mode

Change .iter() to .par_iter() โ€” that's it. Splitting, dispatching, and collecting results is all handled automatically by Rayon.

๐Ÿ˜ด 1 thread:
  Thread 1: [Task 1, Task 2, ..., Task 524,288]
  โ†’ One thread does everything sequentially

โšก 8 threads (Rayon):
  Thread 1: [Task      1 โ€“  65,536]  ๐Ÿƒ
  Thread 2: [Task 65,537 โ€“ 131,072]  ๐Ÿƒ
  ...
  Thread 8: [Task 458,753 โ€“ 524,288]  ๐Ÿƒ
  โ†’ 8 threads working simultaneously โ†’ ~8x faster
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ Same Rust Code, Different Backend

The beauty of Rayon is that the same Rust code runs on both native and browser. Only the underlying thread implementation differs.

         Same Rust code
         data.par_iter()
              โ”‚
     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
     โ†“                 โ†“
 ๐Ÿ–ฅ๏ธ Native         ๐ŸŒ Browser
     โ”‚                 โ”‚
  OS threads      wasm-bindgen-rayon
  (pthread)            โ”‚
     โ”‚            Web Worker ร— N
     โ†“                 โ†“
  Parallel โœ…      Parallel โœ…
Enter fullscreen mode Exit fullscreen mode
  • Native: Rayon โ†’ OS threads (pthread). No configuration needed โ€” it just works.
  • Browser: OS threads don't exist โ†’ wasm-bindgen-rayon translates Rayon's thread requests into Web Workers. initThreadPool(N) spawns N Web Workers.

initThreadPool() is only needed in the browser. On native, Rayon automatically uses OS threads.


๐Ÿ”’ Requirements for Browser Multithreading

Browser multithreading requires several conditions to be met for security reasons.

Requirement Why?
๐Ÿ” HTTPS SharedArrayBuffer (shared memory between threads) is only available over HTTPS
๐Ÿ“ฆ COEP/COOP headers Cross-origin isolation required as a Spectre vulnerability mitigation
โš›๏ธ atomics flag Enables inter-thread memory synchronisation at WASM compile time
๐Ÿ“š build-std Rebuilds Rust's standard library from source with atomics support

โš›๏ธ Why is build-std needed?

The pre-built version of Rust's standard library (std) does not include atomics (shared memory operations). To share memory between threads, std must be rebuilt from source.

# โŒ Pre-built std โ†’ no atomics โ†’ no multithreading
wasm-pack build --release --target web

# โœ… build-std rebuilds std โ†’ atomics enabled โ†’ multithreading works
CARGO_UNSTABLE_BUILD_STD=std,panic_abort wasm-pack build --release --target web
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก We pass this as an environment variable because Cargo's [unstable] section is global and cannot be scoped to WASM targets only. Putting it in the config file would break native builds.


๐ŸŽฏ Summary

User triggers an action ๐Ÿ–ฑ๏ธ
  โ”‚
  โ†“ postMessage
  โ”‚
๐Ÿ‘ท Layer 1: Worker Thread (1)
  โ”‚  โ†’ Does not block the UI
  โ”‚
  โ”œโ†’ Rayon par_iter()
  โ”‚   โ”‚
  โ”‚   โ”œโ†’ ๐Ÿƒ Worker #1: part of the work
  โ”‚   โ”œโ†’ ๐Ÿƒ Worker #2: part of the work
  โ”‚   ...
  โ”‚   โ””โ†’ ๐Ÿƒ Worker #N: part of the work
  โ”‚
  โ”‚   โ†’ All workers complete โ†’ results collected
  โ”‚
  โ†“ postMessage({ status: 'done' })
  โ”‚
๐Ÿ–ฅ๏ธ Main Thread: display result โœ…
   โ†’ UI stayed responsive throughout ๐ŸŽ‰
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”‘ Three Key Takeaways

  1. Layer 1 = Protects the UI. Just one worker. Moves heavy computation off the main thread.
  2. Layer 2 = Makes computation faster. N workers. par_iter() automatically splits the work.
  3. The same Rust code runs on both native and browser. Only the underlying thread implementation differs.

How was that?

Speed up your browser computation with Rust's parallel processing library Rayon โšกโšกโšก

Top comments (0)