Skip to content

Commit d41bf54

Browse files
authored
Rollup merge of rust-lang#65583 - eddyb:more-query-like-cross-crate-tables, r=michaelwoerister
rustc_metadata: use a table for super_predicates, fn_sig, impl_trait_ref. This is an attempt at a part of rust-lang#65407, i.e. moving parts of cross-crate "metadata" into tables that match queries more closely. Three new tables should be enough to see some perf/metadata size changes. (need to do something similar to rust-lang#59953 (comment)) There are other bits of data that could be made into tables, but they can be more compact so the impact would likely be not as bad, and they're also more work to set up.
2 parents 2cb7d20 + 371cc39 commit d41bf54

File tree

3 files changed

+74
-93
lines changed

3 files changed

+74
-93
lines changed

src/librustc_metadata/decoder.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'tcx> EntryKind<'tcx> {
448448
EntryKind::Mod(_) => DefKind::Mod,
449449
EntryKind::Variant(_) => DefKind::Variant,
450450
EntryKind::Trait(_) => DefKind::Trait,
451-
EntryKind::TraitAlias(_) => DefKind::TraitAlias,
451+
EntryKind::TraitAlias => DefKind::TraitAlias,
452452
EntryKind::Enum(..) => DefKind::Enum,
453453
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
454454
EntryKind::ForeignType => DefKind::ForeignTy,
@@ -458,7 +458,7 @@ impl<'tcx> EntryKind<'tcx> {
458458
EntryKind::Impl(_) |
459459
EntryKind::Field |
460460
EntryKind::Generator(_) |
461-
EntryKind::Closure(_) => return None,
461+
EntryKind::Closure => return None,
462462
})
463463
}
464464
}
@@ -575,7 +575,7 @@ impl<'a, 'tcx> CrateMetadata {
575575
data.is_marker,
576576
self.def_path_table.def_path_hash(item_id))
577577
},
578-
EntryKind::TraitAlias(_) => {
578+
EntryKind::TraitAlias => {
579579
ty::TraitDef::new(self.local_def_id(item_id),
580580
hir::Unsafety::Normal,
581581
false,
@@ -680,13 +680,7 @@ impl<'a, 'tcx> CrateMetadata {
680680
item_id: DefIndex,
681681
tcx: TyCtxt<'tcx>,
682682
) -> ty::GenericPredicates<'tcx> {
683-
let super_predicates = match self.kind(item_id) {
684-
EntryKind::Trait(data) => data.decode(self).super_predicates,
685-
EntryKind::TraitAlias(data) => data.decode(self).super_predicates,
686-
_ => bug!("def-index does not refer to trait or trait alias"),
687-
};
688-
689-
super_predicates.decode((self, tcx))
683+
self.root.per_def.super_predicates.get(self, item_id).unwrap().decode((self, tcx))
690684
}
691685

692686
crate fn get_generics(&self, item_id: DefIndex, sess: &Session) -> ty::Generics {
@@ -717,7 +711,7 @@ impl<'a, 'tcx> CrateMetadata {
717711
}
718712
}
719713

720-
fn get_impl_data(&self, id: DefIndex) -> ImplData<'tcx> {
714+
fn get_impl_data(&self, id: DefIndex) -> ImplData {
721715
match self.kind(id) {
722716
EntryKind::Impl(data) => data.decode(self),
723717
_ => bug!(),
@@ -744,7 +738,7 @@ impl<'a, 'tcx> CrateMetadata {
744738
}
745739

746740
crate fn get_impl_trait(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option<ty::TraitRef<'tcx>> {
747-
self.get_impl_data(id).trait_ref.map(|tr| tr.decode((self, tcx)))
741+
self.root.per_def.impl_trait_ref.get(self, id).map(|tr| tr.decode((self, tcx)))
748742
}
749743

750744
/// Iterates over all the stability attributes in the given crate.
@@ -1118,7 +1112,7 @@ impl<'a, 'tcx> CrateMetadata {
11181112
def_key.parent.and_then(|parent_index| {
11191113
match self.kind(parent_index) {
11201114
EntryKind::Trait(_) |
1121-
EntryKind::TraitAlias(_) => Some(self.local_def_id(parent_index)),
1115+
EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
11221116
_ => None,
11231117
}
11241118
})
@@ -1245,16 +1239,7 @@ impl<'a, 'tcx> CrateMetadata {
12451239
}
12461240

12471241
crate fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
1248-
let sig = match self.kind(id) {
1249-
EntryKind::Fn(data) |
1250-
EntryKind::ForeignFn(data) => data.decode(self).sig,
1251-
EntryKind::Method(data) => data.decode(self).fn_data.sig,
1252-
EntryKind::Variant(data) |
1253-
EntryKind::Struct(data, _) => data.decode(self).ctor_sig.unwrap(),
1254-
EntryKind::Closure(data) => data.decode(self).sig,
1255-
_ => bug!(),
1256-
};
1257-
sig.decode((self, tcx))
1242+
self.root.per_def.fn_sig.get(self, id).unwrap().decode((self, tcx))
12581243
}
12591244

12601245
#[inline]

src/librustc_metadata/encoder.rs

+46-38
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ struct PerDefTables<'tcx> {
7171
deprecation: PerDefTable<Lazy<attr::Deprecation>>,
7272

7373
ty: PerDefTable<Lazy<Ty<'tcx>>>,
74+
fn_sig: PerDefTable<Lazy<ty::PolyFnSig<'tcx>>>,
75+
impl_trait_ref: PerDefTable<Lazy<ty::TraitRef<'tcx>>>,
7476
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
7577
variances: PerDefTable<Lazy<[ty::Variance]>>,
7678
generics: PerDefTable<Lazy<ty::Generics>>,
7779
predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
7880
predicates_defined_on: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
81+
super_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
7982

8083
mir: PerDefTable<Lazy<mir::Body<'tcx>>>,
8184
promoted_mir: PerDefTable<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
@@ -508,11 +511,14 @@ impl<'tcx> EncodeContext<'tcx> {
508511
deprecation: self.per_def.deprecation.encode(&mut self.opaque),
509512

510513
ty: self.per_def.ty.encode(&mut self.opaque),
514+
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
515+
impl_trait_ref: self.per_def.impl_trait_ref.encode(&mut self.opaque),
511516
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
512517
variances: self.per_def.variances.encode(&mut self.opaque),
513518
generics: self.per_def.generics.encode(&mut self.opaque),
514519
predicates: self.per_def.predicates.encode(&mut self.opaque),
515520
predicates_defined_on: self.per_def.predicates_defined_on.encode(&mut self.opaque),
521+
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),
516522

517523
mir: self.per_def.mir.encode(&mut self.opaque),
518524
promoted_mir: self.per_def.promoted_mir.encode(&mut self.opaque),
@@ -635,13 +641,7 @@ impl EncodeContext<'tcx> {
635641
let data = VariantData {
636642
ctor_kind: variant.ctor_kind,
637643
discr: variant.discr,
638-
// FIXME(eddyb) deduplicate these with `encode_enum_variant_ctor`.
639644
ctor: variant.ctor_def_id.map(|did| did.index),
640-
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
641-
variant.ctor_def_id.map(|ctor_def_id| self.lazy(&tcx.fn_sig(ctor_def_id)))
642-
} else {
643-
None
644-
},
645645
};
646646

647647
let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();
@@ -660,6 +660,11 @@ impl EncodeContext<'tcx> {
660660
self.encode_deprecation(def_id);
661661
self.encode_item_type(def_id);
662662
if variant.ctor_kind == CtorKind::Fn {
663+
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
664+
if let Some(ctor_def_id) = variant.ctor_def_id {
665+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
666+
}
667+
// FIXME(eddyb) is this ever used?
663668
self.encode_variances_of(def_id);
664669
}
665670
self.encode_generics(def_id);
@@ -679,15 +684,11 @@ impl EncodeContext<'tcx> {
679684
let def_id = variant.ctor_def_id.unwrap();
680685
debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id);
681686

687+
// FIXME(eddyb) encode only the `CtorKind` for constructors.
682688
let data = VariantData {
683689
ctor_kind: variant.ctor_kind,
684690
discr: variant.discr,
685691
ctor: Some(def_id.index),
686-
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
687-
Some(self.lazy(tcx.fn_sig(def_id)))
688-
} else {
689-
None
690-
}
691692
};
692693

693694
// Variant constructors have the same visibility as the parent enums, unless marked as
@@ -706,6 +707,7 @@ impl EncodeContext<'tcx> {
706707
self.encode_deprecation(def_id);
707708
self.encode_item_type(def_id);
708709
if variant.ctor_kind == CtorKind::Fn {
710+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
709711
self.encode_variances_of(def_id);
710712
}
711713
self.encode_generics(def_id);
@@ -780,11 +782,6 @@ impl EncodeContext<'tcx> {
780782
ctor_kind: variant.ctor_kind,
781783
discr: variant.discr,
782784
ctor: Some(def_id.index),
783-
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
784-
Some(self.lazy(tcx.fn_sig(def_id)))
785-
} else {
786-
None
787-
}
788785
};
789786

