Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
cache def-path hashes across all items
This seems like approx a 2x win on syntex_syntax.
  • Loading branch information
nikomatsakis committed Aug 23, 2016
1 parent 484da37 commit f923083
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/librustc_incremental/calculate_svh/mod.rs
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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,
}

Expand All @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions src/librustc_incremental/calculate_svh/svh_visitor.rs
Expand Up @@ -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)
})
}
}

Expand Down

0 comments on commit f923083

Please sign in to comment.