diff --git a/multilinear_extensions/src/util.rs b/multilinear_extensions/src/util.rs index a0a8e56a2..230f187f4 100644 --- a/multilinear_extensions/src/util.rs +++ b/multilinear_extensions/src/util.rs @@ -40,11 +40,16 @@ pub fn max_usable_threads() -> usize { if cfg!(test) { 1 } else { - let n = rayon::current_num_threads(); - let threads = prev_power_of_two(n); - if n != threads { - tracing::warn!("thread size {n} is not power of 2, using {threads} threads instead."); - } - threads + static MAX_USABLE_THREADS: std::sync::OnceLock = std::sync::OnceLock::new(); + *MAX_USABLE_THREADS.get_or_init(|| { + let n = rayon::current_num_threads(); + let threads = prev_power_of_two(n); + if n != threads { + tracing::warn!( + "thread size {n} is not power of 2, using {threads} threads instead." + ); + } + threads + }) } }