Skip to content

Commit ef1975b

Browse files
committed
discover parallelism only once to avoid cpu cgroup reads
1 parent 68f2903 commit ef1975b

File tree

1 file changed

+8
-3
lines changed
  • datafusion/common/src/utils

1 file changed

+8
-3
lines changed

datafusion/common/src/utils/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,14 @@ pub fn combine_limit(
928928
/// This is a wrapper around `std::thread::available_parallelism`, providing a default value
929929
/// of `1` if the system's parallelism cannot be determined.
930930
pub fn get_available_parallelism() -> usize {
931-
available_parallelism()
932-
.unwrap_or(NonZero::new(1).expect("literal value `1` shouldn't be zero"))
933-
.get()
931+
use std::sync::OnceLock;
932+
static CACHED_PARALLELISM: OnceLock<usize> = OnceLock::new();
933+
934+
*CACHED_PARALLELISM.get_or_init(|| {
935+
available_parallelism()
936+
.unwrap_or(NonZero::new(1).expect("literal value `1` shouldn't be zero"))
937+
.get()
938+
})
934939
}
935940

936941
/// Converts a collection of function arguments into an fixed-size array of length N

0 commit comments

Comments
 (0)