Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage parallel capabilities #142

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ regex = "1.5"
once_cell = "1.9"
onig = "6.3"
uucore = { version = "0.0.12", features = ["entries", "fs", "fsext"] }
rayon = "1.5"

[dev-dependencies]
assert_cmd = "2"
Expand Down
2 changes: 1 addition & 1 deletion src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> MatcherIO<'a> {
/// is what's being searched for. To a first order approximation, find consists
/// of building a chain of Matcher objects, and then walking a directory tree,
/// passing each entry to the chain of Matchers.
pub trait Matcher {
pub trait Matcher: Sync {
/// Returns whether the given file matches the object's predicate.
fn matches(&self, file_info: &DirEntry, matcher_io: &mut MatcherIO) -> bool;

Expand Down
29 changes: 19 additions & 10 deletions src/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

pub mod matchers;

use rayon::prelude::*;
use std::cell::RefCell;
use std::error::Error;
use std::io::{stderr, stdout, Write};
Expand Down Expand Up @@ -39,7 +40,7 @@ impl Default for Config {

/// Trait that encapsulates various dependencies (output, clocks, etc.) that we
/// might want to fake out for unit tests.
pub trait Dependencies<'a> {
pub trait Dependencies<'a>: Sync {
fn get_output(&'a self) -> &'a RefCell<dyn Write>;
fn now(&'a self) -> SystemTime;
}
Expand All @@ -50,6 +51,8 @@ pub struct StandardDependencies {
now: SystemTime,
}

unsafe impl Sync for StandardDependencies {}

impl StandardDependencies {
pub fn new() -> StandardDependencies {
StandardDependencies {
Expand Down Expand Up @@ -155,15 +158,19 @@ fn do_find<'a>(args: &[&str], deps: &'a dyn Dependencies<'a>) -> Result<u64, Box
return Ok(0);
}

let mut found_count: u64 = 0;
for path in paths_and_matcher.paths {
found_count += process_dir(
&path,
&paths_and_matcher.config,
deps,
&*paths_and_matcher.matcher,
);
}
let found_count: u64 = paths_and_matcher
.paths
.par_iter()
.map(|path| {
process_dir(
path,
&paths_and_matcher.config,
deps,
&*paths_and_matcher.matcher,
)
})
.sum();

Ok(found_count)
}

Expand Down Expand Up @@ -271,6 +278,8 @@ mod tests {
now: SystemTime,
}

unsafe impl Sync for FakeDependencies {}

impl<'a> FakeDependencies {
pub fn new() -> FakeDependencies {
FakeDependencies {
Expand Down
2 changes: 2 additions & 0 deletions tests/common/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct FakeDependencies {
now: SystemTime,
}

unsafe impl Sync for FakeDependencies {}

impl<'a> FakeDependencies {
pub fn new() -> FakeDependencies {
FakeDependencies {
Expand Down