cache def-path hashes across all items

This seems like approx a 2x win on syntex_syntax.
This commit is contained in:
Niko Matsakis 2016-08-23 07:56:34 -04:00
parent 484da37845
commit f923083308
2 changed files with 17 additions and 10 deletions

View File

@ -35,6 +35,7 @@ use rustc::hir;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use rustc::hir::intravisit as visit;
use rustc::ty::TyCtxt;
use rustc::util::nodemap::DefIdMap;
use rustc_data_structures::fnv::FnvHashMap;
use self::svh_visitor::StrictVersionHashVisitor;
@ -47,7 +48,9 @@ pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> IncrementalHashesMap {
let _ignore = tcx.dep_graph.in_ignore();
let krate = tcx.map.krate();
let mut visitor = HashItemsVisitor { tcx: tcx, hashes: FnvHashMap() };
let mut visitor = HashItemsVisitor { tcx: tcx,
hashes: FnvHashMap(),
def_path_hashes: DefIdMap() };
visitor.calculate_def_id(DefId::local(CRATE_DEF_INDEX), |v| visit::walk_crate(v, krate));
krate.visit_all_items(&mut visitor);
visitor.compute_crate_hash();
@ -56,6 +59,7 @@ pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
struct HashItemsVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_path_hashes: DefIdMap<u64>,
hashes: IncrementalHashesMap,
}
@ -75,7 +79,9 @@ impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
// FIXME: this should use SHA1, not SipHash. SipHash is not
// built to avoid collisions.
let mut state = SipHasher::new();
walk_op(&mut StrictVersionHashVisitor::new(&mut state, self.tcx));
walk_op(&mut StrictVersionHashVisitor::new(&mut state,
self.tcx,
&mut self.def_path_hashes));
let item_hash = state.finish();
self.hashes.insert(DepNode::Hir(def_id), item_hash);
debug!("calculate_item_hash: def_id={:?} hash={:?}", def_id, item_hash);

View File

@ -35,23 +35,24 @@ pub struct StrictVersionHashVisitor<'a, 'tcx: 'a> {
pub st: &'a mut SipHasher,
// collect a deterministic hash of def-ids that we have seen
def_id_hashes: DefIdMap<u64>,
def_path_hashes: &'a mut DefIdMap<u64>,
}
impl<'a, 'tcx> StrictVersionHashVisitor<'a, 'tcx> {
pub fn new(st: &'a mut SipHasher,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_path_hashes: &'a mut DefIdMap<u64>)
-> Self {
StrictVersionHashVisitor { st: st, tcx: tcx, def_id_hashes: DefIdMap() }
StrictVersionHashVisitor { st: st, tcx: tcx, def_path_hashes: def_path_hashes }
}
fn compute_def_id_hash(&mut self, def_id: DefId) -> u64 {
let tcx = self.tcx;
*self.def_id_hashes.entry(def_id)
.or_insert_with(|| {
let def_path = tcx.def_path(def_id);
def_path.deterministic_hash(tcx)
})
*self.def_path_hashes.entry(def_id)
.or_insert_with(|| {
let def_path = tcx.def_path(def_id);
def_path.deterministic_hash(tcx)
})
}
}