Skip to content

Commit

Permalink
Auto merge of #76897 - Aaron1011:feature/min-proc-macro-metadata, r=p…
Browse files Browse the repository at this point in the history
…etrochenkov

Encode less metadata for proc-macro crates

Currently, we serialize the same crate metadata for proc-macro crates as
we do for normal crates. This is quite wasteful - almost none of this
metadata is ever used, and much of it can't even be deserialized (if it
contains a foreign `CrateNum`).

This PR changes metadata encoding to skip encoding the majority of crate
metadata for proc-macro crates. Most of the `Lazy<[T]>` fields are left
completetly empty, while the non-lazy fields are left as-is.

Additionally, proc-macros now have a def span that does not include
their body. This was done for normal functions in #75465, but was missed
for proc-macros.

As a result of this PR, we should only ever encode local `CrateNum`s
when encoding proc-macro crates. I've added a specialized serialization
impl for `CrateNum` to assert this.
  • Loading branch information
bors committed Sep 26, 2020
2 parents e37c99f + b965356 commit 623fb90
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 60 deletions.
56 changes: 38 additions & 18 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,11 @@ impl CrateRoot<'_> {

impl<'a, 'tcx> CrateMetadataRef<'a> {
fn is_proc_macro(&self, id: DefIndex) -> bool {
self.root.proc_macro_data.and_then(|data| data.decode(self).find(|x| *x == id)).is_some()
self.root
.proc_macro_data
.as_ref()
.and_then(|data| data.macros.decode(self).find(|x| *x == id))
.is_some()
}

fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
Expand All @@ -729,7 +733,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
// DefIndex's in root.proc_macro_data have a one-to-one correspondence
// with items in 'raw_proc_macros'.
let pos = self.root.proc_macro_data.unwrap().decode(self).position(|i| i == id).unwrap();
let pos = self
.root
.proc_macro_data
.as_ref()
.unwrap()
.macros
.decode(self)
.position(|i| i == id)
.unwrap();
&self.raw_proc_macros.unwrap()[pos]
}

Expand Down Expand Up @@ -766,7 +778,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
self.root.tables.span.get(self, index).unwrap().decode((self, sess))
self.root
.tables
.span
.get(self, index)
.unwrap_or_else(|| panic!("Missing span for {:?}", index))
.decode((self, sess))
}

fn load_proc_macro(&self, id: DefIndex, sess: &Session) -> SyntaxExtension {
Expand Down Expand Up @@ -942,7 +959,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
match self.is_proc_macro(id) {
true => self.root.proc_macro_stability,
true => self.root.proc_macro_data.as_ref().unwrap().stability,
false => self.root.tables.stability.get(self, id).map(|stab| stab.decode(self)),
}
}
Expand Down Expand Up @@ -1035,24 +1052,20 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
where
F: FnMut(Export<hir::HirId>),
{
if let Some(proc_macros_ids) = self.root.proc_macro_data.map(|d| d.decode(self)) {
if let Some(data) = &self.root.proc_macro_data {
/* If we are loading as a proc macro, we want to return the view of this crate
* as a proc macro crate.
*/
if id == CRATE_DEF_INDEX {
for def_index in proc_macros_ids {
let macros = data.macros.decode(self);
for def_index in macros {
let raw_macro = self.raw_proc_macro(def_index);
let res = Res::Def(
DefKind::Macro(macro_kind(raw_macro)),
self.local_def_id(def_index),
);
let ident = self.item_ident(def_index, sess);
callback(Export {
ident,
res,
vis: ty::Visibility::Public,
span: self.get_span(def_index, sess),
});
callback(Export { ident, res, vis: ty::Visibility::Public, span: ident.span });
}
}
return;
Expand Down Expand Up @@ -1559,12 +1572,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn all_def_path_hashes_and_def_ids(&self) -> Vec<(DefPathHash, DefId)> {
let mut def_path_hashes = self.def_path_hash_cache.lock();
(0..self.num_def_ids())
.map(|index| {
let index = DefIndex::from_usize(index);
(self.def_path_hash_unlocked(index, &mut def_path_hashes), self.local_def_id(index))
})
.collect()
let mut def_index_to_data = |index| {
(self.def_path_hash_unlocked(index, &mut def_path_hashes), self.local_def_id(index))
};
if let Some(data) = &self.root.proc_macro_data {
std::iter::once(CRATE_DEF_INDEX)
.chain(data.macros.decode(self))
.map(def_index_to_data)
.collect()
} else {
(0..self.num_def_ids())
.map(|index| def_index_to_data(DefIndex::from_usize(index)))
.collect()
}
}

/// Get the `DepNodeIndex` corresponding this crate. The result of this
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ provide! { <'tcx> tcx, def_id, other, cdata,
})
}
proc_macro_decls_static => {
cdata.root.proc_macro_decls_static.map(|index| {
DefId { krate: def_id.krate, index }
cdata.root.proc_macro_data.as_ref().map(|data| {
DefId {
krate: def_id.krate,
index: data.proc_macro_decls_static,
}
})
}
crate_disambiguator => { cdata.root.disambiguator }
Expand Down
Loading

0 comments on commit 623fb90

Please sign in to comment.