Skip to content

Commit d4d8bdf

Browse files
Add documentation to Unhasher impl for Fingerprint.
1 parent 6a4878f commit d4d8bdf

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,18 @@ impl<H: Hasher> FingerprintHasher for H {
103103
impl FingerprintHasher for crate::unhash::Unhasher {
104104
#[inline]
105105
fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
106-
// `Unhasher` only wants a single `u64`
106+
// Even though both halves of the fingerprint are expected to be good
107+
// quality hash values, let's still combine the two values because the
108+
// Fingerprints in DefPathHash have the StableCrateId portion which is
109+
// the same for all DefPathHashes from the same crate. Combining the
110+
// two halfs makes sure we get a good quality hash in such cases too.
111+
//
112+
// Since `Unhasher` is used only in the context of HashMaps, it is OK
113+
// to combine the two components in an order-independent way (which is
114+
// cheaper than the more robust Fingerprint::to_smaller_hash()). For
115+
// HashMaps we don't really care if Fingerprint(x,y) and
116+
// Fingerprint(y, x) result in the same hash value. Collision
117+
// probability will still be much better than with FxHash.
107118
self.write_u64(fingerprint.0.wrapping_add(fingerprint.1));
108119
}
109120
}

0 commit comments

Comments
 (0)