Replace num_cpus with std:🧵:available_parallelism

I think this might help startup time a little bit, since it looks like
on linux at least the std implementation uses syscalls to determine the
parallelism, whereas num_cpus was processing the /proc/cpuinfo file.

It also removes another dependency.
This commit is contained in:
Thayne McCombs 2023-10-25 02:00:31 -06:00
parent 8b5532d8dd
commit 325d419e39
3 changed files with 14 additions and 23 deletions

17
Cargo.lock generated
View File

@ -312,7 +312,6 @@ dependencies = [
"nix 0.26.4",
"normpath",
"nu-ansi-term",
"num_cpus",
"regex",
"regex-syntax 0.7.5",
"tempfile",
@ -357,12 +356,6 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "home"
version = "0.5.5"
@ -546,16 +539,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.18.0"

View File

@ -38,7 +38,6 @@ aho-corasick = "1.0"
nu-ansi-term = "0.49"
argmax = "0.3.1"
ignore = "0.4.20"
num_cpus = "1.16"
regex = "1.9.6"
regex-syntax = "0.7"
ctrlc = "3.2"

View File

@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::time::Duration;
@ -693,11 +694,9 @@ impl Opts {
// unlikely fd will be running in such an environment, and even more unlikely someone would
// be trying to use that many threads on such an environment, so I think panicing is an
// appropriate way to handle that.
std::cmp::max(
self.threads
.map_or_else(num_cpus::get, |n| n.try_into().expect("too many threads")),
1,
)
self.threads.map_or_else(default_num_threads, |n| {
std::cmp::max(n.try_into().expect("too many threads"), 1)
})
}
pub fn max_results(&self) -> Option<usize> {
@ -719,6 +718,16 @@ impl Opts {
}
}
/// Get the default number of threads to use, if not explicitly specified.
fn default_num_threads() -> usize {
// If we can't get the amount of parallelism for some reason, then
// default to a single thread, because that is safe.
const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;
std::thread::available_parallelism()
.unwrap_or(FALLBACK_PARALLELISM)
.get()
}
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum FileType {
#[value(alias = "f")]