Skip to content

ls: parallelize with rayon #7990

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ uucore = { workspace = true, features = [
"version-cmp",
] }
uutils_term_grid = { workspace = true }
rayon = { workspace = true }

[[bin]]
name = "ls"
Expand Down
85 changes: 71 additions & 14 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
mod colors;
use colors::{StyleManager, color_name};

use rayon::iter::Either;
use rayon::prelude::*;

#[cfg(not(feature = "selinux"))]
static CONTEXT_HELP_TEXT: &str = "print any security context of each file (not enabled)";
#[cfg(feature = "selinux")]
Expand Down Expand Up @@ -2005,6 +2008,44 @@
}
}

fn get_metadata_no_flush(&self) -> Option<&Metadata> {
self.md
.get_or_init(|| {
// check if we can use DirEntry metadata
// it will avoid a call to stat()
if !self.must_dereference {
if let Some(dir_entry) = &self.de {
return dir_entry.metadata().ok();
}

Check warning on line 2019 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2019

Added line #L2019 was not covered by tests
}

// if not, check if we can use Path metadata
match get_metadata_with_deref_opt(self.p_buf.as_path(), self.must_dereference) {
Err(err) => {
// FIXME: A bit tricky to propagate the result here
let errno = err.raw_os_error().unwrap_or(1i32);
// a bad fd will throw an error when dereferenced,
// but GNU will not throw an error until a bad fd "dir"
// is entered, here we match that GNU behavior, by handing
// back the non-dereferenced metadata upon an EBADF
if self.must_dereference && errno == 9i32 {
if let Some(dir_entry) = &self.de {
return dir_entry.metadata().ok();
}

Check warning on line 2034 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2032-L2034

Added lines #L2032 - L2034 were not covered by tests
}
show!(LsError::IOErrorContext(
self.p_buf.clone(),
err,
self.command_line
));
None
}
Ok(md) => Some(md),
}
})
.as_ref()
}

fn get_metadata(&self, out: &mut BufWriter<Stdout>) -> Option<&Metadata> {
self.md
.get_or_init(|| {
Expand Down Expand Up @@ -2349,22 +2390,38 @@
vec![]
};

// Convert those entries to the PathData struct
for raw_entry in read_dir {
let dir_entry = match raw_entry {
Ok(path) => path,
Err(err) => {
state.out.flush()?;
show!(LsError::IOError(err));
continue;
// read_dir is still an iterator. Collect it first.
let raw_entries: Vec<_> = read_dir.collect();

let (new_entries, errors): (Vec<_>, Vec<_>) =
raw_entries.into_par_iter().partition_map(|raw_entry| {
match raw_entry {
Ok(dir_entry) => {
if should_display(&dir_entry, config) {
let entry_path_data = PathData::new(
dir_entry.path(),
Some(Ok(dir_entry)),
None,
config,
false,
);
entry_path_data.get_metadata_no_flush();
Either::Left(entry_path_data)
} else {
// No error, just filtered out
Either::Right(None)
}
}
// Real IO error
Err(err) => Either::Right(Some(err)),

Check warning on line 2416 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2416

Added line #L2416 was not covered by tests
}
};
});

if should_display(&dir_entry, config) {
let entry_path_data =
PathData::new(dir_entry.path(), Some(Ok(dir_entry)), None, config, false);
entries.push(entry_path_data);
};
entries.extend(new_entries);

for error in errors.into_iter().flatten() {
state.out.flush()?;
show!(LsError::IOError(error));

Check warning on line 2424 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2423-L2424

Added lines #L2423 - L2424 were not covered by tests
}

sort_entries(&mut entries, config, &mut state.out);
Expand Down
Loading