Skip to content

Commit

Permalink
Avoid sorting the item_ids array the StableHash impl of hir::Mod.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Jul 3, 2018
1 parent 79d8d08 commit 447f1f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rustc_data_structures::indexed_vec::{IndexVec};
use rustc_data_structures::stable_hasher::StableHasher;
use serialize::{Encodable, Decodable, Encoder, Decoder};
use session::CrateDisambiguator;
use std::borrow::Borrow;
use std::fmt::Write;
use std::hash::Hash;
use syntax::ast;
Expand Down Expand Up @@ -389,6 +390,13 @@ pub struct DefPathHash(pub Fingerprint);

impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });

impl Borrow<Fingerprint> for DefPathHash {
#[inline]
fn borrow(&self) -> &Fingerprint {
&self.0
}
}

impl Definitions {
/// Create new empty definition map.
pub fn new() -> Definitions {
Expand Down
12 changes: 12 additions & 0 deletions src/librustc/ich/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ impl Fingerprint {
)
}

// Combines two hashes in an order independent way. Make sure this is what
// you want.
#[inline]
pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
let a = (self.1 as u128) << 64 | self.0 as u128;
let b = (other.1 as u128) << 64 | other.0 as u128;

let c = a.wrapping_add(b);

Fingerprint((c >> 64) as u64, c as u64)
}

pub fn to_hex(&self) -> String {
format!("{:x}{:x}", self.0, self.1)
}
Expand Down
26 changes: 16 additions & 10 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use hir;
use hir::map::DefPathHash;
use hir::def_id::{DefId, LocalDefId, CrateNum, CRATE_DEF_INDEX};
use ich::{StableHashingContext, NodeIdHashingMode};
use ich::{StableHashingContext, NodeIdHashingMode, Fingerprint};
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
StableHasher, StableHasherResult};
use std::mem;
Expand Down Expand Up @@ -767,15 +767,21 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Mod {

inner_span.hash_stable(hcx, hasher);

let mut item_ids: Vec<DefPathHash> = item_ids.iter().map(|id| {
let (def_path_hash, local_id) = id.id.to_stable_hash_key(hcx);
debug_assert_eq!(local_id, hir::ItemLocalId(0));
def_path_hash
}).collect();

item_ids.sort_unstable();

item_ids.hash_stable(hcx, hasher);
// Combining the DefPathHashes directly is faster than feeding them
// into the hasher. Because we use a commutative combine, we also don't
// have to sort the array.
let item_ids_hash = item_ids
.iter()
.map(|id| {
let (def_path_hash, local_id) = id.id.to_stable_hash_key(hcx);
debug_assert_eq!(local_id, hir::ItemLocalId(0));
def_path_hash.0
}).fold(Fingerprint::ZERO, |a, b| {
a.combine_commutative(b)
});

item_ids.len().hash_stable(hcx, hasher);
item_ids_hash.hash_stable(hcx, hasher);
}
}

Expand Down

0 comments on commit 447f1f3

Please sign in to comment.