Skip to content

Commit cd4fd03

Browse files
committed
refactor: Avoid unnecessary allocation
Use Cow::borrowed, if we can re-use the already absolute path.
1 parent fcd8466 commit cd4fd03

2 files changed

Lines changed: 50 additions & 51 deletions

File tree

src/filesystem.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
2020
env::current_dir().map(|path_buf| path_buf.join(path))
2121
}
2222

23-
/// Construct an absolute path from a potentially relative path and a
24-
/// pre-resolved working directory. Unlike `path_absolute_form`, this
25-
/// does not call `env::current_dir()` and cannot fail.
26-
pub fn make_absolute(path: &Path, cwd: &Path) -> PathBuf {
27-
if path.is_absolute() {
28-
return path.to_path_buf();
29-
}
30-
let path = path.strip_prefix(".").unwrap_or(path);
31-
cwd.join(path)
32-
}
33-
3423
pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
3524
let path_buf = path_absolute_form(path)?;
3625

@@ -164,40 +153,4 @@ mod tests {
164153
Path::new("foo/bar/baz")
165154
);
166155
}
167-
168-
#[test]
169-
fn make_absolute_with_relative_path() {
170-
use super::make_absolute;
171-
use std::path::PathBuf;
172-
173-
let cwd = Path::new("/home/user");
174-
assert_eq!(
175-
make_absolute(Path::new("foo/bar"), cwd),
176-
PathBuf::from("/home/user/foo/bar")
177-
);
178-
}
179-
180-
#[test]
181-
fn make_absolute_strips_dot_prefix() {
182-
use super::make_absolute;
183-
use std::path::PathBuf;
184-
185-
let cwd = Path::new("/home/user");
186-
assert_eq!(
187-
make_absolute(Path::new("./foo/bar"), cwd),
188-
PathBuf::from("/home/user/foo/bar")
189-
);
190-
}
191-
192-
#[test]
193-
fn make_absolute_with_absolute_path() {
194-
use super::make_absolute;
195-
use std::path::PathBuf;
196-
197-
let cwd = Path::new("/home/user");
198-
assert_eq!(
199-
make_absolute(Path::new("/absolute/path"), cwd),
200-
PathBuf::from("/absolute/path")
201-
);
202-
}
203156
}

src/walk.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,16 @@ impl WorkerState {
665665

666666
fn search_str_for_entry<'a>(
667667
entry_path: &'a std::path::Path,
668-
cwd: Option<&std::path::Path>,
668+
full_path_base: Option<&std::path::Path>,
669669
) -> Cow<'a, OsStr> {
670-
if let Some(cwd) = cwd {
671-
let abs_path = filesystem::make_absolute(entry_path, cwd);
672-
Cow::Owned(abs_path.into_os_string())
670+
if let Some(cwd) = full_path_base {
671+
// If full_path_base is some, that means that we need to return
672+
// the absolute path
673+
if entry_path.is_absolute() {
674+
return Cow::Borrowed(entry_path.as_os_str());
675+
}
676+
let path = entry_path.strip_prefix(".").unwrap_or(entry_path);
677+
Cow::Owned(cwd.join(path).into())
673678
} else {
674679
match entry_path.file_name() {
675680
Some(filename) => Cow::Borrowed(filename),
@@ -690,3 +695,44 @@ fn search_str_for_entry<'a>(
690695
pub fn scan(paths: &[PathBuf], patterns: Vec<Regex>, config: Config) -> Result<ExitCode> {
691696
WorkerState::new(patterns, config).scan(paths)
692697
}
698+
699+
#[cfg(test)]
700+
mod tests {
701+
use super::search_str_for_entry;
702+
use std::path::{Path, PathBuf};
703+
704+
#[test]
705+
fn search_str_for_entry_with_relative_path() {
706+
let full_path_base = Some(Path::new("/home/user"));
707+
assert_eq!(
708+
search_str_for_entry(Path::new("foo/bar"), full_path_base),
709+
PathBuf::from("/home/user/foo/bar")
710+
);
711+
}
712+
713+
#[test]
714+
fn search_str_for_entry_strips_dot_prefix() {
715+
let full_path_base = Some(Path::new("/home/user"));
716+
assert_eq!(
717+
search_str_for_entry(Path::new("./foo/bar"), full_path_base),
718+
PathBuf::from("/home/user/foo/bar")
719+
);
720+
}
721+
722+
#[test]
723+
fn search_str_for_entry_with_absolute_path() {
724+
let full_path_base = Some(Path::new("/home/user"));
725+
assert_eq!(
726+
search_str_for_entry(Path::new("/absolute/path"), full_path_base),
727+
PathBuf::from("/absolute/path")
728+
);
729+
}
730+
731+
#[test]
732+
fn search_str_no_base_dir() {
733+
assert_eq!(
734+
search_str_for_entry(Path::new("./foo/bar"), None),
735+
PathBuf::from("bar")
736+
);
737+
}
738+
}

0 commit comments

Comments
 (0)