From 5a89a94c0272d3faf77df83fac0e1a77a1418885 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 5 Apr 2026 09:46:53 +0000 Subject: [PATCH] perf: optimize linker path processing and validation - Refactored `for_each_nested_glob_match` to use a more efficient platform-agnostic path string construction, avoiding multiple allocations per file during discovery. - Eliminated redundant `Vec` allocations in `ensure_safe_destination` by iterating over path components directly. - Added performance comments to document optimizations and their expected impact. Co-authored-by: yacosta738 <33158051+yacosta738@users.noreply.github.com> --- src/linker.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/linker.rs b/src/linker.rs index 8df692f9..fb7f5744 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -165,15 +165,15 @@ impl Linker { anyhow::bail!("Destination path must be relative: {}", dest_path); } - let components: Vec<_> = path.components().collect(); - // SECURITY: Reject empty destinations like "" or "." that do not identify a concrete file. - if !components.iter().any(|c| matches!(c, Component::Normal(_))) { + // Optimization: iterate directly to avoid unnecessary Vec allocation. + if !path.components().any(|c| matches!(c, Component::Normal(_))) { anyhow::bail!("Destination path must not be empty: {}", dest_path); } // SECURITY: Reject traversal, root, and drive-prefixed components. - if components.iter().any(|c| { + // Optimization: iterate directly to avoid unnecessary Vec allocation. + if path.components().any(|c| { matches!( c, Component::ParentDir | Component::RootDir | Component::Prefix(_) @@ -845,11 +845,15 @@ impl Linker { Err(_) => continue, }; - let rel_str = rel_path - .components() - .map(|component| component.as_os_str().to_string_lossy().into_owned()) - .collect::>() - .join("/"); + // Performance: generating a platform-agnostic relative path for glob matching. + // On Unix, this is zero-allocation (Cow::Borrowed). + // On Windows, it performs exactly one allocation for the backslash replacement. + let rel_os_str = rel_path.to_string_lossy(); + let rel_str = if std::path::MAIN_SEPARATOR == '/' { + rel_os_str + } else { + std::borrow::Cow::Owned(rel_os_str.replace(std::path::MAIN_SEPARATOR, "/")) + }; if rel_str.is_empty() { continue;