Crate rayon_tlsctx[][src]

Expand description

Clone only when it’s necessary

This library provides an efficient way to clone values in a rayon thread pool, but usually just once per thread. It cuts down on computation time for potentially expensive cloning operations.

Additional clones can rarely occur when rayon schedules execution of another instance of the same job, recursively. But in the end, there should not be more than 2N clones, for N threads.

Examples

use rayon_tlsctx::ThreadLocalCtx;
use rayon::iter::*;

const NUM_COPIES: usize = 16;

let mut buf: Vec<u16> = (0..!0).collect();

// Create a thread local context with value 0.
let ctx = ThreadLocalCtx::new(|| {
    // Simulate expensive operation.
    // Since we are building unlocked context,
    // the sleeps will occur concurrently.
    std::thread::sleep_ms(200);
    0
});

let pool = rayon::ThreadPoolBuilder::new().num_threads(64).build().unwrap();

// Run inside a custom thread pool.
pool.install(|| {
    // Sum the buffer `NUM_COPIES` times and accumulate the results
    // into the threaded pool of counts. Note that the counts may be
    // Unevenly distributed.
    (0..NUM_COPIES)
        .into_par_iter()
        .flat_map(|_| buf.par_iter())
        .for_each(|i| {
            let mut cnt = unsafe { ctx.get() };
            *cnt += *i as usize;
        });
});


let buf_sum = buf.into_iter().fold(0, |acc, i| acc + i as usize);

// What matters is that the final sum matches the expected value.
assert_eq!(ctx.into_iter().sum::<usize>(), buf_sum * NUM_COPIES);

Structs

ThreadLocalCtx

A thread local storage container for Rayon jobs

ThreadLocalMut

Borrowed thread local variable.

ThreadNodeIterator

Final iterator for ThreadLocalCtx.