Skip to content

Commit f854f8b

Browse files
Build DefPathHash->DefId table when incr.comp. is enabled
1 parent 59ebe8e commit f854f8b

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/librustc/middle/cstore.rs

Lines changed: 6 additions & 1 deletion
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;
@@ -282,6 +283,7 @@ pub trait CrateStore {
282283
fn def_key(&self, def: DefId) -> DefKey;
283284
fn def_path(&self, def: DefId) -> hir_map::DefPath;
284285
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;
@@ -415,6 +417,9 @@ impl CrateStore for DummyCrateStore {
415417
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash {
416418
bug!("def_path_hash")
417419
}
420+
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable> {
421+
bug!("def_path_table")
422+
}
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") }
420425
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }

src/librustc/ty/context.rs

Lines changed: 40 additions & 1 deletion
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;
@@ -448,6 +448,10 @@ pub struct GlobalCtxt<'tcx> {
448448

449449
pub hir: hir_map::Map<'tcx>,
450450

451+
/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
452+
/// as well as all upstream crates. Only populated in incremental mode.
453+
pub def_path_hash_to_def_id: Option<FxHashMap<DefPathHash, DefId>>,
454+
451455
pub maps: maps::Maps<'tcx>,
452456

453457
pub mir_passes: Rc<Passes>,
@@ -676,6 +680,40 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
676680
let max_cnum = s.cstore.crates().iter().map(|c| c.as_usize()).max().unwrap_or(0);
677681
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
678682
providers[LOCAL_CRATE] = local_providers;
683+
684+
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
685+
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
686+
.cstore
687+
.crates()
688+
.iter()
689+
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
690+
.collect();
691+
692+
let def_path_tables = || {
693+
upstream_def_path_tables
694+
.iter()
695+
.map(|&(cnum, ref rc)| (cnum, &**rc))
696+
.chain(iter::once((LOCAL_CRATE, hir.definitions().def_path_table())))
697+
};
698+
699+
// Precompute the capacity of the hashmap so we don't have to
700+
// re-allocate when populating it.
701+
let capacity = def_path_tables().map(|(_, t)| t.size()).sum::<usize>();
702+
703+
let mut map: FxHashMap<_, _> = FxHashMap::with_capacity_and_hasher(
704+
capacity,
705+
::std::default::Default::default()
706+
);
707+
708+
for (cnum, def_path_table) in def_path_tables() {
709+
def_path_table.add_def_path_hashes_to(cnum, &mut map);
710+
}
711+
712+
Some(map)
713+
} else {
714+
None
715+
};
716+
679717
tls::enter_global(GlobalCtxt {
680718
sess: s,
681719
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
@@ -689,6 +727,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
689727
export_map: resolutions.export_map,
690728
fulfilled_predicates: RefCell::new(fulfilled_predicates),
691729
hir: hir,
730+
def_path_hash_to_def_id: def_path_hash_to_def_id,
692731
maps: maps::Maps::new(providers),
693732
mir_passes,
694733
freevars: RefCell::new(resolutions.freevars),

src/librustc_metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl<'a> CrateLoader<'a> {
326326
let mut cmeta = cstore::CrateMetadata {
327327
name: name,
328328
extern_crate: Cell::new(None),
329-
def_path_table: def_path_table,
329+
def_path_table: Rc::new(def_path_table),
330330
exported_symbols: exported_symbols,
331331
trait_impls: trait_impls,
332332
proc_macros: crate_root.macro_derive_registrar.map(|_| {

src/librustc_metadata/cstore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct CrateMetadata {
7676
/// hashmap, which gives the reverse mapping. This allows us to
7777
/// quickly retrace a `DefPath`, which is needed for incremental
7878
/// compilation support.
79-
pub def_path_table: DefPathTable,
79+
pub def_path_table: Rc<DefPathTable>,
8080

8181
pub exported_symbols: Tracked<FxHashSet<DefIndex>>,
8282

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE
2525

2626
use rustc::dep_graph::{DepNode, GlobalMetaDataKind};
2727
use rustc::hir::map::{DefKey, DefPath, DisambiguatedDefPathData, DefPathHash};
28+
use rustc::hir::map::definitions::DefPathTable;
2829
use rustc::util::nodemap::{NodeSet, DefIdMap};
2930
use rustc_back::PanicStrategy;
3031

@@ -337,6 +338,10 @@ impl CrateStore for cstore::CStore {
337338
self.get_crate_data(def.krate).def_path_hash(def.index)
338339
}
339340

341+
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable> {
342+
self.get_crate_data(cnum).def_path_table.clone()
343+
}
344+
340345
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>
341346
{
342347
self.dep_graph.read(DepNode::MetaData(def));

0 commit comments

Comments
 (0)