|
1 | 1 | use std::{ |
2 | 2 | fs::File, |
3 | 3 | path::{Path, PathBuf}, |
| 4 | + sync::{Arc, Mutex}, |
4 | 5 | }; |
5 | 6 |
|
6 | 7 | use error_stack::{Result, ResultExt}; |
7 | 8 | use fast_glob::glob_match; |
8 | | -use ignore::WalkBuilder; |
| 9 | +use ignore::{WalkBuilder, WalkParallel, WalkState}; |
9 | 10 | use rayon::iter::{IntoParallelIterator, ParallelIterator}; |
10 | 11 | use tracing::{instrument, warn}; |
11 | 12 |
|
@@ -53,12 +54,29 @@ impl<'a> ProjectBuilder<'a> { |
53 | 54 | let mut entry_types = Vec::with_capacity(INITIAL_VECTOR_CAPACITY); |
54 | 55 | let mut builder = WalkBuilder::new(&self.base_path); |
55 | 56 | builder.hidden(false); |
56 | | - let walkdir = builder.build(); |
| 57 | + let walk_parallel: WalkParallel = builder.build_parallel(); |
57 | 58 |
|
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 { |
60 | 77 | entry_types.push(self.build_entry_type(entry)?); |
61 | 78 | } |
| 79 | + |
62 | 80 | self.build_project_from_entry_types(entry_types) |
63 | 81 | } |
64 | 82 |
|
|
0 commit comments