Skip to content

Commit ba9cb06

Browse files
committed
perf,fix: Avoid collecting sorted results in batch if no sort required
1 parent 216b771 commit ba9cb06

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/walk.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,23 +407,23 @@ impl WorkerState {
407407
/// threads (for --exec).
408408
fn receive(&self, rx: Receiver<Batch>) -> ExitCode {
409409
let config = &self.config;
410-
411410
// This will be set to `Some` if the `--exec` argument was supplied.
412411
if let Some(ref cmd) = config.command {
413-
if cmd.in_batch_mode() {
414-
let mut results: Vec<WorkerResult> = rx.into_iter().flatten().collect();
415-
if let Some(sort_key) = config.sort_key {
416-
sort_worker_results(&mut results, sort_key);
417-
}
418-
exec::batch(results, cmd, config)
419-
} else if let Some(sort_key) = config.sort_key {
412+
if let Some(sort_key) = config.sort_key {
420413
// With --sort, we must collect all results before dispatching,
421414
// and run sequentially so the order is preserved.
422-
423415
let mut results: Vec<WorkerResult> = rx.into_iter().flatten().collect();
424416
sort_worker_results(&mut results, sort_key);
425-
exec::job(results, cmd, config)
417+
if cmd.in_batch_mode() {
418+
exec::batch(results, cmd, config)
419+
} else {
420+
exec::job(results, cmd, config)
421+
}
422+
} else if cmd.in_batch_mode() {
423+
// Batch mode without sorting.
424+
exec::batch(rx.into_iter().flatten(), cmd, config)
426425
} else {
426+
// No sort. Not Batch mode. Dispatch jobs across a thread pool as results stream in.
427427
thread::scope(|scope| {
428428
// Each spawned job will store its thread handle in here.
429429
let threads = config.threads;

tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ fn test_sort_by_path_with_exec() {
20562056
let te = TestEnv::new(DEFAULT_DIRS, &shuffle_files(DEFAULT_FILES, 42))
20572057
.allow_random_result_order(false);
20582058

2059-
// --exec with --sort should produce output in sorted order
2059+
// --exec with --sort should produce output in sorted order.
20602060
te.assert_output(
20612061
&["foo", "--sort=path", "--exec", "echo", "File: {}"],
20622062
"File: ./a.foo

0 commit comments

Comments
 (0)