Skip to content

Commit 16ee717

Browse files
committed
Parallelize walking directories for finding binaries
1 parent ea325bd commit 16ee717

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ clap = { version = "4.5", features = ["cargo", "derive", "deprecated", "wrap_hel
4343
crossbeam-channel = "0.5"
4444
flate2 = "1.1"
4545
globset = "0.4"
46+
ignore = "0.4.23"
4647
infer = "0.19.0"
4748
lazy_static = "1.5"
4849
log = "0.4"

src/llvm_tools.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crossbeam_channel::unbounded;
12
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
23
use std::env;
34
use std::env::consts::EXE_SUFFIX;
@@ -9,8 +10,9 @@ use std::path::{Path, PathBuf};
910
use std::process::{Command, Stdio};
1011
use std::sync::OnceLock;
1112

13+
use ignore::WalkBuilder;
14+
use ignore::WalkState::Continue;
1215
use log::warn;
13-
use walkdir::WalkDir;
1416

1517
pub static LLVM_PATH: OnceLock<PathBuf> = OnceLock::new();
1618

@@ -74,28 +76,38 @@ pub fn find_binaries(binary_path: &Path) -> Vec<PathBuf> {
7476
vec![binary_path.to_owned()]
7577
} else {
7678
let mut paths = vec![];
77-
let mut bytes = vec![0u8; 128];
7879

79-
for entry in WalkDir::new(binary_path).follow_links(true) {
80-
let entry = entry
81-
.unwrap_or_else(|e| panic!("Failed to open directory '{:?}': {}", binary_path, e));
80+
let (sender, receiver) = unbounded();
81+
let walker = WalkBuilder::new(binary_path)
82+
.threads(num_cpus::get() - 1)
83+
.build_parallel();
84+
walker.run(|| {
85+
let sender = sender.clone();
86+
let mut bytes = vec![0u8; 128];
8287

83-
if !entry.file_type().is_file() {
84-
continue;
85-
}
88+
Box::new(move |result| {
89+
let entry = result.unwrap();
8690

87-
let size = entry.metadata().unwrap().len();
88-
if size == 0 {
89-
continue;
90-
}
91+
if !entry.file_type().unwrap().is_file() {
92+
return Continue;
93+
}
9194

92-
let file = fs::File::open(entry.path()).unwrap();
95+
let file = fs::File::open(entry.path()).unwrap();
96+
let read = file.take(128).read(&mut bytes).unwrap();
97+
if read == 0 {
98+
return Continue;
99+
}
93100

94-
file.take(128).read_exact(&mut bytes).unwrap();
101+
if infer::is_app(&bytes) {
102+
sender.send(entry.into_path()).unwrap();
103+
}
95104

96-
if infer::is_app(&bytes) {
97-
paths.push(entry.into_path());
98-
}
105+
Continue
106+
})
107+
});
108+
109+
while let Ok(path) = receiver.try_recv() {
110+
paths.push(path);
99111
}
100112

101113
paths

0 commit comments

Comments
 (0)