Skip to content

Commit

Permalink
perf: reuse filetype from DirEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Apr 15, 2024
1 parent 07f6770 commit 8ddefc9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
19 changes: 10 additions & 9 deletions src/fs/dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::fs::feature::git::GitCache;
use crate::fs::fields::GitStatus;
use std::fs;
use std::fs::DirEntry;
use std::io;
use std::path::{Path, PathBuf};
use std::slice::Iter as SliceIter;
Expand All @@ -17,7 +18,7 @@ use crate::fs::File;
/// accordingly. (See `File#get_source_files`)
pub struct Dir {
/// A vector of the files that have been read from this directory.
contents: Vec<PathBuf>,
contents: Vec<DirEntry>,

/// The path that was read.
pub path: PathBuf,
Expand All @@ -35,9 +36,7 @@ impl Dir {
pub fn read_dir(path: PathBuf) -> io::Result<Self> {
info!("Reading directory {:?}", &path);

let contents = fs::read_dir(&path)?
.map(|result| result.map(|entry| entry.path()))
.collect::<Result<_, _>>()?;
let contents = fs::read_dir(&path)?.collect::<Result<Vec<_>, _>>()?;

info!("Read directory success {:?}", &path);
Ok(Self { contents, path })
Expand Down Expand Up @@ -67,7 +66,7 @@ impl Dir {

/// Whether this directory contains a file with the given path.
pub fn contains(&self, path: &Path) -> bool {
self.contents.iter().any(|p| p.as_path() == path)
self.contents.iter().any(|p| p.path().as_path() == path)
}

/// Append a path onto the path specified by this directory.
Expand All @@ -80,7 +79,7 @@ impl Dir {
#[allow(clippy::struct_excessive_bools)]
pub struct Files<'dir, 'ig> {
/// The internal iterator over the paths that have been read already.
inner: SliceIter<'dir, PathBuf>,
inner: SliceIter<'dir, DirEntry>,

/// The directory that begat those paths.
dir: &'dir Dir,
Expand Down Expand Up @@ -117,8 +116,9 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
/// varies depending on the dotfile visibility flag)
fn next_visible_file(&mut self) -> Option<Result<File<'dir>, (PathBuf, io::Error)>> {
loop {
if let Some(path) = self.inner.next() {
let filename = File::filename(path);
if let Some(entry) = self.inner.next() {
let path = entry.path();
let filename = File::filename(&path);
if !self.dotfiles && filename.starts_with('.') {
continue;
}
Expand All @@ -131,7 +131,7 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
}

if self.git_ignoring {
let git_status = self.git.map(|g| g.get(path, false)).unwrap_or_default();
let git_status = self.git.map(|g| g.get(&path, false)).unwrap_or_default();
if git_status.unstaged == GitStatus::Ignored {
continue;
}
Expand All @@ -143,6 +143,7 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
filename,
self.deref_links,
self.total_size,
entry.file_type().ok(),
)
.map_err(|e| (path.clone(), e));

Expand Down
Loading

0 comments on commit 8ddefc9

Please sign in to comment.