Skip to content

Commit ed5a88e

Browse files
definitions: Store DefPath data in separate table in metadata
1 parent aed0cdb commit ed5a88e

File tree

8 files changed

+41
-62
lines changed

8 files changed

+41
-62
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub enum DefPathData {
193193
// they are treated specially by the `def_path` function.
194194
/// The crate root (marker)
195195
CrateRoot,
196-
196+
197197
// Catch-all for random DefId things like DUMMY_NODE_ID
198198
Misc,
199199

@@ -243,6 +243,10 @@ impl Definitions {
243243
}
244244
}
245245

246+
pub fn def_path_table(&self) -> &DefPathTable {
247+
&self.table
248+
}
249+
246250
/// Get the number of definitions.
247251
pub fn len(&self) -> usize {
248252
self.def_index_to_node.len()

src/librustc/hir/map/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use hir::*;
2929
use hir::print as pprust;
3030

3131
use arena::TypedArena;
32-
use std::cell::RefCell;
32+
use std::cell::{RefCell, Ref};
3333
use std::io;
3434
use std::mem;
3535

@@ -395,6 +395,10 @@ impl<'ast> Map<'ast> {
395395
self.definitions.borrow().len()
396396
}
397397

398+
pub fn definitions(&self) -> Ref<Definitions> {
399+
self.definitions.borrow()
400+
}
401+
398402
pub fn def_key(&self, def_id: DefId) -> DefKey {
399403
assert!(def_id.is_local());
400404
self.definitions.borrow().def_key(def_id.index)

src/librustc_metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'a> CrateLoader<'a> {
300300
let mut cmeta = cstore::CrateMetadata {
301301
name: name,
302302
extern_crate: Cell::new(None),
303-
key_map: metadata.load_key_map(crate_root.index),
303+
def_path_table: crate_root.def_path_table.decode(&metadata),
304304
proc_macros: crate_root.macro_derive_registrar.map(|_| {
305305
self.load_derive_macros(&crate_root, dylib.clone().map(|p| p.0), span)
306306
}),

src/librustc_metadata/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use schema;
1616

1717
use rustc::dep_graph::DepGraph;
1818
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefIndex, DefId};
19-
use rustc::hir::map::DefKey;
19+
use rustc::hir::map::definitions::DefPathTable;
2020
use rustc::hir::svh::Svh;
2121
use rustc::middle::cstore::{DepKind, ExternCrate};
2222
use rustc_back::PanicStrategy;
@@ -78,7 +78,7 @@ pub struct CrateMetadata {
7878
/// hashmap, which gives the reverse mapping. This allows us to
7979
/// quickly retrace a `DefPath`, which is needed for incremental
8080
/// compilation support.
81-
pub key_map: FxHashMap<DefKey, DefIndex>,
81+
pub def_path_table: DefPathTable,
8282

8383
pub dep_kind: Cell<DepKind>,
8484
pub source: CrateSource,

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
341341
def: DefKey)
342342
-> Option<DefIndex> {
343343
let cdata = self.get_crate_data(cnum);
344-
cdata.key_map.get(&def).cloned()
344+
cdata.def_path_table.def_index_for_def_key(&def)
345345
}
346346

347347
/// Returns the `DefKey` for a given `DefId`. This indicates the

src/librustc_metadata/decoder.rs

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
use astencode::decode_inlined_item;
1414
use cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary};
15-
use index::Index;
1615
use schema::*;
1716

1817
use rustc::hir::map as hir_map;
1918
use rustc::hir::map::{DefKey, DefPathData};
20-
use rustc::util::nodemap::FxHashMap;
2119
use rustc::hir;
2220
use rustc::hir::intravisit::IdRange;
2321

@@ -456,14 +454,6 @@ impl<'a, 'tcx> MetadataBlob {
456454
Lazy::with_position(pos).decode(self)
457455
}
458456

459-
/// Go through each item in the metadata and create a map from that
460-
/// item's def-key to the item's DefIndex.
461-
pub fn load_key_map(&self, index: LazySeq<Index>) -> FxHashMap<DefKey, DefIndex> {
462-
index.iter_enumerated(self.raw_bytes())
463-
.map(|(index, item)| (item.decode(self).def_key.decode(self), index))
464-
.collect()
465-
}
466-
467457
pub fn list_crate_metadata(&self, out: &mut io::Write) -> io::Result<()> {
468458
write!(out, "=External Dependencies=\n")?;
469459
let root = self.get_root();
@@ -543,9 +533,8 @@ impl<'a, 'tcx> CrateMetadata {
543533
}
544534
}
545535

546-
fn item_name(&self, item: &Entry<'tcx>) -> ast::Name {
547-
item.def_key
548-
.decode(self)
536+
fn item_name(&self, item_index: DefIndex) -> ast::Name {
537+
self.def_key(item_index)
549538
.disambiguated_data
550539
.data
551540
.get_opt_name()
@@ -594,12 +583,12 @@ impl<'a, 'tcx> CrateMetadata {
594583

595584
(ty::VariantDef {
596585
did: self.local_def_id(data.struct_ctor.unwrap_or(index)),
597-
name: self.item_name(item),
586+
name: self.item_name(index),
598587
fields: item.children.decode(self).map(|index| {
599588
let f = self.entry(index);
600589
ty::FieldDef {
601590
did: self.local_def_id(index),
602-
name: self.item_name(&f),
591+
name: self.item_name(index),
603592
vis: f.visibility
604593
}
605594
}).collect(),
@@ -771,7 +760,7 @@ impl<'a, 'tcx> CrateMetadata {
771760
if let Some(def) = self.get_def(child_index) {
772761
callback(def::Export {
773762
def: def,
774-
name: self.item_name(&self.entry(child_index)),
763+
name: self.item_name(child_index),
775764
});
776765
}
777766
}
@@ -783,7 +772,7 @@ impl<'a, 'tcx> CrateMetadata {
783772
_ => {}
784773
}
785774

786-
let def_key = child.def_key.decode(self);
775+
let def_key = self.def_key(child_index);
787776
if let (Some(def), Some(name)) =
788777
(self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) {
789778
callback(def::Export {
@@ -886,7 +875,7 @@ impl<'a, 'tcx> CrateMetadata {
886875
pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> {
887876
let item = self.entry(id);
888877
let parent_and_name = || {
889-
let def_key = item.def_key.decode(self);
878+
let def_key = self.def_key(id);
890879
(self.local_def_id(def_key.parent.unwrap()),
891880
def_key.disambiguated_data.data.get_opt_name().unwrap())
892881
};
@@ -963,7 +952,7 @@ impl<'a, 'tcx> CrateMetadata {
963952
// we assume that someone passing in a tuple struct ctor is actually wanting to
964953
// look at the definition
965954
let mut item = self.entry(node_id);
966-
let def_key = item.def_key.decode(self);
955+
let def_key = self.def_key(node_id);
967956
if def_key.disambiguated_data.data == DefPathData::StructCtor {
968957
item = self.entry(def_key.parent.unwrap());
969958
}
@@ -974,7 +963,7 @@ impl<'a, 'tcx> CrateMetadata {
974963
self.entry(id)
975964
.children
976965
.decode(self)
977-
.map(|index| self.item_name(&self.entry(index)))
966+
.map(|index| self.item_name(index))
978967
.collect()
979968
}
980969

@@ -1036,7 +1025,7 @@ impl<'a, 'tcx> CrateMetadata {
10361025
}
10371026

10381027
pub fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {
1039-
self.entry(id).def_key.decode(self).parent.and_then(|parent_index| {
1028+
self.def_key(id).parent.and_then(|parent_index| {
10401029
match self.entry(parent_index).kind {
10411030
EntryKind::Trait(_) => Some(self.local_def_id(parent_index)),
10421031
_ => None,
@@ -1082,7 +1071,7 @@ impl<'a, 'tcx> CrateMetadata {
10821071
pub fn get_macro(&self, id: DefIndex) -> (ast::Name, MacroDef) {
10831072
let entry = self.entry(id);
10841073
match entry.kind {
1085-
EntryKind::MacroDef(macro_def) => (self.item_name(&entry), macro_def.decode(self)),
1074+
EntryKind::MacroDef(macro_def) => (self.item_name(id), macro_def.decode(self)),
10861075
_ => bug!(),
10871076
}
10881077
}
@@ -1135,20 +1124,8 @@ impl<'a, 'tcx> CrateMetadata {
11351124
}
11361125
}
11371126

1138-
pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey {
1139-
debug!("def_key: id={:?}", id);
1140-
if self.is_proc_macro(id) {
1141-
let name = self.proc_macros.as_ref().unwrap()[id.as_usize() - 1].0;
1142-
hir_map::DefKey {
1143-
parent: Some(CRATE_DEF_INDEX),
1144-
disambiguated_data: hir_map::DisambiguatedDefPathData {
1145-
data: hir_map::DefPathData::MacroDef(name.as_str()),
1146-
disambiguator: 0,
1147-
},
1148-
}
1149-
} else {
1150-
self.entry(id).def_key.decode(self)
1151-
}
1127+
pub fn def_key(&self, index: DefIndex) -> DefKey {
1128+
self.def_path_table.def_key(index)
11521129
}
11531130

11541131
// Returns the path leading to the thing with this `id`. Note that

src/librustc_metadata/encoder.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc::middle::cstore::{InlinedItemRef, LinkMeta};
1616
use rustc::middle::cstore::{LinkagePreference, NativeLibrary};
1717
use rustc::hir::def;
1818
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId};
19+
use rustc::hir::map::definitions::DefPathTable;
1920
use rustc::middle::dependency_format::Linkage;
2021
use rustc::middle::lang_items;
2122
use rustc::mir;
@@ -233,13 +234,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
233234
Ok(())
234235
}
235236

236-
/// For every DefId that we create a metadata item for, we include a
237-
/// serialized copy of its DefKey, which allows us to recreate a path.
238-
fn encode_def_key(&mut self, def_id: DefId) -> Lazy<hir::map::DefKey> {
239-
let tcx = self.tcx;
240-
self.lazy(&tcx.map.def_key(def_id))
241-
}
242-
243237
fn encode_item_variances(&mut self, def_id: DefId) -> LazySeq<ty::Variance> {
244238
let tcx = self.tcx;
245239
self.lazy_seq(tcx.item_variances(def_id).iter().cloned())
@@ -276,7 +270,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
276270
kind: EntryKind::Variant(self.lazy(&data)),
277271
visibility: enum_vis.simplify(),
278272
span: self.lazy(&tcx.def_span(def_id)),
279-
def_key: self.encode_def_key(def_id),
280273
attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
281274
children: self.lazy_seq(variant.fields.iter().map(|f| {
282275
assert!(f.did.is_local());
@@ -315,7 +308,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
315308
kind: EntryKind::Mod(self.lazy(&data)),
316309
visibility: vis.simplify(),
317310
span: self.lazy(&md.inner),
318-
def_key: self.encode_def_key(def_id),
319311
attributes: self.encode_attributes(attrs),
320312
children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
321313
tcx.map.local_def_id(item_id.id).index
@@ -396,7 +388,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
396388
kind: EntryKind::Field,
397389
visibility: field.vis.simplify(),
398390
span: self.lazy(&tcx.def_span(def_id)),
399-
def_key: self.encode_def_key(def_id),
400391
attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs),
401392
children: LazySeq::empty(),
402393
stability: self.encode_stability(def_id),
@@ -430,7 +421,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
430421
kind: EntryKind::Struct(self.lazy(&data)),
431422
visibility: struct_vis.simplify(),
432423
span: self.lazy(&tcx.def_span(def_id)),
433-
def_key: self.encode_def_key(def_id),
434424
attributes: LazySeq::empty(),
435425
children: LazySeq::empty(),
436426
stability: self.encode_stability(def_id),
@@ -497,7 +487,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
497487
kind: kind,
498488
visibility: trait_item.vis.simplify(),
499489
span: self.lazy(&ast_item.span),
500-
def_key: self.encode_def_key(def_id),
501490
attributes: self.encode_attributes(&ast_item.attrs),
502491
children: LazySeq::empty(),
503492
stability: self.encode_stability(def_id),
@@ -587,7 +576,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
587576
kind: kind,
588577
visibility: impl_item.vis.simplify(),
589578
span: self.lazy(&ast_item.span),
590-
def_key: self.encode_def_key(def_id),
591579
attributes: self.encode_attributes(&ast_item.attrs),
592580
children: LazySeq::empty(),
593581
stability: self.encode_stability(def_id),
@@ -750,7 +738,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
750738
kind: kind,
751739
visibility: item.vis.simplify(),
752740
span: self.lazy(&item.span),
753-
def_key: self.encode_def_key(def_id),
754741
attributes: self.encode_attributes(&item.attrs),
755742
children: match item.node {
756743
hir::ItemForeignMod(ref fm) => {
@@ -858,14 +845,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
858845

859846
/// Serialize the text of exported macros
860847
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
861-
let def_id = self.tcx.map.local_def_id(macro_def.id);
862848
Entry {
863849
kind: EntryKind::MacroDef(self.lazy(&MacroDef {
864850
body: ::syntax::print::pprust::tts_to_string(&macro_def.body)
865851
})),
866852
visibility: ty::Visibility::Public,
867853
span: self.lazy(&macro_def.span),
868-
def_key: self.encode_def_key(def_id),
869854

870855
attributes: self.encode_attributes(&macro_def.attrs),
871856
children: LazySeq::empty(),
@@ -967,7 +952,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
967952
kind: kind,
968953
visibility: nitem.vis.simplify(),
969954
span: self.lazy(&nitem.span),
970-
def_key: self.encode_def_key(def_id),
971955
attributes: self.encode_attributes(&nitem.attrs),
972956
children: LazySeq::empty(),
973957
stability: self.encode_stability(def_id),
@@ -1050,7 +1034,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10501034
kind: EntryKind::Type,
10511035
visibility: ty::Visibility::Public,
10521036
span: self.lazy(&tcx.def_span(def_id)),
1053-
def_key: self.encode_def_key(def_id),
10541037
attributes: LazySeq::empty(),
10551038
children: LazySeq::empty(),
10561039
stability: None,
@@ -1079,7 +1062,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10791062
kind: EntryKind::Closure(self.lazy(&data)),
10801063
visibility: ty::Visibility::Public,
10811064
span: self.lazy(&tcx.def_span(def_id)),
1082-
def_key: self.encode_def_key(def_id),
10831065
attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
10841066
children: LazySeq::empty(),
10851067
stability: None,
@@ -1179,6 +1161,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11791161
})
11801162
.map(|filemap| &**filemap))
11811163
}
1164+
1165+
fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
1166+
let definitions = self.tcx.map.definitions();
1167+
self.lazy(definitions.def_path_table())
1168+
}
11821169
}
11831170

11841171
struct ImplVisitor<'a, 'tcx: 'a> {
@@ -1276,6 +1263,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12761263
let codemap = self.encode_codemap();
12771264
let codemap_bytes = self.position() - i;
12781265

1266+
// Encode DefPathTable
1267+
i = self.position();
1268+
let def_path_table = self.encode_def_path_table();
1269+
let def_path_table_bytes = self.position() - i;
1270+
12791271
// Encode the def IDs of impls, for coherence checking.
12801272
i = self.position();
12811273
let impls = self.encode_impls();
@@ -1321,6 +1313,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13211313
lang_items_missing: lang_items_missing,
13221314
native_libraries: native_libraries,
13231315
codemap: codemap,
1316+
def_path_table: def_path_table,
13241317
impls: impls,
13251318
exported_symbols: exported_symbols,
13261319
index: index,
@@ -1343,6 +1336,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13431336
println!(" codemap bytes: {}", codemap_bytes);
13441337
println!(" impl bytes: {}", impl_bytes);
13451338
println!(" exp. symbols bytes: {}", exported_symbols_bytes);
1339+
println!(" def-path table bytes: {}", def_path_table_bytes);
13461340
println!(" item bytes: {}", item_bytes);
13471341
println!(" index bytes: {}", index_bytes);
13481342
println!(" zero bytes: {}", zero_bytes);

src/librustc_metadata/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub struct CrateRoot {
179179
pub lang_items_missing: LazySeq<lang_items::LangItem>,
180180
pub native_libraries: LazySeq<NativeLibrary>,
181181
pub codemap: LazySeq<syntax_pos::FileMap>,
182+
pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
182183
pub impls: LazySeq<TraitImpls>,
183184
pub exported_symbols: LazySeq<DefIndex>,
184185
pub index: LazySeq<index::Index>,
@@ -202,7 +203,6 @@ pub struct Entry<'tcx> {
202203
pub kind: EntryKind<'tcx>,
203204
pub visibility: ty::Visibility,
204205
pub span: Lazy<Span>,
205-
pub def_key: Lazy<hir::map::DefKey>,
206206
pub attributes: LazySeq<ast::Attribute>,
207207
pub children: LazySeq<DefIndex>,
208208
pub stability: Option<Lazy<attr::Stability>>,

0 commit comments

Comments
 (0)