Merge pull request #1412 from tmccombs/limit-default-threads

Limit default threads
This commit is contained in:
Thayne McCombs 2023-10-27 23:26:31 -06:00 committed by GitHub
commit 95b4dff379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -5,10 +5,14 @@
- Breaking: `.git/` is now ignored by default when using `--hidden` / `-H`, use `--no-ignore` / `-I` or
`--no-ignore-vcs` to override, see #1387 and #1396 (@skoriop)
## Bugfixes
## Changes
- The default number of threads is now constrained to be at most 16. This should improve startup time on
systems with many CPU cores. (#1203)
## Other
# v8.7.1

View File

@ -719,7 +719,20 @@ fn default_num_threads() -> NonZeroUsize {
// Unfortunately, we can't do `NonZeroUsize::new(1).unwrap()`
// in a const context.
const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;
std::thread::available_parallelism().unwrap_or(FALLBACK_PARALLELISM)
// 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,
)
}
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]