Skip to content

Commit

Permalink
Support for file metadata to approximate typical tool usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 1, 2021
1 parent 9c66ef6 commit a6afcfe
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions benches/walk_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,73 @@ fn checkout_linux_if_needed() {

#[derive(Default)]
struct FsTree {
path: PathBuf,
children: Vec<FsTree>,
metadata: Option<std::fs::Metadata>,
}

impl FsTree {
fn recursive_descent(root: impl AsRef<Path>, file_type: Option<std::fs::FileType>) -> Self {
fn recursive_descent(
root: impl AsRef<Path>,
file_type: Option<std::fs::FileType>,
get_file_metadata: bool,
) -> Self {
let root = root.as_ref();
let is_dir = file_type
.map(|ft| ft.is_dir())
let (metadata, is_dir) = file_type
.map(|ft| {
(
if !ft.is_dir() && get_file_metadata {
std::fs::symlink_metadata(root).ok()
} else {
None
},
ft.is_dir(),
)
})
.or_else(|| {
std::fs::symlink_metadata(root)
.map(|m| m.file_type().is_dir())
.map(|m| {
let is_dir = m.file_type().is_dir();
(Some(m), is_dir)
})
.ok()
})
.unwrap_or(false);
.unwrap_or((None, false));

let children: Vec<_> = if is_dir {
std::fs::read_dir(root)
.map(|iter| {
let entries = iter.filter_map(Result::ok).collect::<Vec<_>>();
entries
iter.filter_map(Result::ok)
.collect::<Vec<_>>()
.into_par_iter()
.map(|entry| {
FsTree::recursive_descent(entry.path(), entry.file_type().ok())
FsTree::recursive_descent(
entry.path(),
entry.file_type().ok(),
get_file_metadata,
)
})
.collect()
})
.unwrap_or_default()
} else {
Vec::new()
};
FsTree {
children,
path: root.to_owned(),
}
FsTree { children, metadata }
}
}

fn walk_benches(c: &mut Criterion) {
checkout_linux_if_needed();

c.bench_function("simple recursive (unsorted, n threads)", |b| {
b.iter(|| black_box(FsTree::recursive_descent(linux_dir(), None)))
});
c.bench_function(
"simple recursive NO file metadata (unsorted, n threads)",
|b| b.iter(|| black_box(FsTree::recursive_descent(linux_dir(), None, false))),
);

c.bench_function(
"simple recursive WITH file metadata (unsorted, n threads)",
|b| b.iter(|| black_box(FsTree::recursive_descent(linux_dir(), None, true))),
);

c.bench_function("jwalk (unsorted, n threads)", |b| {
b.iter(|| for _ in WalkDir::new(linux_dir()) {})
Expand Down

0 comments on commit a6afcfe

Please sign in to comment.