Skip to content

Commit 1814963

Browse files
committed
feat: faster and stable path hash for the cache
closes #322
1 parent 7255f9b commit 1814963

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/cache.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@ impl<Fs: FileSystem> Cache<Fs> {
4242
}
4343

4444
pub fn value(&self, path: &Path) -> CachedPath {
45+
// `Path::hash` is slow: https://doc.rust-lang.org/std/path/struct.Path.html#impl-Hash-for-Path
46+
// `path.as_os_str()` hash is not stable because we may joined a path like `foo/bar` and `foo\\bar` on windows.
4547
let hash = {
4648
let mut hasher = FxHasher::default();
47-
path.as_os_str().hash(&mut hasher);
49+
for b in path
50+
.as_os_str()
51+
.as_encoded_bytes()
52+
.iter()
53+
.rev()
54+
.filter(|&&b| b != b'/' && b != b'\\')
55+
.take(10)
56+
{
57+
b.hash(&mut hasher);
58+
}
4859
hasher.finish()
4960
};
5061
if let Some(cache_entry) = self.paths.get((hash, path).borrow() as &dyn CacheKey) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy