vortex_utils/parallelism.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Useful utilities for discovering the desired level of parallelism
5
6use std::sync::LazyLock;
7
8/// Estimates the degree of parallelism the program should use, caching the result after the first call.
9///
10/// This is currently implemented using [`std::thread::available_parallelism`], but might change in the future.
11///
12/// Returns `None` if the underlying functions fails.
13pub fn get_available_parallelism() -> Option<usize> {
14 #[allow(clippy::disallowed_methods)]
15 static PARALLELISM: LazyLock<Option<usize>> =
16 LazyLock::new(|| std::thread::available_parallelism().ok().map(|n| n.get()));
17
18 *PARALLELISM
19}