Skip to main content

tinyquant_core/codec/
parallelism.rs

1//! Parallelism dispatch for batch codec methods (Phase 15).
2//!
3//! Phase 15 implements only [`Parallelism::Serial`]; `Custom` is the
4//! escape hatch for Phase 21's rayon integration.
5
6/// Strategy for dispatching row-parallel work inside batch codec methods.
7#[derive(Clone, Copy, Default)]
8pub enum Parallelism {
9    /// Execute rows sequentially on the current thread.
10    #[default]
11    Serial,
12    /// Defer to a caller-supplied driver (e.g. rayon).
13    ///
14    /// The driver is a plain function pointer, so it cannot close over state
15    /// such as a specific `rayon::ThreadPool`. To target a non-global pool,
16    /// install it before invoking `compress_batch_with`:
17    /// `pool.install(|| codec.compress_batch_with(..., Parallelism::Custom(driver)))`.
18    Custom(fn(count: usize, body: &(dyn Fn(usize) + Sync + Send))),
19}
20
21impl Parallelism {
22    /// Drive `body` once per row, honoring the selected strategy.
23    pub fn for_each_row<F>(self, count: usize, body: F)
24    where
25        F: Fn(usize) + Sync + Send,
26    {
27        match self {
28            Self::Serial => (0..count).for_each(body),
29            Self::Custom(driver) => driver(count, &body),
30        }
31    }
32}