perf(rayon): restrict thread count (#3667)

This commit is contained in:
David Knaack 2022-02-27 19:37:43 +01:00 committed by GitHub
parent b22cae8c4b
commit 4369c92d40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -2,6 +2,7 @@
use clap::crate_authors;
use std::io;
use std::thread::available_parallelism;
use std::time::SystemTime;
use clap::{IntoApp, Parser, Subcommand};
@ -108,6 +109,7 @@ fn main() {
#[cfg(windows)]
let _ = ansi_term::enable_ansi_support();
logger::init();
init_global_threadpool();
let args = match Cli::try_parse() {
Ok(args) => args,
@ -221,3 +223,19 @@ fn main() {
),
}
}
/// Intialize global `rayon` thread pool
fn init_global_threadpool() {
// Allow overriding the number of threads
let num_threads = std::env::var("STARSHIP_NUM_THREADS")
.ok()
.and_then(|s| s.parse().ok())
// Default to the number of logical cores,
// but restrict the number of threads to 8
.unwrap_or_else(|| available_parallelism().map(usize::from).unwrap_or(1).min(8));
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
.build_global()
.expect("Failed to initialize worker thread pool");
}