]> git.lizzy.rs Git - rust.git/commitdiff
Add special case for length 1
authorJakub Beránek <berykubik@gmail.com>
Mon, 13 Dec 2021 20:34:54 +0000 (21:34 +0100)
committerJakub Beránek <berykubik@gmail.com>
Mon, 13 Dec 2021 20:34:54 +0000 (21:34 +0100)
compiler/rustc_data_structures/src/stable_hasher.rs

index f37cf76c32bdeba28e96924afb5682fb92745f17..144eaed7e076e7af0363655221c1f7ce08d9f240 100644 (file)
@@ -559,20 +559,28 @@ fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
 fn stable_hash_reduce<HCX, I, C, F>(
     hcx: &mut HCX,
     hasher: &mut StableHasher,
-    collection: C,
+    mut collection: C,
     length: usize,
     hash_function: F,
 ) where
     C: Iterator<Item = I>,
     F: Fn(&mut StableHasher, &mut HCX, I),
 {
-    let hash = collection
-        .map(|value| {
-            let mut hasher = StableHasher::new();
-            hash_function(&mut hasher, hcx, value);
-            hasher.finish::<u128>()
-        })
-        .reduce(|accum, value| accum.wrapping_add(value));
     length.hash_stable(hcx, hasher);
-    hash.hash_stable(hcx, hasher);
+
+    match length {
+        1 => {
+            hash_function(hasher, hcx, collection.next().unwrap());
+        }
+        _ => {
+            let hash = collection
+                .map(|value| {
+                    let mut hasher = StableHasher::new();
+                    hash_function(&mut hasher, hcx, value);
+                    hasher.finish::<u128>()
+                })
+                .reduce(|accum, value| accum.wrapping_add(value));
+            hash.hash_stable(hcx, hasher);
+        }
+    }
 }