Skip to content

Commit

Permalink
incr.comp.: Use StableHash impls instead of functions for hashing mos…
Browse files Browse the repository at this point in the history
…t maps.
  • Loading branch information
michaelwoerister committed Sep 18, 2017
1 parent b9816c5 commit 67c84e0
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 247 deletions.
108 changes: 1 addition & 107 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ use hir::map::DefPathHash;
use ich::{self, CachingCodemapView};
use session::config::DebugInfoLevel::NoDebugInfo;
use ty::{self, TyCtxt, fast_reject};
use util::nodemap::{NodeMap, NodeSet, ItemLocalMap};

use std::cmp::Ord;
use std::hash as std_hash;
use std::collections::{HashMap, HashSet, BTreeMap};
use std::collections::HashMap;

use syntax::ast;
use syntax::attr;
Expand Down Expand Up @@ -337,111 +336,6 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
}
}

pub fn hash_stable_hashmap<'a, 'gcx, 'tcx, K, V, R, SK, F, W>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
map: &HashMap<K, V, R>,
extract_stable_key: F)
where K: Eq + std_hash::Hash,
V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
R: std_hash::BuildHasher,
SK: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + Ord + Clone,
F: Fn(&mut StableHashingContext<'a, 'gcx, 'tcx>, &K) -> SK,
W: StableHasherResult,
{
let mut keys: Vec<_> = map.keys()
.map(|k| (extract_stable_key(hcx, k), k))
.collect();
keys.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
keys.len().hash_stable(hcx, hasher);
for (stable_key, key) in keys {
stable_key.hash_stable(hcx, hasher);
map[key].hash_stable(hcx, hasher);
}
}

pub fn hash_stable_hashset<'a, 'tcx, 'gcx, K, R, SK, F, W>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
set: &HashSet<K, R>,
extract_stable_key: F)
where K: Eq + std_hash::Hash,
R: std_hash::BuildHasher,
SK: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + Ord + Clone,
F: Fn(&mut StableHashingContext<'a, 'gcx, 'tcx>, &K) -> SK,
W: StableHasherResult,
{
let mut keys: Vec<_> = set.iter()
.map(|k| extract_stable_key(hcx, k))
.collect();
keys.sort_unstable();
keys.hash_stable(hcx, hasher);
}

pub fn hash_stable_nodemap<'a, 'tcx, 'gcx, V, W>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
map: &NodeMap<V>)
where V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
W: StableHasherResult,
{
let definitions = hcx.tcx.hir.definitions();
hash_stable_hashmap(hcx, hasher, map, |_, node_id| {
let hir_id = definitions.node_to_hir_id(*node_id);
let owner_def_path_hash = definitions.def_path_hash(hir_id.owner);
(owner_def_path_hash, hir_id.local_id)
});
}

pub fn hash_stable_nodeset<'a, 'tcx, 'gcx, W>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
map: &NodeSet)
where W: StableHasherResult,
{
let definitions = hcx.tcx.hir.definitions();
hash_stable_hashset(hcx, hasher, map, |_, node_id| {
let hir_id = definitions.node_to_hir_id(*node_id);
let owner_def_path_hash = definitions.def_path_hash(hir_id.owner);
(owner_def_path_hash, hir_id.local_id)
});
}

pub fn hash_stable_itemlocalmap<'a, 'tcx, 'gcx, V, W>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
map: &ItemLocalMap<V>)
where V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
W: StableHasherResult,
{
hash_stable_hashmap(hcx, hasher, map, |_, local_id| {
*local_id
});
}


pub fn hash_stable_btreemap<'a, 'tcx, 'gcx, K, V, SK, F, W>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
map: &BTreeMap<K, V>,
extract_stable_key: F)
where K: Eq + Ord,
V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
SK: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + Ord + Clone,
F: Fn(&mut StableHashingContext<'a, 'gcx, 'tcx>, &K) -> SK,
W: StableHasherResult,
{
let mut keys: Vec<_> = map.keys()
.map(|k| (extract_stable_key(hcx, k), k))
.collect();
keys.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
keys.len().hash_stable(hcx, hasher);
for (stable_key, key) in keys {
stable_key.hash_stable(hcx, hasher);
map[key].hash_stable(hcx, hasher);
}
}

pub fn hash_stable_trait_impls<'a, 'tcx, 'gcx, W, R>(
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>,
Expand Down
83 changes: 27 additions & 56 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! This module contains `HashStable` implementations for various data types
//! from rustc::ty in no particular order.

use ich::{self, StableHashingContext, NodeIdHashingMode};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHasherResult};
use ich::{StableHashingContext, NodeIdHashingMode};
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
StableHasher, StableHasherResult};
use std::hash as std_hash;
use std::mem;
use middle::region;
Expand Down Expand Up @@ -124,9 +124,10 @@ for ty::adjustment::Adjust<'gcx> {

