Skip to content

Commit 2f2d741

Browse files
committed
Auto merge of #42332 - michaelwoerister:no-more-retracing, r=nikomatsakis
incr.comp.: Use DefPathHash-based DepNodes in the serialized DepGraph and remove obsolete DefIdDirectory With this PR we don't store the dep-graph as a set of `DepNode<IndexIntoDefIdDirectory>` anymore but instead as a set of `DepNode<DefPathHash>`. Since a `DefPathHash` is a global identifier that is valid across compilation sessions, we don't need the `DefIdDirectory` anymore. Since a `DepNode<DefPathHash>` is bigger than a `DepNode<IndexIntoDefIdDirectory>` and our on-disk encoding of the dep-graph is inefficient, this PR will probably increase the amount of space the dep-graph takes up on disk. I'm in the process of gathering some performance data. The changes in here are a step towards implementing ICH-based `DepNodes` (#42294). r? @nikomatsakis
2 parents fbb9276 + a3417bf commit 2f2d741

File tree

19 files changed

+289
-350
lines changed

19 files changed

+289
-350
lines changed

src/librustc/hir/map/definitions.rs

+43-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use util::nodemap::NodeMap;
3636
pub struct DefPathTable {
3737
index_to_key: [Vec<DefKey>; 2],
3838
key_to_index: FxHashMap<DefKey, DefIndex>,
39-
def_path_hashes: [Vec<Fingerprint>; 2],
39+
def_path_hashes: [Vec<DefPathHash>; 2],
4040
}
4141

