Skip to content

Commit c4c745e

Browse files
committed
cached get_available_parallelism
1 parent c6f7145 commit c4c745e

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

  • datafusion/common/src/utils

datafusion/common/src/utils/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use std::cmp::{Ordering, min};
4040
use std::collections::HashSet;
4141
use std::num::NonZero;
4242
use std::ops::Range;
43-
use std::sync::Arc;
43+
use std::sync::{Arc, LazyLock};
4444
use std::thread::available_parallelism;
4545

4646
/// Applies an optional projection to a [`SchemaRef`], returning the
@@ -923,10 +923,15 @@ pub fn combine_limit(
923923
///
924924
/// This is a wrapper around `std::thread::available_parallelism`, providing a default value
925925
/// of `1` if the system's parallelism cannot be determined.
926+
///
927+
/// The result is cached after the first call.
926928
pub fn get_available_parallelism() -> usize {
927-
available_parallelism()
928-
.unwrap_or(NonZero::new(1).expect("literal value `1` shouldn't be zero"))
929-
.get()
929+
static PARALLELISM: LazyLock<usize> = LazyLock::new(|| {
930+
available_parallelism()
931+
.unwrap_or(NonZero::new(1).expect("literal value `1` shouldn't be zero"))
932+
.get()
933+
});
934+
*PARALLELISM
930935
}
931936

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

0 commit comments

Comments
 (0)