What is Rayon?

  • A data-parallelism library for Rust
  • Super lightweight
  • Makes it easy to convert a sequential computation into a parallel one
  • No data races

A quick example

  • Parallel iterators
    • Rayon provides drop in replacements for common operations
    • For example, just replace iterator.iter() to iterator.par_iter() and let rayon do its thing.
use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter()
         .map(|&i| i * i)
         .sum()
}

Guarantees data-race freedom

  • For most cases Rayon guarantees freedom from race conditions.
  • One exception is that if your method has side effects they might occur in a different order.

Refs