use std::sync::atomic::{AtomicUsize, Ordering};
pub(crate) fn parallel_map<T, R, F>(items: &[T], f: F) -> Vec<R>
where
T: Sync,
R: Send,
F: Fn(&T) -> R + Sync,
{
let threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
if threads <= 1 || items.len() <= 1 {
return items.iter().map(&f).collect();
}
let threads = threads.min(items.len());
let chunk_size = items.len().div_ceil(threads);
let mut out: Vec<R> = Vec::with_capacity(items.len());
std::thread::scope(|scope| {
let handles: Vec<_> = items
.chunks(chunk_size)
.map(|chunk| {
let f = &f;
scope.spawn(move || chunk.iter().map(f).collect::<Vec<R>>())
})
.collect();
for h in handles {
out.extend(h.join().expect("worker thread panicked"));
}
});
out
}
pub(crate) fn parallel_map_balanced<T, R, F>(items: &[T], f: F) -> Vec<R>
where
T: Sync,
R: Send,
F: Fn(&T) -> R + Sync,
{
let threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
.min(items.len());
if threads <= 1 {
return items.iter().map(&f).collect();
}
let next = AtomicUsize::new(0);
let parts = std::thread::scope(|scope| {
let handles: Vec<_> = (0..threads)
.map(|_| {
scope.spawn(|| {
let mut out = Vec::new();
loop {
let i = next.fetch_add(1, Ordering::Relaxed);
let Some(item) = items.get(i) else {
break out;
};
out.push((i, f(item)));
}
})
})
.collect();
handles
.into_iter()
.map(|h| h.join().expect("worker thread panicked"))
.collect::<Vec<_>>()
});
let mut slots = (0..items.len()).map(|_| None).collect::<Vec<_>>();
for (i, value) in parts.into_iter().flatten() {
slots[i] = Some(value);
}
slots.into_iter().map(Option::unwrap).collect()
}
pub(crate) fn parallel_chunks<T, R, F>(items: &[T], f: F) -> Vec<R>
where
T: Sync,
R: Send,
F: Fn(&[T]) -> R + Sync,
{
parallel_chunks_capped(items, usize::MAX, f)
}
pub(crate) fn parallel_chunks_capped<T, R, F>(items: &[T], max_chunks: usize, f: F) -> Vec<R>
where
T: Sync,
R: Send,
F: Fn(&[T]) -> R + Sync,
{
let threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
.min(max_chunks.max(1));
if threads <= 1 || items.len() <= 1 {
return vec![f(items)];
}
let threads = threads.min(items.len());
let chunk_size = items.len().div_ceil(threads);
let mut out: Vec<R> = Vec::new();
std::thread::scope(|scope| {
let handles: Vec<_> = items
.chunks(chunk_size)
.map(|chunk| {
let f = &f;
scope.spawn(move || f(chunk))
})
.collect();
for h in handles {
out.push(h.join().expect("worker thread panicked"));
}
});
out
}