summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-11-29 16:53:24 -0500
committerDavid Peter <sharkdp@users.noreply.github.com>2023-12-18 21:55:18 +0100
commitfea16227248fe806095ab69ede11f1031c2f40e6 (patch)
tree64c0c83965e0660c1573f0273782e99ef749de5b /src
parent00b64f3ccbfb832ef744bb42bbdfabaf929b5ee2 (diff)
cli: Tweak default thread count logic
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs26
1 files changed, 8 insertions, 18 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 1b02288..3bd8d84 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -715,24 +715,14 @@ impl Opts {
fn default_num_threads() -> NonZeroUsize {
// If we can't get the amount of parallelism for some reason, then
// default to a single thread, because that is safe.
- // Note that the minimum value for a NonZeroUsize is 1.
- // Unfortunately, we can't do `NonZeroUsize::new(1).unwrap()`
- // in a const context.
- const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;
- // As the number of threads increases, the startup time suffers from
- // initializing the threads, and we get diminishing returns from additional
- // parallelism. So set a maximum number of threads to use by default.
- //
- // This value is based on some empirical observations, but the ideal value
- // probably depends on the exact hardware in use.
- //
- // Safety: The literal "20" is known not to be zero.
- const MAX_DEFAULT_THREADS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) };
-
- std::cmp::min(
- std::thread::available_parallelism().unwrap_or(FALLBACK_PARALLELISM),
- MAX_DEFAULT_THREADS,
- )
+ let fallback = NonZeroUsize::MIN;
+ // To limit startup overhead on massively parallel machines, don't use more
+ // than 64 threads.
+ let limit = NonZeroUsize::new(64).unwrap();
+
+ std::thread::available_parallelism()
+ .unwrap_or(fallback)
+ .min(limit)
}
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]