Merge pull request #1410 from tmccombs/available-parallelism
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Replace num_cpus with `std::thread::available_parallelism`
authored by Thayne McCombs · committed by GitHub · parents 8b5532d, 1d57b3a · Browse files · 4 files·+17 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+17 added−32 removed
- other:+17 added−32 removed
Provenance
- Commit
c96b1af- Tree
144e0e9- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
8b5532d- Parent 2
1d57b3a
4 files changed+17 added−32 removed
modified:src/cli.rs+16 added-13 removed
1+use std::num::NonZeroUsize;2 use std::path::{Path, PathBuf};3 use std::time::Duration;4 5 use anyhow::anyhow;491 unmodified lines497 pub color: ColorWhen,498 499 /// Set number of threads to use for searching & executing (default: number500 /// of available CPU cores)500- #[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = clap::value_parser!(u32).range(1..))]501- pub threads: Option<u32>,501+ #[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = str::parse::<NonZeroUsize>)]502+ pub threads: Option<NonZeroUsize>,503 504 /// Milliseconds to buffer before streaming search results to console505 ///506 /// Amount of time in milliseconds to buffer, before streaming the search180 unmodified lines687 pub fn min_depth(&self) -> Option<usize> {688 self.min_depth.or(self.exact_depth)689 }690 690- pub fn threads(&self) -> usize {691- // This will panic if the number of threads passed in is more than usize::MAX in an environment692- // where usize is less than 32 bits (for example 16-bit architectures). It's pretty693- // unlikely fd will be running in such an environment, and even more unlikely someone would694- // be trying to use that many threads on such an environment, so I think panicing is an695- // 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- )691+ pub fn threads(&self) -> NonZeroUsize {692+ self.threads.unwrap_or_else(default_num_threads)693 }694 695 pub fn max_results(&self) -> Option<usize> {696 self.max_results13 unmodified lines710 .transpose()711 }712 }713 714+/// Get the default number of threads to use, if not explicitly specified.715+fn default_num_threads() -> NonZeroUsize {716+ // If we can't get the amount of parallelism for some reason, then717+ // default to a single thread, because that is safe.718+ // Note that the minimum value for a NonZeroUsize is 1.719+ // Unfortunately, we can't do `NonZeroUsize::new(1).unwrap()`720+ // in a const context.721+ const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;722+ std::thread::available_parallelism().unwrap_or(FALLBACK_PARALLELISM)723+}724+725 #[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]726 pub enum FileType {727 #[value(alias = "f")]728 File,154 unmodified linesmodified:src/main.rs+1 added-1 removed
250 unmodified lines251 quiet: opts.quiet,252 max_depth: opts.max_depth(),253 min_depth: opts.min_depth(),254 prune: opts.prune,255- threads: opts.threads(),255+ threads: opts.threads().get(),256 max_buffer_time: opts.max_buffer_time,257 ls_colors,258 interactive_terminal,259 file_types: opts.filetype.as_ref().map(|values| {214 unmodified linesmodified:Cargo.lock-17 removed
310 unmodified lines311 "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 lines356 source = "registry+https://github.com/rust-lang/crates.io-index"357 checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"358 359 [[package]]361-name = "hermit-abi"362-version = "0.3.3"363-source = "registry+https://github.com/rust-lang/crates.io-index"364-checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"365-366-[[package]]360 name = "home"361 version = "0.5.5"362 source = "registry+https://github.com/rust-lang/crates.io-index"363 checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb"175 unmodified lines539 "autocfg",540 ]541 542 [[package]]550-name = "num_cpus"551-version = "1.16.0"552-source = "registry+https://github.com/rust-lang/crates.io-index"553-checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"554-dependencies = [555- "hermit-abi",556- "libc",557-]558-559-[[package]]543 name = "once_cell"544 version = "1.18.0"545 source = "registry+https://github.com/rust-lang/crates.io-index"546 checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"411 unmodified linesmodified:Cargo.toml-1 removed
36 unmodified lines37383940414142434450 unmodified lines36 unmodified linesaho-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"humantime = "2.1"50 unmodified lines