impl_stable_hash_for!(struct ty::adjustment::Adjustment<'tcx> { kind, target });
impl_stable_hash_for!(struct ty::adjustment::OverloadedDeref<'tcx> { region, mutbl });
impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
impl_stable_hash_for!(struct ty::UpvarBorrow<'tcx> { kind, region });

impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });

impl_stable_hash_for!(enum ty::BorrowKind {
ImmBorrow,
UniqueImmBorrow,
Expand Down Expand Up @@ -513,28 +514,20 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
FnPtrAddrCast
});

impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for region::Scope
{
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
match *self {
region::Scope::Node(node_id) |
region::Scope::Destruction(node_id) => {
node_id.hash_stable(hcx, hasher);
}
region::Scope::CallSite(body_id) |
region::Scope::Arguments(body_id) => {
body_id.hash_stable(hcx, hasher);
}
region::Scope::Remainder(block_remainder) => {
block_remainder.hash_stable(hcx, hasher);
}
}
})
impl_stable_hash_for!(enum ::middle::region::Scope {
Node(local_id),
Destruction(local_id),
CallSite(local_id),
Arguments(local_id),
Remainder(block_remainder)
});

impl<'a, 'gcx, 'tcx> ToStableHashKey<StableHashingContext<'a, 'gcx, 'tcx>> for region::Scope {
type KeyType = region::Scope;

#[inline]
fn to_stable_hash_key(&self, _: &StableHashingContext<'a, 'gcx, 'tcx>) -> region::Scope {
*self
}
}

Expand Down Expand Up @@ -770,10 +763,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::Cr
} = *self;

dependencies.hash_stable(hcx, hasher);

ich::hash_stable_hashmap(hcx, hasher, variances, |hcx, def_id| {
hcx.def_path_hash(*def_id)
});
variances.hash_stable(hcx, hasher);
}
}

Expand Down Expand Up @@ -836,25 +826,14 @@ for ::middle::privacy::AccessLevels {
ref map
} = *self;

ich::hash_stable_nodemap(hcx, hasher, map);
map.hash_stable(hcx, hasher);
});
}
}

impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for ty::CrateInherentImpls {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
let ty::CrateInherentImpls {
ref inherent_impls,
} = *self;

ich::hash_stable_hashmap(hcx, hasher, inherent_impls, |hcx, def_id| {
hcx.def_path_hash(*def_id)
});
}
}
impl_stable_hash_for!(struct ty::CrateInherentImpls {
inherent_impls
});

