Skip to main content

Module pool

Module pool 

Source
Expand description

Data-parallel for over independent chunks, run on helper Web Workers that share this module’s memory, or on rayon’s thread pool in a native build.

Only the threaded wasm build (target_feature = "atomics", shared imported memory) and native builds with the rayon feature have helpers; every other build, and any wasm call made while a job is already running, executes the chunks inline in index order. Natively the helpers are the other threads of the rayon pool the call runs in (the global pool, or one a host entered with ThreadPool::install); a nested call joins that pool. Kernels are written so their output never depends on which thread ran a chunk or in what order: chunks write disjoint output ranges, and any reduction is kept per chunk and folded afterwards in chunk order.

Contract for chunk bodies: no heap allocation and no locks. The calling thread may be a browser main thread, which cannot block on memory.atomic.wait32; it never waits here (it runs chunks itself and then spins for the last few), and because chunk bodies never touch the allocator the helpers can never hold its lock while the caller needs it.

Structs§

SyncPtr
A raw pointer chunk bodies may share; each chunk derives its own disjoint slice from it.

Functions§

chunks_for
A chunk count for len units of work with at least min units per chunk: enough chunks to balance load across helpers, few enough that dispatch stays negligible. Single-threaded builds get 1.
helpers
Helper threads currently parked in worker_loop (0 in single-threaded builds).
par_for
Runs body(i) for every i in 0..chunks, in parallel when helpers exist.
split
Splits 0..len into at most parts contiguous ranges of near-equal size, returned as the start of range i for i in 0..=parts (so range i is bounds(i)..bounds(i + 1)).
worker_loop
Entered once by each helper instance; never returns.