From e78690e4f59f6aeeffa778336b708418d9291a93 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 6 May 2024 20:09:01 +0100 Subject: chore: Cleanup threads commit --- src/cli.rs | 2 +- src/config.rs | 9 +++++++++ src/main.rs | 43 ++++++++++++++++++------------------------- todo.txt | 1 - 4 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 todo.txt diff --git a/src/cli.rs b/src/cli.rs index fce5322..5be6988 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -20,7 +20,7 @@ pub fn build_cli() -> Command { Arg::new("threads") .short('T') .long("threads") - .value_parser(value_parser!(u8)) + .value_parser(value_parser!(usize)) .help("Number of threads to use") .num_args(1) ) diff --git a/src/config.rs b/src/config.rs index 32262ad..edc1113 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,6 +29,7 @@ pub struct Config { pub depth: Option, pub bars_on_right: Option, pub stack_size: Option, + pub threads: Option, pub output_json: Option, } @@ -118,6 +119,14 @@ impl Config { from_cmd_line.copied() } } + pub fn get_threads(&self, options: &ArgMatches) -> Option { + let from_cmd_line = options.get_one::("threads"); + if from_cmd_line.is_none() { + self.threads + } else { + from_cmd_line.copied() + } + } pub fn get_output_json(&self, options: &ArgMatches) -> bool { Some(true) == self.output_json || options.get_flag("output_json") } diff --git a/src/main.rs b/src/main.rs index d55f6bf..99e8e35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -228,7 +228,7 @@ fn main() { progress_data: indicator.data.clone(), errors: errors_for_rayon, }; - let threads_to_use: Option = options.get_one::("threads").copied(); + let threads_to_use = config.get_threads(&options); let stack_size = config.get_custom_stack_size(&options); init_rayon(&stack_size, &threads_to_use); @@ -312,7 +312,7 @@ fn main() { } } -fn init_rayon(stack_size: &Option, threads: &Option) { +fn init_rayon(stack_size: &Option, threads: &Option) { // Rayon seems to raise this error on 32-bit builds // The global thread pool has not been initialized.: ThreadPoolBuildError { kind: GlobalPoolAlreadyInitialized } if cfg!(target_pointer_width = "64") { @@ -332,38 +332,31 @@ fn output_json(output_filename: &str, top_level_nodes: &Vec) -> std::io::R fn build_thread_pool( stack: Option, - threads: Option, + threads: Option, ) -> Result<(), rayon::ThreadPoolBuildError> { - match stack { - Some(s) => match threads { - Some(t) => rayon::ThreadPoolBuilder::new() - .num_threads(t.into()) - .stack_size(s) - .build_global(), - None => rayon::ThreadPoolBuilder::new().stack_size(s).build_global(), - }, + let mut pool = rayon::ThreadPoolBuilder::new(); + + if let Some(thread_count) = threads { + pool = pool.num_threads(thread_count); + } + + let stack_size = match stack { + Some(s) => Some(s), None => { let large_stack = usize::pow(1024, 3); let mut s = System::new(); s.refresh_memory(); + // Larger stack size if possible to handle cases with lots of nested directories let available = s.available_memory(); if available > large_stack.try_into().unwrap() { - match threads { - Some(t) => - // Larger stack size to handle cases with lots of nested directories - { - rayon::ThreadPoolBuilder::new() - .num_threads(t.into()) - .stack_size(large_stack) - .build_global() - } - None => rayon::ThreadPoolBuilder::new() - .stack_size(large_stack) - .build_global(), - } + Some(large_stack) } else { - rayon::ThreadPoolBuilder::new().build_global() + None } } + }; + if let Some(stack_size_param) = stack_size { + pool = pool.stack_size(stack_size_param); } + pool.build_global() } diff --git a/todo.txt b/todo.txt deleted file mode 100644 index 84ed370..0000000 --- a/todo.txt +++ /dev/null @@ -1 +0,0 @@ -do not commit completions/_dust file -- cgit v1.2.3