Skip to content

Commit

Permalink
Merge pull request #1410 from tmccombs/available-parallelism
Browse files Browse the repository at this point in the history
Replace num_cpus with `std::thread::available_parallelism`
  • Loading branch information
tmccombs authored Oct 28, 2023
2 parents 8b5532d + 1d57b3a commit c96b1af
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
17 changes: 0 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 16 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::time::Duration;

Expand Down Expand Up @@ -497,8 +498,8 @@ pub struct Opts {

/// Set number of threads to use for searching & executing (default: number
/// of available CPU cores)
#[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = clap::value_parser!(u32).range(1..))]
pub threads: Option<u32>,
#[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = str::parse::<NonZeroUsize>)]
pub threads: Option<NonZeroUsize>,

/// Milliseconds to buffer before streaming search results to console
///
Expand Down Expand Up @@ -687,17 +688,8 @@ impl Opts {
self.min_depth.or(self.exact_depth)
}

pub fn threads(&self) -> usize {
// This will panic if the number of threads passed in is more than usize::MAX in an environment
// where usize is less than 32 bits (for example 16-bit architectures). It's pretty
// 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,
)
pub fn threads(&self) -> NonZeroUsize {
self.threads.unwrap_or_else(default_num_threads)
}

pub fn max_results(&self) -> Option<usize> {
Expand All @@ -719,6 +711,17 @@ impl Opts {
}
}

/// Get the default number of threads to use, if not explicitly specified.
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;
std::thread::available_parallelism().unwrap_or(FALLBACK_PARALLELISM)
}

#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum FileType {
#[value(alias = "f")]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
max_depth: opts.max_depth(),
min_depth: opts.min_depth(),
prune: opts.prune,
threads: opts.threads(),
threads: opts.threads().get(),
max_buffer_time: opts.max_buffer_time,
ls_colors,
interactive_terminal,
Expand Down

0 comments on commit c96b1af

Please sign in to comment.