impl_stable_hash_for!(enum ::session::CompileIncomplete {
Stopped,
Expand All @@ -863,14 +842,6 @@ impl_stable_hash_for!(enum ::session::CompileIncomplete {

impl_stable_hash_for!(struct ::util::common::ErrorReported {});

impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for ::middle::reachable::ReachableSet {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
let ::middle::reachable::ReachableSet(ref reachable_set) = *self;

ich::hash_stable_nodeset(hcx, hasher, reachable_set);
}
}

impl_stable_hash_for!(tuple_struct ::middle::reachable::ReachableSet {
reachable_set
});
4 changes: 1 addition & 3 deletions src/librustc/ich/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

pub use self::fingerprint::Fingerprint;
pub use self::caching_codemap_view::CachingCodemapView;
pub use self::hcx::{StableHashingContext, NodeIdHashingMode, hash_stable_hashmap,
hash_stable_hashset, hash_stable_nodemap, hash_stable_nodeset,
hash_stable_btreemap, hash_stable_itemlocalmap,
pub use self::hcx::{StableHashingContext, NodeIdHashingMode,
hash_stable_trait_impls};
mod fingerprint;
mod caching_codemap_view;
Expand Down
31 changes: 16 additions & 15 deletions src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use std::cmp;

use errors::DiagnosticBuilder;
use hir::HirId;
use ich::{self, StableHashingContext};
use ich::StableHashingContext;
use lint::builtin;
use lint::context::CheckLintNameResult;
use lint::{self, Lint, LintId, Level, LintSource};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
StableHasherResult};
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
StableHasher, StableHasherResult};
use session::Session;
use syntax::ast;
use syntax::attr;
Expand Down Expand Up @@ -396,10 +396,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintLe
ref id_to_set,
} = *self;

let definitions = hcx.tcx().hir.definitions();
ich::hash_stable_hashmap(hcx, hasher, id_to_set, |_, hir_id| {
(definitions.def_path_hash(hir_id.owner), hir_id.local_id)
});
id_to_set.hash_stable(hcx, hasher);

let LintLevelSets {
ref list,
Expand All @@ -418,14 +415,10 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintLe

match *lint_set {
LintSet::CommandLine { ref specs } => {
ich::hash_stable_hashmap(hcx, hasher, specs, |_, lint_id| {
lint_id.lint_name_raw()
});
specs.hash_stable(hcx, hasher);
}
LintSet::Node { ref specs, parent } => {
ich::hash_stable_hashmap(hcx, hasher, specs, |_, lint_id| {
lint_id.lint_name_raw()
});
specs.hash_stable(hcx, hasher);
parent.hash_stable(hcx, hasher);
}
}
Expand All @@ -434,12 +427,20 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintLe
}
}

impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintId {
impl<HCX> HashStable<HCX> for LintId {
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hcx: &mut HCX,
hasher: &mut StableHasher<W>) {
self.lint_name_raw().hash_stable(hcx, hasher);
}
}

impl<HCX> ToStableHashKey<HCX> for LintId {
type KeyType = &'static str;

#[inline]
fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
self.lint_name_raw()
}
}
25 changes: 7 additions & 18 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! Most of the documentation on regions can be found in
//! `middle/infer/region_inference/README.md`

use ich::{self, StableHashingContext, NodeIdHashingMode};
use ich::{StableHashingContext, NodeIdHashingMode};
use util::nodemap::{FxHashMap, FxHashSet};
use ty;

Expand Down Expand Up @@ -1259,22 +1259,11 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ScopeT
root_parent.hash_stable(hcx, hasher);
});

ich::hash_stable_hashmap(hcx, hasher, parent_map, |hcx, scope| {
let mut hasher = StableHasher::new();
scope.hash_stable(hcx, &mut hasher);
let stable: u64 = hasher.finish();
stable
});

ich::hash_stable_itemlocalmap(hcx, hasher, var_map);
ich::hash_stable_itemlocalmap(hcx, hasher, destruction_scopes);
ich::hash_stable_itemlocalmap(hcx, hasher, rvalue_scopes);
ich::hash_stable_itemlocalmap(hcx, hasher, closure_tree);
ich::hash_stable_hashmap(hcx, hasher, yield_in_scope, |hcx, scope| {
let mut hasher = StableHasher::new();
scope.hash_stable(hcx, &mut hasher);
let stable: u64 = hasher.finish();
stable
});
parent_map.hash_stable(hcx, hasher);
var_map.hash_stable(hcx, hasher);
destruction_scopes.hash_stable(hcx, hasher);
rvalue_scopes.hash_stable(hcx, hasher);
closure_tree.hash_stable(hcx, hasher);
yield_in_scope.hash_stable(hcx, hasher);
}
}
21 changes: 4 additions & 17 deletions src/librustc/traits/specialize/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Childr
}
}

impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Graph {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
let Graph {
ref parent,
ref children,
} = *self;

ich::hash_stable_hashmap(hcx, hasher, parent, |hcx, def_id| {
hcx.def_path_hash(*def_id)
});
ich::hash_stable_hashmap(hcx, hasher, children, |hcx, def_id| {
hcx.def_path_hash(*def_id)
});
}
}
impl_stable_hash_for!(struct self::Graph {
parent,
children
});
Loading

0 comments on commit 67c84e0

Please sign in to comment.