Skip to content
Myr

Replace num_cpus with std::thread::available_parallelism

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Thayne McCombs · committed by Thayne McCombs · parent 8b5532d · Browse files · 3 files+14 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+14 added−23 removed

  • other:+14 added−23 removed

3 files+14 added−23 removed

modified:src/cli.rs+14 added-5 removed
1use std::num::NonZeroUsize;2use std::path::{Path, PathBuf};3use std::time::Duration;45use anyhow::anyhow;
687 unmodified lines
693 // where usize is less than 32 bits (for example 16-bit architectures). It's pretty694 // unlikely fd will be running in such an environment, and even more unlikely someone would695 // be trying to use that many threads on such an environment, so I think panicing is an696 // appropriate way to handle that.696 std::cmp::max(697 self.threads698 .map_or_else(num_cpus::get, |n| n.try_into().expect("too many threads")),699 1,700 )697 self.threads.map_or_else(default_num_threads, |n| {698 std::cmp::max(n.try_into().expect("too many threads"), 1)699 })700 }701702 pub fn max_results(&self) -> Option<usize> {703 self.max_results
13 unmodified lines
717 .transpose()718 }719}720721/// Get the default number of threads to use, if not explicitly specified.722fn default_num_threads() -> usize {723 // If we can't get the amount of parallelism for some reason, then724 // default to a single thread, because that is safe.725 const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;726 std::thread::available_parallelism()727 .unwrap_or(FALLBACK_PARALLELISM)728 .get()729}730731#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]732pub enum FileType {733 #[value(alias = "f")]734 File,
154 unmodified lines
modified:Cargo.lock-17 removed
310 unmodified lines
311 "lscolors",312 "nix 0.26.4",313 "normpath",314 "nu-ansi-term",315 "num_cpus",315 "regex",316 "regex-syntax 0.7.5",317 "tempfile",318 "test-case",
37 unmodified lines
356source = "registry+https://github.com/rust-lang/crates.io-index"357checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"358359[[package]]361name = "hermit-abi"362version = "0.3.3"363source = "registry+https://github.com/rust-lang/crates.io-index"364checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"365366[[package]]360name = "home"361version = "0.5.5"362source = "registry+https://github.com/rust-lang/crates.io-index"363checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb"
175 unmodified lines
539 "autocfg",540]541542[[package]]550name = "num_cpus"551version = "1.16.0"552source = "registry+https://github.com/rust-lang/crates.io-index"553checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"554dependencies = [555 "hermit-abi",556 "libc",557]558559[[package]]543name = "once_cell"544version = "1.18.0"545source = "registry+https://github.com/rust-lang/crates.io-index"546checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
411 unmodified lines
modified:Cargo.toml-1 removed