Skip to content

Commit 4e8855b

Browse files
committed
rustc_metadata: Make opt_item_ident in decoder faster and stricter
By avoiding formatting and allocations in the no-ident case, and by making the span mandatory if the ident exists. Use the optimized `opt_item_ident` to cleanup `fn each_child_of_item`
1 parent 4c6120c commit 4e8855b

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+24-33
Original file line numberDiff line numberDiff line change
@@ -722,25 +722,24 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
722722
&self.raw_proc_macros.unwrap()[pos]
723723
}
724724

725-
fn try_item_ident(&self, item_index: DefIndex, sess: &Session) -> Result<Ident, String> {
726-
let name = self
727-
.def_key(item_index)
728-
.disambiguated_data
729-
.data
730-
.get_opt_name()
731-
.ok_or_else(|| format!("Missing opt name for {:?}", item_index))?;
732-
let span = self
733-
.root
734-
.tables
735-
.ident_span
736-
.get(self, item_index)
737-
.ok_or_else(|| format!("Missing ident span for {:?} ({:?})", name, item_index))?
738-
.decode((self, sess));
739-
Ok(Ident::new(name, span))
725+
fn opt_item_ident(&self, item_index: DefIndex, sess: &Session) -> Option<Ident> {
726+
let name = self.def_key(item_index).disambiguated_data.data.get_opt_name()?;
727+
let span = match self.root.tables.ident_span.get(self, item_index) {
728+
Some(lazy_span) => lazy_span.decode((self, sess)),
729+
None => {
730+
// FIXME: this weird case of a name with no span is specific to `extern crate`
731+
// items, which are supposed to be treated like `use` items and only be encoded
732+
// to metadata as `Export`s, return `None` because that's what all the callers
733+
// expect in this case.
734+
assert_eq!(self.def_kind(item_index), DefKind::ExternCrate);
735+
return None;
736+
}
737+
};
738+
Some(Ident::new(name, span))
740739
}
741740

742741
fn item_ident(&self, item_index: DefIndex, sess: &Session) -> Ident {
743-
self.try_item_ident(item_index, sess).unwrap()
742+
self.opt_item_ident(item_index, sess).expect("no encoded ident for item")
744743
}
745744

746745
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
@@ -1102,27 +1101,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11021101
// Iterate over all children.
11031102
if let Some(children) = self.root.tables.children.get(self, id) {
11041103
for child_index in children.decode((self, sess)) {
1105-
// FIXME: Merge with the logic below.
1106-
if let None | Some(EntryKind::ForeignMod | EntryKind::Impl(_)) =
1107-
self.maybe_kind(child_index)
1108-
{
1109-
continue;
1110-
}
1111-
1112-
let def_key = self.def_key(child_index);
1113-
if def_key.disambiguated_data.data.get_opt_name().is_some() {
1114-
let span = self.get_span(child_index, sess);
1104+
if let Some(ident) = self.opt_item_ident(child_index, sess) {
11151105
let kind = self.def_kind(child_index);
1116-
let ident = self.item_ident(child_index, sess);
1117-
let vis = self.get_visibility(child_index);
1106+
if matches!(kind, DefKind::Macro(..)) {
1107+
// FIXME: Macros are currently encoded twice, once as items and once as
1108+
// reexports. We ignore the items here and only use the reexports.
1109+
continue;
1110+
}
11181111
let def_id = self.local_def_id(child_index);
11191112
let res = Res::Def(kind, def_id);
1113+
let vis = self.get_visibility(child_index);
1114+
let span = self.get_span(child_index, sess);
11201115

1121-
// FIXME: Macros are currently encoded twice, once as items and once as
1122-
// reexports. We ignore the items here and only use the reexports.
1123-
if !matches!(kind, DefKind::Macro(..)) {
1124-
callback(Export { res, ident, vis, span });
1125-
}
1116+
callback(Export { ident, res, vis, span });
11261117

11271118
// For non-re-export structs and variants add their constructors to children.
11281119
// Re-export lists automatically contain constructors when necessary.

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
133133
generator_kind => { cdata.generator_kind(def_id.index) }
134134
opt_def_kind => { Some(cdata.def_kind(def_id.index)) }
135135
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
136-
def_ident_span => {
137-
cdata.try_item_ident(def_id.index, &tcx.sess).ok().map(|ident| ident.span)
138-
}
136+
def_ident_span => { cdata.opt_item_ident(def_id.index, &tcx.sess).map(|ident| ident.span) }
139137
lookup_stability => {
140138
cdata.get_stability(def_id.index).map(|s| tcx.intern_stability(s))
141139
}

0 commit comments

Comments
 (0)