Skip to content

Commit 3a184b2

Browse files
committed
perf: use std::fs::read_dir directly
1 parent 022dc7e commit 3a184b2

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

crates/artifacts/solc/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ regex.workspace = true
3131
tokio = { workspace = true, optional = true, features = ["fs"] }
3232
futures-util = { workspace = true, optional = true }
3333

34-
# walkdir
35-
walkdir = { workspace = true, optional = true }
36-
3734
# rayon
3835
rayon = { workspace = true, optional = true }
3936

@@ -48,5 +45,5 @@ foundry-compilers-core = { workspace = true, features = ["test-utils"] }
4845
[features]
4946
async = ["dep:tokio", "dep:futures-util"]
5047
checksum = ["foundry-compilers-core/hasher"]
51-
walkdir = ["dep:walkdir", "foundry-compilers-core/walkdir"]
48+
walkdir = ["foundry-compilers-core/walkdir"]
5249
rayon = ["dep:rayon"]

crates/artifacts/solc/src/remappings/find.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use foundry_compilers_core::utils;
33
use rayon::prelude::*;
44
use std::{
55
collections::{btree_map::Entry, BTreeMap, HashSet},
6+
fs::FileType,
67
path::{Path, PathBuf},
78
sync::Mutex,
89
};
@@ -84,11 +85,10 @@ impl Remapping {
8485

8586
// iterate over all dirs that are children of the root
8687
let candidates = read_dir(dir)
87-
.filter(|e| e.file_type().is_dir())
88+
.filter(|(_, file_type)| file_type.is_dir())
8889
.collect::<Vec<_>>()
8990
.par_iter()
90-
.flat_map_iter(|entry| {
91-
let dir = entry.path();
91+
.flat_map_iter(|(dir, _)| {
9292
find_remapping_candidates(
9393
dir,
9494
dir,
@@ -285,8 +285,8 @@ fn is_lib_dir(dir: &Path) -> bool {
285285
}
286286

287287
/// Returns true if the file is _hidden_
288-
fn is_hidden(entry: &walkdir::DirEntry) -> bool {
289-
entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
288+
fn is_hidden(path: &Path) -> bool {
289+
path.file_name().and_then(|p| p.to_str()).map(|s| s.starts_with('.')).unwrap_or(false)
290290
}
291291

292292
/// Finds all remappings in the directory recursively
@@ -307,14 +307,11 @@ fn find_remapping_candidates(
307307

308308
// scan all entries in the current dir
309309
let mut search = Vec::new();
310-
for entry in read_dir(current_dir) {
310+
for (subdir, file_type) in read_dir(current_dir) {
311311
// found a solidity file directly the current dir
312-
if !is_candidate
313-
&& entry.file_type().is_file()
314-
&& entry.path().extension() == Some("sol".as_ref())
315-
{
312+
if !is_candidate && file_type.is_file() && subdir.extension() == Some("sol".as_ref()) {
316313
is_candidate = true;
317-
} else if entry.file_type().is_dir() {
314+
} else if file_type.is_dir() {
318315
// if the dir is a symlink to a parent dir we short circuit here
319316
// `walkdir` will catch symlink loops, but this check prevents that we end up scanning a
320317
// workspace like
@@ -323,8 +320,8 @@ fn find_remapping_candidates(
323320
// ├── dep/node_modules
324321
// ├── symlink to `my-package`
325322
// ```
326-
if entry.path_is_symlink() {
327-
if let Ok(target) = utils::canonicalize(entry.path()) {
323+
if file_type.is_symlink() {
324+
if let Ok(target) = utils::canonicalize(&subdir) {
328325
if !visited_symlink_dirs.lock().unwrap().insert(target.clone()) {
329326
// short-circuiting if we've already visited the symlink
330327
return Vec::new();
@@ -339,10 +336,9 @@ fn find_remapping_candidates(
339336
}
340337
}
341338

342-
let subdir = entry.path();
343339
// we skip commonly used subdirs that should not be searched for recursively
344-
if !no_recurse(subdir) {
345-
search.push(subdir.to_path_buf());
340+
if !no_recurse(&subdir) {
341+
search.push(subdir);
346342
}
347343
}
348344
}
@@ -416,15 +412,13 @@ fn find_remapping_candidates(
416412
candidates
417413
}
418414

419-
fn read_dir(dir: &Path) -> impl Iterator<Item = walkdir::DirEntry> {
420-
walkdir::WalkDir::new(dir)
421-
.sort_by_file_name()
422-
.follow_links(true)
423-
.min_depth(1)
424-
.max_depth(1)
415+
fn read_dir(dir: &Path) -> impl Iterator<Item = (PathBuf, FileType)> {
416+
std::fs::read_dir(dir)
425417
.into_iter()
426-
.filter_entry(|e| !is_hidden(e))
418+
.flatten()
427419
.filter_map(Result::ok)
420+
.filter_map(|e| Some((e.path(), e.file_type().ok()?)))
421+
.filter(|(p, _)| !is_hidden(p))
428422
}
429423

430424
fn no_recurse(dir: &Path) -> bool {

0 commit comments

Comments
 (0)