#[derive(Debug, Clone, Copy)]
pub(crate) struct ParallelismThresholds {
two: usize,
four: usize,
eight: Option<usize>,
}
impl ParallelismThresholds {
pub(crate) const fn new(two: usize, four: usize, eight: usize) -> Self {
assert!(two <= four);
assert!(four <= eight);
Self {
two,
four,
eight: Some(eight),
}
}
pub(crate) const fn up_to_four(two: usize, four: usize) -> Self {
assert!(two <= four);
Self {
two,
four,
eight: None,
}
}
}
pub(crate) fn parallel_chunk_size(
task_count: usize,
work_units: usize,
thresholds: ParallelismThresholds,
) -> Option<usize> {
let available = rayon::current_num_threads().min(task_count).min(8);
if available < 2 || work_units < thresholds.two {
return None;
}
let desired = if available >= 8 && thresholds.eight.is_some_and(|eight| work_units >= eight) {
8
} else if available >= 4 && work_units >= thresholds.four {
4
} else {
2
};
Some(task_count.div_ceil(desired.min(available)))
}
#[cfg(test)]
#[path = "./parallelism_test.rs"]
mod parallelism_test;