solana/rayon-threadlimit/src/lib.rs

26 lines
692 B
Rust
Raw Permalink Normal View History

2019-09-12 11:39:39 -07:00
#[macro_use]
extern crate lazy_static;
use std::env;
2019-09-12 11:39:39 -07:00
//TODO remove this hack when rayon fixes itself
2019-09-12 11:39:39 -07:00
lazy_static! {
// reduce the number of threads each pool is allowed to half the cpu core count, to avoid rayon
// hogging cpu
static ref MAX_RAYON_THREADS: usize =
env::var("SOLANA_RAYON_THREADS").ok()
.and_then(|num_threads| num_threads.parse().ok())
2022-11-09 11:39:38 -08:00
.unwrap_or_else(|| num_cpus::get() / 2)
.max(1);
2019-09-12 11:39:39 -07:00
}
pub fn get_thread_count() -> usize {
*MAX_RAYON_THREADS
2019-09-12 11:39:39 -07:00
}
// Only used in legacy code.
// Use get_thread_count instead in all new code.
pub fn get_max_thread_count() -> usize {
get_thread_count().saturating_mul(2)
}