perf: Canonicalize ignored absolute path only once

This commit is contained in:
Teemu Pätsi
2025-03-10 18:10:26 +02:00
committed by andy.boot
parent 28d409ea27
commit c31468b199
3 changed files with 18 additions and 9 deletions

View File

@@ -132,19 +132,14 @@ fn is_ignored_path(path: &Path, walk_data: &WalkData) -> bool {
}
// Entry is inside an ignored absolute path
// Absolute paths should be canonicalized before being added to `WalkData.ignore_directories`
for ignored_path in walk_data.ignore_directories.iter() {
if !ignored_path.is_absolute() {
continue;
}
match std::fs::canonicalize(ignored_path) {
Ok(absolute_ignored_path) => {
let absolute_entry_path =
std::fs::canonicalize(path).unwrap_or_default();
if absolute_entry_path.starts_with(absolute_ignored_path) {
return true;
}
}
Err(_) => continue,
let absolute_entry_path = std::fs::canonicalize(path).unwrap_or_default();
if absolute_entry_path.starts_with(ignored_path) {
return true;
}
}

View File

@@ -29,6 +29,7 @@ use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use sysinfo::{System, SystemExt};
use utils::canonicalize_absolute_path;
use self::display::draw_it;
use config::get_config;
@@ -198,6 +199,7 @@ fn main() {
Some(values) => values
.map(|v| v.as_str())
.map(PathBuf::from)
.map(canonicalize_absolute_path)
.collect::<Vec<PathBuf>>(),
None => vec![],
};

View File

@@ -67,6 +67,18 @@ pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
path.as_ref().components().collect()
}
// Canonicalize the path only if it is an absolute path
pub fn canonicalize_absolute_path(path: PathBuf) -> PathBuf {
if path.is_absolute() {
match std::fs::canonicalize(&path) {
Ok(canonicalized_path) => canonicalized_path,
Err(_) => path,
}
} else {
path
}
}
pub fn is_filtered_out_due_to_regex(filter_regex: &[Regex], dir: &Path) -> bool {
if filter_regex.is_empty() {
false