Mercurial > public > mercurial-scm > hg-stable
diff rust/hg-core/src/utils.rs @ 50321:14b57943ae6d stable
rust: fix thread cap (for real this time)
Both e2f8ed37201c and c52435820bbd failed to put a *default* ceiling on
the number of threads used by Rayon to prevent a contention issue.
Calling `rayon::available_parallelism()` creates the global threadpool,
which made our whole dance useless last time.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Thu, 23 Mar 2023 19:10:15 +0100 |
parents | a6b8b1ab9116 |
children | 331a3cbe1c9e |
line wrap: on
line diff
--- a/rust/hg-core/src/utils.rs Wed Mar 22 17:18:32 2023 +0000 +++ b/rust/hg-core/src/utils.rs Thu Mar 23 19:10:15 2023 +0100 @@ -498,3 +498,35 @@ Err(e) => Some(Err(e)), }) } + +/// Force the global rayon threadpool to not exceed 16 concurrent threads +/// unless the user has specified a value. +/// This is a stop-gap measure until we figure out why using more than 16 +/// threads makes `status` slower for each additional thread. +/// +/// TODO find the underlying cause and fix it, then remove this. +/// +/// # Errors +/// +/// Returns an error if the global threadpool has already been initialized if +/// we try to initialize it. +pub fn cap_default_rayon_threads() -> Result<(), rayon::ThreadPoolBuildError> { + const THREAD_CAP: usize = 16; + + if std::env::var("RAYON_NUM_THREADS").is_err() { + let available_parallelism = std::thread::available_parallelism() + .map(usize::from) + .unwrap_or(1); + let new_thread_count = THREAD_CAP.min(available_parallelism); + let res = rayon::ThreadPoolBuilder::new() + .num_threads(new_thread_count) + .build_global(); + if res.is_ok() { + log::trace!( + "Capped the rayon threadpool to {new_thread_count} threads", + ); + } + return res; + } + Ok(()) +}