790787
let struct_id = tcx.hir().as_local_hir_id(adt_def_id).unwrap();
@@ -811,6 +808,7 @@ impl EncodeContext<'tcx> {
811808
self.encode_deprecation(def_id);
812809
self.encode_item_type(def_id);
813810
if variant.ctor_kind == CtorKind::Fn {
811+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
814812
self.encode_variances_of(def_id);
815813
}
816814
self.encode_generics(def_id);
@@ -835,6 +833,11 @@ impl EncodeContext<'tcx> {
835833
self.tcx.predicates_defined_on(def_id))
836834
}
837835

836+
fn encode_super_predicates(&mut self, def_id: DefId) {
837+
debug!("EncodeContext::encode_super_predicates({:?})", def_id);
838+
record!(self.per_def.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id));
839+
}
840+
838841
fn encode_info_for_trait_item(&mut self, def_id: DefId) {
839842
debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
840843
let tcx = self.tcx;
@@ -874,7 +877,6 @@ impl EncodeContext<'tcx> {
874877
asyncness: m_sig.header.asyncness,
875878
constness: hir::Constness::NotConst,
876879
param_names,
877-
sig: self.lazy(tcx.fn_sig(def_id)),
878880
}
879881
} else {
880882
bug!()
@@ -906,6 +908,7 @@ impl EncodeContext<'tcx> {
906908
ty::AssocKind::OpaqueTy => unreachable!(),
907909
}
908910
if trait_item.kind == ty::AssocKind::Method {
911+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
909912
self.encode_variances_of(def_id);
910913
}
911914
self.encode_generics(def_id);
@@ -952,7 +955,6 @@ impl EncodeContext<'tcx> {
952955
asyncness: sig.header.asyncness,
953956
constness: sig.header.constness,
954957
param_names: self.encode_fn_param_names_for_body(body),
955-
sig: self.lazy(tcx.fn_sig(def_id)),
956958
}
957959
} else {
958960
bug!()
@@ -973,6 +975,7 @@ impl EncodeContext<'tcx> {
973975
self.encode_deprecation(def_id);
974976
self.encode_item_type(def_id);
975977
if impl_item.kind == ty::AssocKind::Method {
978+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
976979
self.encode_variances_of(def_id);
977980
}
978981
self.encode_generics(def_id);
@@ -1081,7 +1084,6 @@ impl EncodeContext<'tcx> {
10811084
asyncness: header.asyncness,
10821085
constness: header.constness,
10831086
param_names: self.encode_fn_param_names_for_body(body),
1084-
sig: self.lazy(tcx.fn_sig(def_id)),
10851087
};
10861088