4242
// Unfortunately we have to provide a manual impl of Clone because of the
@@ -57,7 +57,7 @@ impl DefPathTable {
5757

5858
fn allocate(&mut self,
5959
key: DefKey,
60-
def_path_hash: Fingerprint,
60+
def_path_hash: DefPathHash,
6161
address_space: DefIndexAddressSpace)
6262
-> DefIndex {
6363
let index = {
@@ -81,7 +81,7 @@ impl DefPathTable {
8181
}
8282

8383
#[inline(always)]
84-
pub fn def_path_hash(&self, index: DefIndex) -> Fingerprint {
84+
pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
8585
self.def_path_hashes[index.address_space().index()]
8686
[index.as_array_index()]
8787
}
@@ -126,6 +126,30 @@ impl DefPathTable {
126126

127127
Some(index)
128128
}
129+
130+
pub fn add_def_path_hashes_to(&self,
131+
cnum: CrateNum,
132+
out: &mut FxHashMap<DefPathHash, DefId>) {
133+
for address_space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] {
134+
let start_index = address_space.start();
135+
out.extend(
136+
(&self.def_path_hashes[address_space.index()])
137+
.iter()
138+
.enumerate()
139+
.map(|(index, &hash)| {
140+
let def_id = DefId {
141+
krate: cnum,
142+
index: DefIndex::new(index + start_index),
143+
};
144+
(hash, def_id)
145+
})
146+
);
147+
}
148+
}
149+
150+
pub fn size(&self) -> usize {
151+
self.key_to_index.len()
152+
}
129153
}
130154

131155

@@ -148,8 +172,8 @@ impl Decodable for DefPathTable {
148172
let index_to_key_lo: Vec<DefKey> = Decodable::decode(d)?;
149173
let index_to_key_hi: Vec<DefKey> = Decodable::decode(d)?;
150174

151-
let def_path_hashes_lo: Vec<Fingerprint> = Decodable::decode(d)?;
152-
let def_path_hashes_hi: Vec<Fingerprint> = Decodable::decode(d)?;
175+
let def_path_hashes_lo: Vec<DefPathHash> = Decodable::decode(d)?;
176+
let def_path_hashes_hi: Vec<DefPathHash> = Decodable::decode(d)?;
153177

154178
let index_to_key = [index_to_key_lo, index_to_key_hi];
155179
let def_path_hashes = [def_path_hashes_lo, def_path_hashes_hi];
@@ -216,25 +240,25 @@ pub struct DefKey {
216240
}
217241

218242
impl DefKey {
219-
fn compute_stable_hash(&self, parent_hash: Fingerprint) -> Fingerprint {
243+
fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
220244
let mut hasher = StableHasher::new();
221245

222246
// We hash a 0u8 here to disambiguate between regular DefPath hashes,
223247
// and the special "root_parent" below.
224248
0u8.hash(&mut hasher);
225249
parent_hash.hash(&mut hasher);
226250
self.disambiguated_data.hash(&mut hasher);
227-
hasher.finish()
251+
DefPathHash(hasher.finish())
228252
}
229253

230-
fn root_parent_stable_hash(crate_name: &str, crate_disambiguator: &str) -> Fingerprint {
254+
fn root_parent_stable_hash(crate_name: &str, crate_disambiguator: &str) -> DefPathHash {
231255
let mut hasher = StableHasher::new();
232256
// Disambiguate this from a regular DefPath hash,
233257
// see compute_stable_hash() above.
234258
1u8.hash(&mut hasher);
235259
crate_name.hash(&mut hasher);
236260
crate_disambiguator.hash(&mut hasher);
237-
hasher.finish()
261+
DefPathHash(hasher.finish())
238262
}
239263
}
240264

@@ -296,7 +320,9 @@ impl DefPath {
296320

297321
s.push_str(&tcx.original_crate_name(self.krate).as_str());
298322
s.push_str("/");
299-
s.push_str(&tcx.crate_disambiguator(self.krate).as_str());
323+
// Don't print the whole crate disambiguator. That's just annoying in
324+
// debug output.
325+
s.push_str(&tcx.crate_disambiguator(self.krate).as_str()[..7]);
300326

301327
for component in &self.data {
302328
write!(s,
@@ -372,6 +398,12 @@ pub enum DefPathData {
372398
Typeof,
373399
}
374400

401+
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
402+
RustcEncodable, RustcDecodable)]
403+
pub struct DefPathHash(pub Fingerprint);
404+
405+
impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
406+
375407
impl Definitions {
376408
/// Create new empty definition map.
377409
pub fn new() -> Definitions {
@@ -404,7 +436,7 @@ impl Definitions {
404436
}
405437

406438
#[inline(always)]
407-
pub fn def_path_hash(&self, index: DefIndex) -> Fingerprint {
439+
pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
408440
self.table.def_path_hash(index)
409441
}
410442

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use self::MapEntry::*;
1313
use self::collector::NodeCollector;
1414
pub use self::def_collector::{DefCollector, MacroInvocationData};
1515
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
16-
DisambiguatedDefPathData};
16+
DisambiguatedDefPathData, DefPathHash};
1717

1818
use dep_graph::{DepGraph, DepNode};
1919

src/librustc/ich/hcx.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use hir;
1212
use hir::def_id::DefId;
13+
use hir::map::DefPathHash;
1314
use ich::{self, CachingCodemapView};
1415
use session::config::DebugInfoLevel::NoDebugInfo;
1516
use ty;
@@ -115,7 +116,7 @@ impl<'a, 'tcx: 'a> StableHashingContext<'a, 'tcx> {
115116
}
116117

117118
#[inline]
118-
pub fn def_path_hash(&mut self, def_id: DefId) -> ich::Fingerprint {
119+
pub fn def_path_hash(&mut self, def_id: DefId) -> DefPathHash {
119120
self.tcx.def_path_hash(def_id)
120121
}
121122

src/librustc/middle/cstore.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use hir::def;
2626
use dep_graph::DepNode;
2727
use hir::def_id::{CrateNum, DefId, DefIndex};
2828
use hir::map as hir_map;
29-
use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData};
29+
use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData,
30+
DefPathTable};
3031
use hir::svh::Svh;
3132
use ich;
3233
use middle::lang_items;
@@ -281,7 +282,8 @@ pub trait CrateStore {
281282
-> Option<DefId>;
282283
fn def_key(&self, def: DefId) -> DefKey;
283284
fn def_path(&self, def: DefId) -> hir_map::DefPath;
284-
fn def_path_hash(&self, def: DefId) -> ich::Fingerprint;
285+
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash;
286+
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable>;
285287
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
286288
fn item_children(&self, did: DefId) -> Vec<def::Export>;
287289
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
@@ -412,8 +414,11 @@ impl CrateStore for DummyCrateStore {
412414
fn def_path(&self, def: DefId) -> hir_map::DefPath {
413415
bug!("relative_def_path")
414416
}
415-
fn def_path_hash(&self, def: DefId) -> ich::Fingerprint {
416-
bug!("wa")
417+
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash {
418+
bug!("def_path_hash")
419+
}
420+
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable> {
421+
bug!("def_path_table")
417422
}
418423
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
419424
fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }

src/librustc/ty/context.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hir::TraitMap;
1818
use hir::def::{Def, ExportMap};
1919
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
2020
use hir::map as hir_map;
21-
use hir::map::DisambiguatedDefPathData;
21+
use hir::map::{DisambiguatedDefPathData, DefPathHash};
2222
use middle::free_region::FreeRegionMap;
2323
use middle::lang_items;
2424
use middle::resolve_lifetime;
@@ -461,6 +461,10 @@ pub struct GlobalCtxt<'tcx> {
461461

462462
pub hir: hir_map::Map<'tcx>,
463463

464+
/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
465+
/// as well as all upstream crates. Only populated in incremental mode.
466+
pub def_path_hash_to_def_id: Option<FxHashMap<DefPathHash, DefId>>,
467+
464468
pub maps: maps::Maps<'tcx>,
465469

466470
pub mir_passes: Rc<Passes>,
@@ -686,6 +690,40 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
686690
let max_cnum = s.cstore.crates().iter().map(|c| c.as_usize()).max().unwrap_or(0);
687691
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
688692
providers[LOCAL_CRATE] = local_providers;
693+
694+
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
695+
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
696+
.cstore
697+
.crates()
698+
.iter()
699+
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
700+
.collect();
701+
702+
let def_path_tables = || {
703+
upstream_def_path_tables
704+
.iter()
705+
.map(|&(cnum, ref rc)| (cnum, &**rc))
706+
.chain(iter::once((LOCAL_CRATE, hir.definitions().def_path_table())))
707+
};
708+
709+
// Precompute the capacity of the hashmap so we don't have to
710+
// re-allocate when populating it.
711+
let capacity = def_path_tables().map(|(_, t)| t.size()).sum::<usize>();
712+
713+
let mut map: FxHashMap<_, _> = FxHashMap::with_capacity_and_hasher(
714+
capacity,
715+
::std::default::Default::default()
716+
);
717+
718+
for (cnum, def_path_table) in def_path_tables() {
719+
def_path_table.add_def_path_hashes_to(cnum, &mut map);
720+
}
721+
722+
Some(map)
723+
} else {
724+
None
725+
};
726+
689727
tls::enter_global(GlobalCtxt {
690728
sess: s,
691729
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
@@ -699,6 +737,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
699737
export_map: resolutions.export_map,
700738
fulfilled_predicates: RefCell::new(fulfilled_predicates),
701739
hir: hir,
740+
def_path_hash_to_def_id: def_path_hash_to_def_id,
702741
maps: maps::Maps::new(providers),
703742
mir_passes,
704743
freevars: RefCell::new(resolutions.freevars),

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use dep_graph::DepNode;
1919
use hir::{map as hir_map, FreevarMap, TraitMap};
2020
use hir::def::{Def, CtorKind, ExportMap};
2121
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
22-
use ich::{self, StableHashingContext};
22+
use ich::StableHashingContext;
2323
use middle::const_val::ConstVal;
2424
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2525
use middle::privacy::AccessLevels;
@@ -2167,7 +2167,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21672167
}
21682168

21692169
#[inline]
2170-
pub fn def_path_hash(self, def_id: DefId) -> ich::Fingerprint {
2170+
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
21712171
if def_id.is_local() {
21722172
self.hir.definitions().def_path_hash(def_id.index)
21732173
} else {

src/librustc/ty/sty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! This module contains TypeVariants and its major components
1212
1313
use hir::def_id::DefId;
14+
use hir::map::DefPathHash;
1415

1516
use middle::region;
1617
use ty::subst::Substs;
@@ -29,7 +30,6 @@ use util::nodemap::FxHashMap;
2930
use serialize;
3031

3132
use hir;
32-
use ich;
3333

3434
use self::InferTy::*;
3535
use self::TypeVariants::*;
@@ -873,7 +873,7 @@ impl<'a, 'tcx, 'gcx> ExistentialProjection<'tcx> {
873873
self.item_name // safe to skip the binder to access a name
874874
}
875875

876-
pub fn sort_key(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> (ich::Fingerprint, InternedString) {
876+
pub fn sort_key(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> (DefPathHash, InternedString) {
877877
// We want something here that is stable across crate boundaries.
878878
// The DefId isn't but the `deterministic_hash` of the corresponding
879879
// DefPath is.
@@ -908,7 +908,7 @@ impl<'a, 'tcx, 'gcx> PolyExistentialProjection<'tcx> {
908908
self.skip_binder().item_name()
909909
}
910910

911-
pub fn sort_key(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> (ich::Fingerprint, InternedString) {
911+
pub fn sort_key(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> (DefPathHash, InternedString) {
912912
self.skip_binder().sort_key(tcx)
913913
}
914914

src/librustc/ty/trait_def.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use hir::def_id::DefId;
12-
use ich::Fingerprint;
12+
use hir::map::DefPathHash;
1313
use traits::specialization_graph;
1414
use ty::fast_reject;
1515
use ty::fold::TypeFoldable;
@@ -33,7 +33,7 @@ pub struct TraitDef {
3333

3434
/// The ICH of this trait's DefPath, cached here so it doesn't have to be
3535
/// recomputed all the time.
36-
pub def_path_hash: Fingerprint,
36+
pub def_path_hash: DefPathHash,
3737
}
3838

3939
// We don't store the list of impls in a flat list because each cached list of
@@ -95,7 +95,7 @@ impl<'a, 'gcx, 'tcx> TraitDef {
9595
unsafety: hir::Unsafety,
9696
paren_sugar: bool,
9797
has_default_impl: bool,
98-
def_path_hash: Fingerprint)
98+
def_path_hash: DefPathHash)
9999
-> TraitDef {
100100
TraitDef {
101101
def_id,

src/librustc_incremental/calculate_svh/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use std::hash::Hash;
3232
use rustc::dep_graph::DepNode;
3333
use rustc::hir;
3434
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
35+
use rustc::hir::map::DefPathHash;
3536
use rustc::hir::itemlikevisit::ItemLikeVisitor;
3637
use rustc::ich::{Fingerprint, StableHashingContext};
3738
use rustc::ty::TyCtxt;
@@ -218,7 +219,7 @@ impl<'a, 'tcx: 'a> ComputeItemHashesVisitor<'a, 'tcx> {
218219
{
219220
let tcx = self.hcx.tcx();
220221

221-
let mut impls: Vec<(Fingerprint, Fingerprint)> = krate
222+
let mut impls: Vec<(DefPathHash, Fingerprint)> = krate
222223
.trait_impls
223224
.iter()
224225
.map(|(&trait_id, impls)| {

0 commit comments

Comments
 (0)