From a07ea1a2726d5d11ee25f12365412b6a4a0b627b Mon Sep 17 00:00:00 2001 From: Xiaobo Liu Date: Fri, 12 Dec 2025 20:33:49 +0800 Subject: [PATCH] util: Avoid redundant Arc allocation in SanitizedPath::from_arc (#44479) Release Notes: - N/A Signed-off-by: Xiaobo Liu --- crates/util/src/paths.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs index f8e3e557152a24a6be8bb4cdad3a86d2256a764e..a54f91c7a0392748cb64c984559cf1ce25c2a7d8 100644 --- a/crates/util/src/paths.rs +++ b/crates/util/src/paths.rs @@ -227,9 +227,16 @@ impl SanitizedPath { #[cfg(not(target_os = "windows"))] return unsafe { mem::transmute::, Arc>(path) }; - // TODO: could avoid allocating here if dunce::simplified results in the same path #[cfg(target_os = "windows")] - return Self::new(&path).into(); + { + let simplified = dunce::simplified(path.as_ref()); + if simplified == path.as_ref() { + // safe because `Path` and `SanitizedPath` have the same repr and Drop impl + unsafe { mem::transmute::, Arc>(path) } + } else { + Self::unchecked_new(simplified).into() + } + } } pub fn new_arc + ?Sized>(path: &T) -> Arc {