From bb9658d400bdf6db344d0c30e45a310e427e401e Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Sat, 30 Nov 2024 17:43:50 +0000 Subject: [PATCH 1/4] feat: add concurrent working threads option to CLI args --- src/cli/args.rs | 5 +++++ src/cli/mod.rs | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/cli/args.rs b/src/cli/args.rs index 05b522c60..42b9d1b73 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -45,6 +45,10 @@ pub struct CliArgs { #[arg(short = 'p', long = "password", global = true)] pub password: Option, + /// cocurrent working threads + #[arg(short = 't', long, global = true)] + pub threads: Option, + // Ouch and claps subcommands #[command(subcommand)] pub cmd: Subcommand, @@ -138,6 +142,7 @@ mod tests { format: None, // This is usually replaced in assertion tests password: None, + threads: None, cmd: Subcommand::Decompress { // Put a crazy value here so no test can assert it unintentionally files: vec!["\x00\x11\x22".into()], diff --git a/src/cli/mod.rs b/src/cli/mod.rs index ce6b5d591..4144e53ef 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -28,6 +28,13 @@ impl CliArgs { set_accessible(args.accessible); + if let Some(threads) = args.threads { + rayon::ThreadPoolBuilder::new() + .num_threads(threads) + .build_global() + .unwrap(); + } + let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. } | Subcommand::List { archives: files, .. }) = &mut args.cmd; From 0ee6ae193beb2d972a4ae30e60cb7c15bf5f40ef Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Mon, 2 Dec 2024 12:49:19 +0000 Subject: [PATCH 2/4] refactor(cli): move thread pool setup to command execution, use thread::spawn instead of rayon::spawn in the logger thread --- src/cli/mod.rs | 7 ------- src/commands/mod.rs | 8 ++++++++ src/utils/logger.rs | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4144e53ef..ce6b5d591 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -28,13 +28,6 @@ impl CliArgs { set_accessible(args.accessible); - if let Some(threads) = args.threads { - rayon::ThreadPoolBuilder::new() - .num_threads(threads) - .build_global() - .unwrap(); - } - let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. } | Subcommand::List { archives: files, .. }) = &mut args.cmd; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f2963591b..e2e4890d9 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -52,6 +52,14 @@ pub fn run( question_policy: QuestionPolicy, file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result<()> { + + if let Some(threads) = args.threads { + rayon::ThreadPoolBuilder::new() + .num_threads(threads) + .build_global() + .unwrap(); + } + match args.cmd { Subcommand::Compress { files, diff --git a/src/utils/logger.rs b/src/utils/logger.rs index 5156308e1..60936c665 100644 --- a/src/utils/logger.rs +++ b/src/utils/logger.rs @@ -1,4 +1,5 @@ use std::sync::{mpsc, Arc, Barrier, OnceLock}; +use std::thread; pub use logger_thread::spawn_logger_thread; @@ -168,7 +169,7 @@ mod logger_thread { pub fn spawn_logger_thread() { let log_receiver = setup_channel(); - rayon::spawn(move || run_logger(log_receiver)); + thread::spawn(move || run_logger(log_receiver)); } fn run_logger(log_receiver: LogReceiver) { From d7663477e856550eafeb6081b4f6a8a18cd61b35 Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Mon, 2 Dec 2024 12:50:46 +0000 Subject: [PATCH 3/4] refactor: improve code formatting in `mod.rs` and `logger.rs` --- src/commands/mod.rs | 1 - src/utils/logger.rs | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e2e4890d9..e70860247 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -52,7 +52,6 @@ pub fn run( question_policy: QuestionPolicy, file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result<()> { - if let Some(threads) = args.threads { rayon::ThreadPoolBuilder::new() .num_threads(threads) diff --git a/src/utils/logger.rs b/src/utils/logger.rs index 60936c665..0de2dfda7 100644 --- a/src/utils/logger.rs +++ b/src/utils/logger.rs @@ -1,5 +1,7 @@ -use std::sync::{mpsc, Arc, Barrier, OnceLock}; -use std::thread; +use std::{ + sync::{mpsc, Arc, Barrier, OnceLock}, + thread, +}; pub use logger_thread::spawn_logger_thread; From 07074031c13f8dd81788ba50f64cf43ff26a86eb Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Tue, 3 Dec 2024 17:12:31 +0000 Subject: [PATCH 4/4] fix: change threads short flag to -c in cli args to avoid conflict with `tree` --- src/cli/args.rs | 2 +- tests/snapshots/ui__ui_test_usage_help_flag-2.snap | 2 ++ tests/snapshots/ui__ui_test_usage_help_flag.snap | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cli/args.rs b/src/cli/args.rs index 42b9d1b73..a3b7232c7 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -46,7 +46,7 @@ pub struct CliArgs { pub password: Option, /// cocurrent working threads - #[arg(short = 't', long, global = true)] + #[arg(short = 'c', long, global = true)] pub threads: Option, // Ouch and claps subcommands diff --git a/tests/snapshots/ui__ui_test_usage_help_flag-2.snap b/tests/snapshots/ui__ui_test_usage_help_flag-2.snap index cb937d79d..058d54ed8 100644 --- a/tests/snapshots/ui__ui_test_usage_help_flag-2.snap +++ b/tests/snapshots/ui__ui_test_usage_help_flag-2.snap @@ -1,6 +1,7 @@ --- source: tests/ui.rs expression: "output_to_string(ouch!(\"-h\"))" +snapshot_kind: text --- A command-line utility for easily compressing and decompressing files and directories. @@ -21,5 +22,6 @@ Options: -g, --gitignore Ignores files matched by git's ignore files -f, --format Specify the format of the archive -p, --password decompress or list with password + -c, --threads cocurrent working threads -h, --help Print help (see more with '--help') -V, --version Print version diff --git a/tests/snapshots/ui__ui_test_usage_help_flag.snap b/tests/snapshots/ui__ui_test_usage_help_flag.snap index 921aa8d07..f8a2ad42f 100644 --- a/tests/snapshots/ui__ui_test_usage_help_flag.snap +++ b/tests/snapshots/ui__ui_test_usage_help_flag.snap @@ -1,6 +1,7 @@ --- source: tests/ui.rs expression: "output_to_string(ouch!(\"--help\"))" +snapshot_kind: text --- A command-line utility for easily compressing and decompressing files and directories. @@ -43,6 +44,9 @@ Options: -p, --password decompress or list with password + -c, --threads + cocurrent working threads + -h, --help Print help (see a summary with '-h')