Skip to content

Commit 942b8d9

Browse files
zehikotimsaucer
authored andcommitted
discover parallelism only once to avoid cpu cgroup reads
1 parent e443304 commit 942b8d9

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
@@ -945,9 +945,14 @@ pub fn combine_limit(
945945
/// This is a wrapper around `std::thread::available_parallelism`, providing a default value
946946
/// of `1` if the system's parallelism cannot be determined.
947947
pub fn get_available_parallelism() -> usize {
948-
available_parallelism()
949-
.unwrap_or(NonZero::new(1).expect("literal value `1` shouldn't be zero"))
950-
.get()
948+
use std::sync::OnceLock;
949+
static CACHED_PARALLELISM: OnceLock<usize> = OnceLock::new();
950+
951+
*CACHED_PARALLELISM.get_or_init(|| {
952+
available_parallelism()
953+
.unwrap_or(NonZero::new(1).expect("literal value `1` shouldn't be zero"))
954+
.get()
955+
})
951956
}
952957

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

0 commit comments

Comments
 (0)