Skip to main content

max_num_threads_for_computation

Function max_num_threads_for_computation 

Source
pub fn max_num_threads_for_computation(
    pool: &impl ThreadPool,
    params: Params,
    size_hint: (usize, Option<usize>),
) -> usize
Expand description

Calculates the actual thread count for a computation considering multiple constraints.

This method implements the core thread count decision logic by combining:

  1. Pool constraint (self.max_num_threads())

    • The thread pool’s maximum capacity
    • Already includes environment variable constraints
  2. Computation constraint (params.num_threads)

    • Per-computation request from .num_threads() method
    • Can be NumThreads::Auto (use all available)
    • Or NumThreads::Max(n) (hard limit)
  3. Input size constraint (known upper bound from size_hint.1)

    • Cannot spawn more threads than input elements
    • When input size is unknown (None), this constraint doesn’t apply

§Returns

The minimum of all constraints, representing the actual thread count to use.

§Decision Logic

let available = self.max_num_threads()           // Pool limit

let requested = match (size_hint.1, params.num_threads) {
    (Some(len), Auto) => min(len, MaxUsize),     // Cap by input size
    (Some(len), Max(n)) => min(len, n),          // Cap by input size and request
    (None, Auto) => MaxUsize,                    // No constraints
    (None, Max(n)) => n,                         // Only respect request
};

return min(requested, available)                 // Final decision

§Parameters

  • params - Contains .num_threads setting from .num_threads() method
  • size_hint - Tuple of (lower_bound, Option<upper_bound>) for input size
    • If upper_bound is None, input size is unknown
    • If upper_bound is Some(n), input has at most n elements