From 778bf2d255cf2b632cb9d4f32c0f1f157b359951 Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 11 Dec 2024 20:46:05 +0800 Subject: [PATCH] perf: different `PartialEq` --- src/cache.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 173b66c7..6637f57c 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -138,9 +138,30 @@ impl Hash for CachedPath { } } +fn partial_eq(a: &Path, b: &Path) -> bool { + let a = a.as_os_str().as_encoded_bytes(); + let b = b.as_os_str().as_encoded_bytes(); + let len = a.len(); + if len != b.len() { + return false; + } + for i in (0..len).rev() { + let x = a[i]; + let y = b[i]; + if (x == b'/' || x == b'\\') && (y == b'/' || y == b'\\') { + continue; + } + if x == y { + continue; + } + return false; + } + true +} + impl PartialEq for CachedPath { fn eq(&self, other: &Self) -> bool { - self.0.path == other.0.path + partial_eq(self.path(), other.path()) } } impl Eq for CachedPath {} @@ -404,7 +425,7 @@ impl Hash for dyn CacheKey + '_ { impl PartialEq for dyn CacheKey + '_ { fn eq(&self, other: &Self) -> bool { - self.tuple().1 == other.tuple().1 + partial_eq(self.tuple().1, other.tuple().1) } }