reth_primitives_traits/rayon.rs
1//! Rayon parallel iterator utilities.
2
3use alloc::vec::Vec;
4use rayon::iter::IntoParallelIterator;
5
6/// Extension trait for iterators to convert them to parallel iterators via collection.
7///
8/// This is an alternative to [`rayon::iter::ParallelBridge`] that first collects the iterator
9/// into a `Vec`, then calls [`IntoParallelIterator`] on it. This avoids the mutex contention
10/// that can occur with `par_bridge` when either the iterator's `next()` method is fast or the
11/// parallel tasks are fast, as `par_bridge` wraps the iterator in a mutex.
12///
13/// # When to use
14///
15/// Use `par_bridge_buffered` instead of `par_bridge` when:
16/// - The iterator produces items quickly
17/// - The parallel work per item is relatively light
18/// - The total number of items is known to be reasonable for memory
19///
20/// Stick with `par_bridge` when:
21/// - The iterator is slow (e.g., I/O bound) and you want to overlap iteration with processing
22/// - Memory is constrained and you cannot afford to collect all items upfront
23pub trait ParallelBridgeBuffered: Iterator<Item: Send> + Sized {
24 /// Collects this iterator into a `Vec` and returns a parallel iterator over it.
25 ///
26 /// See [this trait's documentation](ParallelBridgeBuffered) for more details.
27 fn par_bridge_buffered(self) -> rayon::vec::IntoIter<Self::Item> {
28 self.collect::<Vec<_>>().into_par_iter()
29 }
30}
31
32impl<I: Iterator<Item: Send>> ParallelBridgeBuffered for I {}