Skip to content

Commit 623fb90

Browse files
committed
Auto merge of rust-lang#76897 - Aaron1011:feature/min-proc-macro-metadata, r=petrochenkov
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 rust-lang#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.
2 parents e37c99f + b965356 commit 623fb90

File tree

4 files changed

+168
-60
lines changed

4 files changed

+168
-60
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+38-18
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,11 @@ impl CrateRoot<'_> {
707707

708708
impl<'a, 'tcx> CrateMetadataRef<'a> {
709709
fn is_proc_macro(&self, id: DefIndex) -> bool {
710-
self.root.proc_macro_data.and_then(|data| data.decode(self).find(|x| *x == id)).is_some()
710+
self.root
711+
.proc_macro_data
712+
.as_ref()
713+
.and_then(|data| data.macros.decode(self).find(|x| *x == id))
714+
.is_some()
711715
}
712716

713717
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
@@ -729,7 +733,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
729733
fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
730734
// DefIndex's in root.proc_macro_data have a one-to-one correspondence
731735
// with items in 'raw_proc_macros'.
732-
let pos = self.root.proc_macro_data.unwrap().decode(self).position(|i| i == id).unwrap();
736+
let pos = self
737+
.root
738+
.proc_macro_data
739+
.as_ref()
740+
.unwrap()
741+
.macros
742+
.decode(self)
743+
.position(|i| i == id)
744+
.unwrap();
733745
&self.raw_proc_macros.unwrap()[pos]
734746
}
735747

@@ -766,7 +778,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
766778
}
767779

768780
fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
769-
self.root.tables.span.get(self, index).unwrap().decode((self, sess))
781+
self.root
782+
.tables
783+
.span
784+
.get(self, index)
785+
.unwrap_or_else(|| panic!("Missing span for {:?}", index))
786+
.decode((self, sess))
770787
}
771788

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

943960
fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
944961
match self.is_proc_macro(id) {
945-
true => self.root.proc_macro_stability,
962+
true => self.root.proc_macro_data.as_ref().unwrap().stability,
946963
false => self.root.tables.stability.get(self, id).map(|stab| stab.decode(self)),
947964
}
948965
}
@@ -1035,24 +1052,20 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10351052
where
10361053
F: FnMut(Export<hir::HirId>),
10371054
{
1038-
if let Some(proc_macros_ids) = self.root.proc_macro_data.map(|d| d.decode(self)) {
1055+
if let Some(data) = &self.root.proc_macro_data {
10391056
/* If we are loading as a proc macro, we want to return the view of this crate
10401057
* as a proc macro crate.
10411058
*/
10421059
if id == CRATE_DEF_INDEX {
1043-
for def_index in proc_macros_ids {
1060+
let macros = data.macros.decode(self);
1061+
for def_index in macros {
10441062
let raw_macro = self.raw_proc_macro(def_index);
10451063
let res = Res::Def(
10461064
DefKind::Macro(macro_kind(raw_macro)),
10471065
self.local_def_id(def_index),
10481066
);
10491067
let ident = self.item_ident(def_index, sess);
1050-
callback(Export {
1051-
ident,
1052-
res,
1053-
vis: ty::Visibility::Public,
1054-
span: self.get_span(def_index, sess),
1055-
});
1068+
callback(Export { ident, res, vis: ty::Visibility::Public, span: ident.span });
10561069
}
10571070
}
10581071
return;
@@ -1559,12 +1572,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15591572

15601573
fn all_def_path_hashes_and_def_ids(&self) -> Vec<(DefPathHash, DefId)> {
15611574
let mut def_path_hashes = self.def_path_hash_cache.lock();
1562-
(0..self.num_def_ids())
1563-
.map(|index| {
1564-
let index = DefIndex::from_usize(index);
1565-
(self.def_path_hash_unlocked(index, &mut def_path_hashes), self.local_def_id(index))
1566-
})
1567-
.collect()
1575+
let mut def_index_to_data = |index| {
1576+
(self.def_path_hash_unlocked(index, &mut def_path_hashes), self.local_def_id(index))
1577+
};
1578+
if let Some(data) = &self.root.proc_macro_data {
1579+
std::iter::once(CRATE_DEF_INDEX)
1580+
.chain(data.macros.decode(self))
1581+
.map(def_index_to_data)
1582+
.collect()
1583+
} else {
1584+
(0..self.num_def_ids())
1585+
.map(|index| def_index_to_data(DefIndex::from_usize(index)))
1586+
.collect()
1587+
}
15681588
}
15691589

15701590
/// Get the `DepNodeIndex` corresponding this crate. The result of this

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@ provide! { <'tcx> tcx, def_id, other, cdata,
179179
})
180180
}
181181
proc_macro_decls_static => {
182-
cdata.root.proc_macro_decls_static.map(|index| {
183-
DefId { krate: def_id.krate, index }
182+
cdata.root.proc_macro_data.as_ref().map(|data| {
183+
DefId {
184+
krate: def_id.krate,
185+
index: data.proc_macro_decls_static,
186+
}
184187
})
185188
}
186189
crate_disambiguator => { cdata.root.disambiguator }

0 commit comments

Comments
 (0)