Skip to content

Commit ac08f13

Browse files
committed
Remove sort from hashing hashset, treeset and treemap
1 parent 6e33d3e commit ac08f13

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,17 @@ impl_stable_hash_via_hash!(::std::path::PathBuf);
503503

504504
impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
505505
where
506-
K: HashStable<HCX> + ToStableHashKey<HCX> + Eq,
506+
K: ToStableHashKey<HCX> + Eq,
507507
V: HashStable<HCX>,
508508
R: BuildHasher,
509509
{
510510
#[inline]
511511
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
512-
hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key);
512+
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| {
513+
let key = key.to_stable_hash_key(hcx);
514+
key.hash_stable(hcx, hasher);
515+
value.hash_stable(hcx, hasher);
516+
});
513517
}
514518
}
515519

@@ -519,9 +523,10 @@ where
519523
R: BuildHasher,
520524
{
521525
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
522-
let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect();
523-
keys.sort_unstable();
524-
keys.hash_stable(hcx, hasher);
526+
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| {
527+
let key = key.to_stable_hash_key(hcx);
528+
key.hash_stable(hcx, hasher);
529+
});
525530
}
526531
}
527532

@@ -531,10 +536,11 @@ where
531536
V: HashStable<HCX>,
532537
{
533538
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
534-
let mut entries: Vec<_> =
535-
self.iter().map(|(k, v)| (k.to_stable_hash_key(hcx), v)).collect();
536-
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
537-
entries.hash_stable(hcx, hasher);
539+
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| {
540+
let key = key.to_stable_hash_key(hcx);
541+
key.hash_stable(hcx, hasher);
542+
value.hash_stable(hcx, hasher);
543+
});
538544
}
539545
}
540546

@@ -543,34 +549,30 @@ where
543549
K: ToStableHashKey<HCX>,
544550
{
545551
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
546-
let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect();
547-
keys.sort_unstable();
548-
keys.hash_stable(hcx, hasher);
552+
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| {
553+
let key = key.to_stable_hash_key(hcx);
554+
key.hash_stable(hcx, hasher);
555+
});
549556
}
550557
}
551558

552-
pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
559+
fn stable_hash_reduce<HCX, I, C, F>(
553560
hcx: &mut HCX,
554561
hasher: &mut StableHasher,
555-
map: &::std::collections::HashMap<K, V, R>,
556-
to_stable_hash_key: F,
562+
collection: C,
563+
length: usize,
564+
hash_function: F,
557565
) where
558-
K: Eq + HashStable<HCX>,
559-
V: HashStable<HCX>,
560-
R: BuildHasher,
561-
SK: HashStable<HCX> + Ord,
562-
F: Fn(&K, &HCX) -> SK,
566+
C: Iterator<Item = I>,
567+
F: Fn(&mut StableHasher, &mut HCX, I),
563568
{
564-
let hash = map
565-
.iter()
566-
.map(|(key, value)| {
567-
let key = to_stable_hash_key(key, hcx);
569+
let hash = collection
570+
.map(|value| {
568571
let mut hasher = StableHasher::new();
569-
key.hash_stable(hcx, &mut hasher);
570-
value.hash_stable(hcx, &mut hasher);
572+
hash_function(&mut hasher, hcx, value);
571573
hasher.finish::<u128>()
572574
})
573575
.reduce(|accum, value| accum.wrapping_add(value));
574-
map.len().hash_stable(hcx, hasher);
576+
length.hash_stable(hcx, hasher);
575577
hash.hash_stable(hcx, hasher);
576578
}

0 commit comments

Comments
 (0)