Skip to content

Commit

Permalink
feat: faster and stable path hash for the cache
Browse files Browse the repository at this point in the history
closes #322
  • Loading branch information
Boshen committed Dec 5, 2024
1 parent 7255f9b commit 1814963
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ impl<Fs: FileSystem> Cache<Fs> {
}

pub fn value(&self, path: &Path) -> CachedPath {
// `Path::hash` is slow: https://doc.rust-lang.org/std/path/struct.Path.html#impl-Hash-for-Path
// `path.as_os_str()` hash is not stable because we may joined a path like `foo/bar` and `foo\\bar` on windows.
let hash = {
let mut hasher = FxHasher::default();
path.as_os_str().hash(&mut hasher);
for b in path
.as_os_str()
.as_encoded_bytes()
.iter()
.rev()
.filter(|&&b| b != b'/' && b != b'\\')
.take(10)
{
b.hash(&mut hasher);
}
hasher.finish()
};
if let Some(cache_entry) = self.paths.get((hash, path).borrow() as &dyn CacheKey) {
Expand Down

0 comments on commit 1814963

Please sign in to comment.