Rayon — data parallelism with par_iter and work stealing

Contents

  1. 1. Serial to parallel
  2. 2. What’s under the hood
  3. 3. join
  4. 4. par_bridge

Rayon parallelizes CPU-bound iterators: swap in par_iter (and friends), keep order when Rayon documents it, and let the type system reject data races at compile time.

Serial to parallel

A synchronous sum:

1
2
3
4
5
6
7
8
9
10
11
fn main() {
let sum: i32 = (0..100)
.into_iter()
.map(|i| {
// Simulate some computation
sleep(Duration::from_nanos(1));
i
})
.sum();
assert_eq!(sum, 4950);
}

Parallel version: into_iter → into_par_iter.

Rayon parallelizes the work but still produces the same reduction result (order of the range is preserved for this pattern):

1
2
3
4
5
6
7
8
9
10
11
12
use rayon::prelude::*;
fn main() {
let sum: i32 = (0..100)
.into_par_iter() // here
.map(|i| {
// Simulate some computation
sleep(Duration::from_nanos(1));
i
})
.sum();
assert_eq!(sum, 4950);
}

On a 10-core M1 Pro, divan showed a clear win for this toy loop:

Benchmark Fastest Slowest Median Mean Samples Iterations
iter 549.2 µs 1.244 ms 687.4 µs 738.5 µs 100 100
par_iter 195 µs 488.1 µs 315.1 µs 321.9 µs 100 100

What’s under the hood

Rayon backs parallel iterators with a work-stealing thread pool. By default the pool size matches logical CPU cores.

For each parallel job, Rayon splits work into subtasks (roughly sized for the pool) and queues them per thread.

If some threads are idle while others still have local queues, idle workers steal tasks from busy queues so cores stay fed.

Partial results are merged pairwise until the full answer is ready.

Splitting plus stealing is the divide-and-conquer bit that makes par_iter feel magical from the call site.

join

Lower in the stack, rayon::join runs two closures in parallel and returns both outputs:

1
2
3
4
5
6
7
8
9
10
11
12
13
use rayon::prelude::*;

fn main() {
let v1 = vec![1, 2, 3, 4, 5];
let v2 = vec![6, 7, 8, 9, 10];

let (sum1, sum2) = rayon::join(
|| v1.par_iter().sum::<i32>(),
|| v2.par_iter().sum::<i32>()
);

println!("sum1: {}, sum2: {}", sum1, sum2);
}

par_bridge

par_iter shines on indexed or slice-backed data. For blocking sources—channels, files, network—use par_bridge.

There’s overhead: items are pulled one at a time and handed to the pool, and order is not preserved. Sort or reorder afterward if you care.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use rayon::iter::ParallelBridge;
use rayon::prelude::ParallelIterator;
use std::sync::mpsc::channel;

fn main() {
let rx = {
let (tx, rx) = channel();

(1..=3).into_iter().for_each(|i| {
let _ = tx.send(i);
});

rx
};

let mut output: Vec<i32> = rx.into_iter().par_bridge().collect();
output.sort_unstable(); // restore order

assert_eq!(&*output, &[1, 2, 3]);
}

Not every for loop deserves Rayon—thread scheduling has cost. For cheap scans, stay serial. For heavier per-item work, into_par_iter is often the fastest one-line change.

Background: Rayon: data parallelism in Rust

Questions? Leave a comment below or email: newbvirgil@gmail.com