Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

util: Avoid redundant Arc allocation in SanitizedPath::from_arc (#44479)

Xiaobo Liua07ea1a2726d
diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs
index f8e3e557152..a54f91c7a03 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>, Arc<Self>>(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>, Arc<Self>>(path) }
+ } else {
+ Self::unchecked_new(simplified).into()
+ }
+ }
}
pub fn new_arc<T: AsRef<Path> + ?Sized>(path: &T) -> Arc<Self> {