solana_rayon_threadlimit/
lib.rs

1use std::env;
2//TODO remove this hack when rayon fixes itself
3
4// reduce the number of threads each pool is allowed to half the cpu core count, to avoid rayon
5// hogging cpu
6static MAX_RAYON_THREADS: std::sync::LazyLock<usize> = std::sync::LazyLock::new(|| {
7    env::var("SOLANA_RAYON_THREADS")
8        .ok()
9        .and_then(|num_threads| num_threads.parse().ok())
10        .unwrap_or_else(|| num_cpus::get() / 2)
11        .max(1)
12});
13
14pub fn get_thread_count() -> usize {
15    *MAX_RAYON_THREADS
16}
17
18// Only used in legacy code.
19// Use get_thread_count instead in all new code.
20pub fn get_max_thread_count() -> usize {
21    get_thread_count().saturating_mul(2)
22}