Skip to content

Commit 5d0c6b1

Browse files
committed
parallel walk dir - 25% faster
1 parent fee60a7 commit 5d0c6b1

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum Command {
2727
visible_alias = "g"
2828
)]
2929
Generate {
30-
#[arg(long, short,default_value = "false", help = "Skip staging the CODEOWNERS file")]
30+
#[arg(long, short, default_value = "false", help = "Skip staging the CODEOWNERS file")]
3131
skip_stage: bool,
3232
},
3333

@@ -39,7 +39,7 @@ enum Command {
3939

4040
#[clap(about = "Chains both `generate` and `validate` commands.", visible_alias = "gv")]
4141
GenerateAndValidate {
42-
#[arg(long, short,default_value = "false", help = "Skip staging the CODEOWNERS file")]
42+
#[arg(long, short, default_value = "false", help = "Skip staging the CODEOWNERS file")]
4343
skip_stage: bool,
4444
},
4545

src/project_builder.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::{
22
fs::File,
33
path::{Path, PathBuf},
4+
sync::{Arc, Mutex},
45
};
56

67
use error_stack::{Result, ResultExt};
78
use fast_glob::glob_match;
8-
use ignore::WalkBuilder;
9+
use ignore::{WalkBuilder, WalkParallel, WalkState};
910
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1011
use tracing::{instrument, warn};
1112

@@ -53,12 +54,29 @@ impl<'a> ProjectBuilder<'a> {
5354
let mut entry_types = Vec::with_capacity(INITIAL_VECTOR_CAPACITY);
5455
let mut builder = WalkBuilder::new(&self.base_path);
5556
builder.hidden(false);
56-
let walkdir = builder.build();
57+
let walk_parallel: WalkParallel = builder.build_parallel();
5758

58-
for entry in walkdir {
59-
let entry = entry.change_context(Error::Io)?;
59+
let collected = Arc::new(Mutex::new(Vec::with_capacity(INITIAL_VECTOR_CAPACITY)));
60+
let collected_for_threads = Arc::clone(&collected);
61+
62+
walk_parallel.run(move || {
63+
let collected = Arc::clone(&collected_for_threads);
64+
Box::new(move |res| {
65+
if let Ok(entry) = res {
66+
if let Ok(mut v) = collected.lock() {
67+
v.push(entry);
68+
}
69+
}
70+
WalkState::Continue
71+
})
72+
});
73+
74+
// Process sequentially with &mut self
75+
let collected_entries = Arc::try_unwrap(collected).unwrap().into_inner().unwrap();
76+
for entry in collected_entries {
6077
entry_types.push(self.build_entry_type(entry)?);
6178
}
79+
6280
self.build_project_from_entry_types(entry_types)
6381
}
6482

src/runner.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,9 @@ impl Runner {
244244
})
245245
}
246246

247-
pub fn validate(&self ) -> RunResult {
247+
pub fn validate(&self) -> RunResult {
248248
match self.ownership.validate() {
249-
Ok(_) => {
250-
RunResult::default()
251-
},
249+
Ok(_) => RunResult::default(),
252250
Err(err) => RunResult {
253251
validation_errors: vec![format!("{}", err)],
254252
..Default::default()
@@ -267,7 +265,7 @@ impl Runner {
267265
self.git_stage();
268266
}
269267
RunResult::default()
270-
},
268+
}
271269
Err(err) => RunResult {
272270
io_errors: vec![err.to_string()],
273271
..Default::default()

0 commit comments

Comments
 (0)