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 only once per thread. It cuts down on computation time for potentially expensive cloning operations.


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.

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.
        .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);



A thread local storage container for Rayon jobs


Borrowed thread local variable.