Skip to content

Commit 85fa6a8

Browse files
committed
Add par_partition
1 parent 477ad98 commit 85fa6a8

File tree

1 file changed

+11
-0
lines changed
  • src/librustc_data_structures

1 file changed

+11
-0
lines changed

src/librustc_data_structures/sync.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
2020
use crate::owning_ref::{Erased, OwningRef};
2121
use std::any::Any;
22+
use std::cmp;
2223
use std::collections::HashMap;
2324
use std::hash::{BuildHasher, Hash};
2425
use std::marker::PhantomData;
@@ -823,3 +824,13 @@ impl<T> DerefMut for OneThread<T> {
823824
&mut self.inner
824825
}
825826
}
827+
828+
/// Splits the slice into parts and runs `f` on them in parallel.
829+
pub fn par_partition<'a, T: Sync, R: Send>(
830+
slice: &'a [T],
831+
parts: usize,
832+
f: impl Fn(&'a [T]) -> R + Sync,
833+
) -> Vec<R> {
834+
let chunks: Vec<_> = slice.chunks(cmp::max((slice.len() + parts - 1) / parts, 1)).collect();
835+
par_map(chunks, |chunk| f(chunk))
836+
}

0 commit comments

Comments
 (0)