10871089
EntryKind::Fn(self.lazy(data))
@@ -1109,7 +1111,6 @@ impl EncodeContext<'tcx> {
11091111
ctor_kind: variant.ctor_kind,
11101112
discr: variant.discr,
11111113
ctor,
1112-
ctor_sig: None,
11131114
}), adt_def.repr)
11141115
}
11151116
hir::ItemKind::Union(..) => {
@@ -1120,7 +1121,6 @@ impl EncodeContext<'tcx> {
11201121
ctor_kind: variant.ctor_kind,
11211122
discr: variant.discr,
11221123
ctor: None,
1123-
ctor_sig: None,
11241124
}), adt_def.repr)
11251125
}
11261126
hir::ItemKind::Impl(_, _, defaultness, ..) => {
@@ -1154,7 +1154,6 @@ impl EncodeContext<'tcx> {
11541154
defaultness,
11551155
parent_impl: parent,
11561156
coerce_unsized_info,
1157-
trait_ref: trait_ref.map(|trait_ref| self.lazy(trait_ref)),
11581157
};
11591158

11601159
EntryKind::Impl(self.lazy(data))
@@ -1166,18 +1165,11 @@ impl EncodeContext<'tcx> {
11661165
paren_sugar: trait_def.paren_sugar,
11671166
has_auto_impl: self.tcx.trait_is_auto(def_id),
11681167
is_marker: trait_def.is_marker,
1169-
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
11701168
};
11711169

