|
| 1 | +#[cfg(unix)] |
| 2 | +use std::os::unix::ffi::OsStrExt; |
| 3 | +use std::{ |
| 4 | + fmt, |
| 5 | + hash::{Hash, Hasher}, |
| 6 | + ops::Deref, |
| 7 | + path::{Path, PathBuf}, |
| 8 | + sync::Arc, |
| 9 | +}; |
| 10 | + |
| 11 | +use rustc_hash::FxHasher; |
| 12 | + |
| 13 | +/// A path returned in [`crate::ResolveContext`] dependencies, paired with a |
| 14 | +/// precomputed `FxHash` of the path bytes. |
| 15 | +/// |
| 16 | +/// Downstream consumers (rspack) place these into hash collections keyed by |
| 17 | +/// the precomputed hash, avoiding repeated hashing of long absolute paths on |
| 18 | +/// every insert and lookup. |
| 19 | +/// |
| 20 | +/// Hash and equality are kept aligned per platform so the standard |
| 21 | +/// `a == b ⇒ hash(a) == hash(b)` contract holds: |
| 22 | +/// - On Unix, both hash and equality compare the raw `OsStr` bytes (fast bulk |
| 23 | +/// `write`; matches the resolver's cache-side `Cache::value` hash). |
| 24 | +/// - On other platforms, both go through `Path` (component-walked hash and |
| 25 | +/// normalized equality), so e.g. `pack1/foo` and `pack1\\foo` are treated as |
| 26 | +/// the same dependency on Windows — same behavior `PathBuf` had before. |
| 27 | +#[derive(Clone)] |
| 28 | +pub struct ResolverPath { |
| 29 | + hash: u64, |
| 30 | + path: Arc<Path>, |
| 31 | +} |
| 32 | + |
| 33 | +impl ResolverPath { |
| 34 | + pub fn new(path: Arc<Path>) -> Self { |
| 35 | + let hash = hash_path(&path); |
| 36 | + Self { hash, path } |
| 37 | + } |
| 38 | + |
| 39 | + /// Construct without recomputing the hash. |
| 40 | + /// |
| 41 | + /// # Precondition |
| 42 | + /// `hash` MUST equal `hash_path(path)`. Violating this breaks `HashSet`'s |
| 43 | + /// bucketing invariant — entries become unfindable and deduplication stops |
| 44 | + /// working. Not `unsafe` because the failure mode is a logic bug rather |
| 45 | + /// than UB. |
| 46 | + #[inline] |
| 47 | + pub(crate) fn from_parts(hash: u64, path: Arc<Path>) -> Self { |
| 48 | + Self { hash, path } |
| 49 | + } |
| 50 | + |
| 51 | + #[inline] |
| 52 | + pub fn as_path(&self) -> &Path { |
| 53 | + &self.path |
| 54 | + } |
| 55 | + |
| 56 | + #[inline] |
| 57 | + pub fn as_arc(&self) -> &Arc<Path> { |
| 58 | + &self.path |
| 59 | + } |
| 60 | + |
| 61 | + #[inline] |
| 62 | + pub fn into_arc(self) -> Arc<Path> { |
| 63 | + self.path |
| 64 | + } |
| 65 | + |
| 66 | + /// The precomputed `FxHash` of the path bytes. |
| 67 | + #[inline] |
| 68 | + pub fn precomputed_hash(&self) -> u64 { |
| 69 | + self.hash |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Hash a path with `FxHasher`, matching the bytes-on-unix optimization used by |
| 74 | +/// the resolver's internal cache so [`ResolverPath`] values constructed from a |
| 75 | +/// `CachedPath` produce the same `u64` as values constructed from a `&Path`. |
| 76 | +#[inline] |
| 77 | +pub fn hash_path(path: &Path) -> u64 { |
| 78 | + let mut hasher = FxHasher::default(); |
| 79 | + // The std `Path::hash` impl walks components (utf8 split + per-segment |
| 80 | + // write); a single bulk `write` of the raw bytes is materially cheaper on |
| 81 | + // the resolver hot path. |
| 82 | + #[cfg(unix)] |
| 83 | + hasher.write(path.as_os_str().as_bytes()); |
| 84 | + #[cfg(not(unix))] |
| 85 | + path.hash(&mut hasher); |
| 86 | + hasher.finish() |
| 87 | +} |
| 88 | + |
| 89 | +impl Hash for ResolverPath { |
| 90 | + #[inline] |
| 91 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 92 | + state.write_u64(self.hash); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl PartialEq for ResolverPath { |
| 97 | + /// Mirror `hash_path`'s per-platform scheme so the `a == b ⇒ hash(a) == |
| 98 | + /// hash(b)` invariant holds: raw `OsStr` bytes on Unix (matches the |
| 99 | + /// bulk-byte hash), component-normalized `Path::eq` elsewhere (matches |
| 100 | + /// `Path::hash`). |
| 101 | + fn eq(&self, other: &Self) -> bool { |
| 102 | + #[cfg(unix)] |
| 103 | + { |
| 104 | + self.path.as_os_str() == other.path.as_os_str() |
| 105 | + } |
| 106 | + #[cfg(not(unix))] |
| 107 | + { |
| 108 | + self.path == other.path |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl Eq for ResolverPath {} |
| 114 | + |
| 115 | +impl Deref for ResolverPath { |
| 116 | + type Target = Path; |
| 117 | + |
| 118 | + fn deref(&self) -> &Self::Target { |
| 119 | + &self.path |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl AsRef<Path> for ResolverPath { |
| 124 | + fn as_ref(&self) -> &Path { |
| 125 | + &self.path |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl From<PathBuf> for ResolverPath { |
| 130 | + fn from(path: PathBuf) -> Self { |
| 131 | + Self::new(Arc::from(path)) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl From<&Path> for ResolverPath { |
| 136 | + fn from(path: &Path) -> Self { |
| 137 | + Self::new(Arc::from(path)) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl From<&PathBuf> for ResolverPath { |
| 142 | + fn from(path: &PathBuf) -> Self { |
| 143 | + Self::new(Arc::from(path.as_path())) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl From<Arc<Path>> for ResolverPath { |
| 148 | + fn from(path: Arc<Path>) -> Self { |
| 149 | + Self::new(path) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl fmt::Debug for ResolverPath { |
| 154 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 155 | + self.path.fmt(f) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +#[cfg(test)] |
| 160 | +mod tests { |
| 161 | + use super::*; |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn hash_is_path_byte_hash() { |
| 165 | + let p: &Path = Path::new("/a/b/c.js"); |
| 166 | + let rp = ResolverPath::from(p); |
| 167 | + assert_eq!(rp.precomputed_hash(), hash_path(p)); |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + fn equal_paths_have_equal_hashes() { |
| 172 | + let a = ResolverPath::from(PathBuf::from("/x/y")); |
| 173 | + let b = ResolverPath::from(Path::new("/x/y")); |
| 174 | + assert_eq!(a, b); |
| 175 | + assert_eq!(a.precomputed_hash(), b.precomputed_hash()); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn writes_u64_into_hasher() { |
| 180 | + use std::{collections::HashSet, hash::BuildHasherDefault}; |
| 181 | + |
| 182 | + #[derive(Default)] |
| 183 | + struct IdHasher(u64); |
| 184 | + impl Hasher for IdHasher { |
| 185 | + fn write(&mut self, _: &[u8]) { |
| 186 | + unreachable!() |
| 187 | + } |
| 188 | + fn write_u64(&mut self, n: u64) { |
| 189 | + self.0 = n; |
| 190 | + } |
| 191 | + fn finish(&self) -> u64 { |
| 192 | + self.0 |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + let mut set: HashSet<ResolverPath, BuildHasherDefault<IdHasher>> = HashSet::default(); |
| 197 | + set.insert(ResolverPath::from(Path::new("/a/b"))); |
| 198 | + assert!(set.contains(&ResolverPath::from(PathBuf::from("/a/b")))); |
| 199 | + } |
| 200 | +} |
0 commit comments