]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_data_structures/src/fingerprint.rs
Auto merge of #81358 - mcastorina:to-upper-lower-speed, r=joshtriplett
[rust.git] / compiler / rustc_data_structures / src / fingerprint.rs
index fecc51324cc677518bae18faa165d90d5926b389..681b49e2ea97bfaca49751da7d8ab1d79335ad1b 100644 (file)
@@ -103,7 +103,18 @@ impl<H: Hasher> FingerprintHasher for H {
 impl FingerprintHasher for crate::unhash::Unhasher {
     #[inline]
     fn write_fingerprint(&mut self, fingerprint: &Fingerprint) {
-        // `Unhasher` only wants a single `u64`
+        // Even though both halves of the fingerprint are expected to be good
+        // quality hash values, let's still combine the two values because the
+        // Fingerprints in DefPathHash have the StableCrateId portion which is
+        // the same for all DefPathHashes from the same crate. Combining the
+        // two halfs makes sure we get a good quality hash in such cases too.
+        //
+        // Since `Unhasher` is used only in the context of HashMaps, it is OK
+        // to combine the two components in an order-independent way (which is
+        // cheaper than the more robust Fingerprint::to_smaller_hash()). For
+        // HashMaps we don't really care if Fingerprint(x,y) and
+        // Fingerprint(y, x) result in the same hash value. Collision
+        // probability will still be much better than with FxHash.
         self.write_u64(fingerprint.0.wrapping_add(fingerprint.1));
     }
 }