Skip to content

Commit

Permalink
working on more generic "work tree"
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegrosjean committed Feb 1, 2019
1 parent 6b0df4a commit ebf9789
Show file tree
Hide file tree
Showing 14 changed files with 1,013 additions and 173 deletions.
7 changes: 4 additions & 3 deletions benches/walk_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn checkout_linux_if_needed() {
fn walk_benches(c: &mut Criterion) {
checkout_linux_if_needed();

c.bench_function("walkdir::WalkDir", move |b| {
/*c.bench_function("walkdir::WalkDir", move |b| {
b.iter(|| for _ in walkdir::WalkDir::new(linux_dir()) {})
});
Expand All @@ -56,12 +56,13 @@ fn walk_benches(c: &mut Criterion) {
.build_parallel()
.run(move || Box::new(move |_| ignore::WalkState::Continue));
})
});
});*/

c.bench_function("jwalk::WalkDir", |b| {
b.iter(|| for _ in WalkDir::new(linux_dir()) {})
});

/*
c.bench_function("jwalk::WalkDir_1", |b| {
b.iter(|| for _ in WalkDir::new(linux_dir()).into_iter().take(1) {})
});
Expand All @@ -86,7 +87,7 @@ fn walk_benches(c: &mut Criterion) {
for _each_entry in each_dir_contents.contents.iter() {}
}
})
});
});*/
}

criterion_group! {
Expand Down
99 changes: 99 additions & 0 deletions src/core/delegate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use rayon::prelude::*;
use std::fs::{self, FileType};
use std::io::{Error, Result};
use std::path::{Path, PathBuf};

//use super::WorkContext;
use crate::core::DirEntry;

pub struct WorkContext<D>
where
D: Delegate,
{
items: Vec<WorkContextItem<D>>,
}

enum WorkContextItem<D>
where
D: Delegate,
{
Item(D::Item),
Work(D::Work),
}

impl<D> WorkContext<D>
where
D: Delegate,
{
fn send_item(&mut self, item: D::Item) {
self.items.push(WorkContextItem::Item(item));
}
fn schedule_work(&mut self, work: D::Work) {
self.items.push(WorkContextItem::Work(work));
}
}

pub trait Delegate: Clone + Send {
type State; // don't need... include state in "work"
type Item;
type Work;

fn process_work(&self, work: Self::Work, context: &mut WorkContext<Self>) -> Result<()>;

fn handle_error(&self, path: &Path, error: &Error) -> bool;
fn process_entries(
&self,
path: &Path,
state: Self::State,
entries: Vec<DirEntry>,
) -> (Self::State, Vec<DirEntry>);
}

#[derive(Clone)]
pub struct DefaultDelegate {}

impl Delegate for DefaultDelegate {
type State = usize;
type Item = PathBuf;
type Work = PathBuf;

fn process_work(&self, work: Self::Work, context: &mut WorkContext<Self>) -> Result<()> {
fs::read_dir(&work)?.for_each(|entry_result| {
let entry = match entry_result {
Ok(entry) => entry,
Err(_) => {
return;
}
};

let file_type = match entry.file_type() {
Ok(file_type) => file_type,
Err(_) => {
return;
}
};

context.send_item(entry.path());

if file_type.is_dir() {
context.schedule_work(entry.path());
}
});

Ok(())
}

fn handle_error(&self, path: &Path, error: &Error) -> bool {
eprintln!("{} {}", path.display(), error);
true
}
fn process_entries(
&self,
_path: &Path,
state: Self::State,
mut entries: Vec<DirEntry>,
) -> (Self::State, Vec<DirEntry>) {
entries.par_sort_by(|a, b| a.file_name().cmp(b.file_name()));
(state, entries)
}
}
Loading

0 comments on commit ebf9789

Please sign in to comment.