11721170
EntryKind::Trait(self.lazy(data))
11731171
}
1174-
hir::ItemKind::TraitAlias(..) => {
1175-
let data = TraitAliasData {
1176-
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
1177-
};
1178-
1179-
EntryKind::TraitAlias(self.lazy(data))
1180-
}
1172+
hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias,
11811173
hir::ItemKind::ExternCrate(_) |
11821174
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
11831175
});
@@ -1232,6 +1224,14 @@ impl EncodeContext<'tcx> {
12321224
hir::ItemKind::Impl(..) => self.encode_item_type(def_id),
12331225
_ => {}
12341226
}
1227+
if let hir::ItemKind::Fn(..) = item.kind {
1228+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
1229+
}
1230+
if let hir::ItemKind::Impl(..) = item.kind {
1231+
if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) {
1232+
record!(self.per_def.impl_trait_ref[def_id] <- trait_ref);
1233+
}
1234+
}
12351235
self.encode_inherent_implementations(def_id);
12361236
match item.kind {
12371237
hir::ItemKind::Enum(..) |
@@ -1269,6 +1269,13 @@ impl EncodeContext<'tcx> {
12691269
}
12701270
_ => {} // not *wrong* for other kinds of items, but not needed
12711271
}
1272+
match item.kind {
1273+
hir::ItemKind::Trait(..) |
1274+
hir::ItemKind::TraitAlias(..) => {
1275+
self.encode_super_predicates(def_id);
1276+
}
1277+
_ => {}
1278+
}
12721279

12731280
let mir = match item.kind {
12741281
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => true,
@@ -1321,10 +1328,12 @@ impl EncodeContext<'tcx> {
13211328
fn encode_info_for_closure(&mut self, def_id: DefId) {
13221329
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
13231330

1324-
let tables = self.tcx.typeck_tables_of(def_id);
1331+
// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
1332+
// including on the signature, which is inferred in `typeck_tables_of.
13251333
let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
1334+
let ty = self.tcx.typeck_tables_of(def_id).node_type(hir_id);
13261335

1327-
record!(self.per_def.kind[def_id] <- match tables.node_type(hir_id).kind {
1336+
record!(self.per_def.kind[def_id] <- match ty.kind {
13281337
ty::Generator(def_id, ..) => {
13291338
let layout = self.tcx.generator_layout(def_id);
13301339
let data = GeneratorData {
@@ -1333,18 +1342,17 @@ impl EncodeContext<'tcx> {
13331342
EntryKind::Generator(self.lazy(data))
13341343
}
13351344

1336-
ty::Closure(def_id, substs) => {
1337-
let sig = substs.as_closure().sig(def_id, self.tcx);
1338-
let data = ClosureData { sig: self.lazy(sig) };
1339-
EntryKind::Closure(self.lazy(data))
1340-
}
1345+
ty::Closure(..) => EntryKind::Closure,
13411346

13421347
_ => bug!("closure that is neither generator nor closure"),
13431348
});
13441349
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
13451350
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
13461351
record!(self.per_def.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
13471352
self.encode_item_type(def_id);
1353+
if let ty::Closure(def_id, substs) = ty.kind {
1354+
record!(self.per_def.fn_sig[def_id] <- substs.as_closure().sig(def_id, self.tcx));
1355+
}
13481356
self.encode_generics(def_id);
13491357
self.encode_optimized_mir(def_id);
13501358
self.encode_promoted_mir(def_id);
@@ -1553,7 +1561,6 @@ impl EncodeContext<'tcx> {
15531561
asyncness: hir::IsAsync::NotAsync,
15541562
constness: hir::Constness::NotConst,
15551563
param_names: self.encode_fn_param_names(names),
1556-
sig: self.lazy(tcx.fn_sig(def_id)),
15571564
};
15581565
EntryKind::ForeignFn(self.lazy(data))
15591566
}
@@ -1569,6 +1576,7 @@ impl EncodeContext<'tcx> {
15691576
self.encode_deprecation(def_id);
15701577
self.encode_item_type(def_id);
15711578
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
1579+
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
15721580
self.encode_variances_of(def_id);
15731581
}
15741582
self.encode_generics(def_id);

0 commit comments

Comments
 (0)