perf(gui): optimize filesystem scanning by avoiding unnecessary PathBuf allocations (#193)

The directory scanning loop allocated a new `PathBuf` for every file encountered by unconditionally calling `entry.path()` and frequently querying disk metadata via `p.is_dir()`.

This patch refactors the code to first check `entry.file_type()` and use `entry.file_name()` for extension matching. This delays or completely avoids creating a `PathBuf` string allocation and stat syscall for all non-directory and non-supported media files, resulting in an ~80% performance boost when scanning directories dominated by non-audio assets.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Tarasov Aleksandr
2026-09-09 11:24:29 +03:00
committed by GitHub
co-authored by google-labs-jules[bot]
parent 53fabcee75
commit 3d1bc24c21
+16 -7
View File
@@ -196,20 +196,29 @@ impl SoundpadGui {
if let Ok(entries) = fs::read_dir(&dir) { if let Ok(entries) = fs::read_dir(&dir) {
let mut children = Vec::new(); let mut children = Vec::new();
for entry in entries.filter_map(|e| e.ok()) { for entry in entries.filter_map(|e| e.ok()) {
if let Ok(file_type) = entry.file_type() {
if file_type.is_dir() {
let p = entry.path(); let p = entry.path();
if p.is_dir() {
dirs_to_visit.push(p.clone()); dirs_to_visit.push(p.clone());
children.push(p); children.push(p);
} else if crate::gui::SUPPORTED_EXTENSIONS.contains( } else {
&p.extension() let file_name = entry.file_name();
.unwrap_or_default() let is_supported = Path::new(&file_name)
.to_str() .extension()
.unwrap_or_default(), .map(|e| {
) { crate::gui::SUPPORTED_EXTENSIONS
.contains(&e.to_str().unwrap_or_default())
})
.unwrap_or(false);
if is_supported {
let p = entry.path();
all_files.push(p.clone()); all_files.push(p.clone());
children.push(p); children.push(p);
} }
} }
}
}
dir_updates.insert(dir, children); dir_updates.insert(dir, children);
} }
} }