Skip to content

Commit

Permalink
Rollup merge of rust-lang#65583 - eddyb:more-query-like-cross-crate-t…
Browse files Browse the repository at this point in the history
…ables, 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.
  • Loading branch information
JohnTitor committed Oct 22, 2019
2 parents 16397a9 + 371cc39 commit 1d2e145
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 93 deletions.
31 changes: 8 additions & 23 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl<'tcx> EntryKind<'tcx> {
EntryKind::Mod(_) => DefKind::Mod,
EntryKind::Variant(_) => DefKind::Variant,
EntryKind::Trait(_) => DefKind::Trait,
EntryKind::TraitAlias(_) => DefKind::TraitAlias,
EntryKind::TraitAlias => DefKind::TraitAlias,
EntryKind::Enum(..) => DefKind::Enum,
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
EntryKind::ForeignType => DefKind::ForeignTy,
Expand All @@ -458,7 +458,7 @@ impl<'tcx> EntryKind<'tcx> {
EntryKind::Impl(_) |
EntryKind::Field |
EntryKind::Generator(_) |
EntryKind::Closure(_) => return None,
EntryKind::Closure => return None,
})
}
}
Expand Down Expand Up @@ -575,7 +575,7 @@ impl<'a, 'tcx> CrateMetadata {
data.is_marker,
self.def_path_table.def_path_hash(item_id))
},
EntryKind::TraitAlias(_) => {
EntryKind::TraitAlias => {
ty::TraitDef::new(self.local_def_id(item_id),
hir::Unsafety::Normal,
false,
Expand Down Expand Up @@ -680,13 +680,7 @@ impl<'a, 'tcx> CrateMetadata {
item_id: DefIndex,
tcx: TyCtxt<'tcx>,
) -> ty::GenericPredicates<'tcx> {
let super_predicates = match self.kind(item_id) {
EntryKind::Trait(data) => data.decode(self).super_predicates,
EntryKind::TraitAlias(data) => data.decode(self).super_predicates,
_ => bug!("def-index does not refer to trait or trait alias"),
};

super_predicates.decode((self, tcx))
self.root.per_def.super_predicates.get(self, item_id).unwrap().decode((self, tcx))
}

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

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

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

/// Iterates over all the stability attributes in the given crate.
Expand Down Expand Up @@ -1118,7 +1112,7 @@ impl<'a, 'tcx> CrateMetadata {
def_key.parent.and_then(|parent_index| {
match self.kind(parent_index) {
EntryKind::Trait(_) |
EntryKind::TraitAlias(_) => Some(self.local_def_id(parent_index)),
EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
_ => None,
}
})
Expand Down Expand Up @@ -1245,16 +1239,7 @@ impl<'a, 'tcx> CrateMetadata {
}

crate fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
let sig = match self.kind(id) {
EntryKind::Fn(data) |
EntryKind::ForeignFn(data) => data.decode(self).sig,
EntryKind::Method(data) => data.decode(self).fn_data.sig,
EntryKind::Variant(data) |
EntryKind::Struct(data, _) => data.decode(self).ctor_sig.unwrap(),
EntryKind::Closure(data) => data.decode(self).sig,
_ => bug!(),
};
sig.decode((self, tcx))
self.root.per_def.fn_sig.get(self, id).unwrap().decode((self, tcx))
}

#[inline]
Expand Down
84 changes: 46 additions & 38 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ struct PerDefTables<'tcx> {
deprecation: PerDefTable<Lazy<attr::Deprecation>>,

ty: PerDefTable<Lazy<Ty<'tcx>>>,
fn_sig: PerDefTable<Lazy<ty::PolyFnSig<'tcx>>>,
impl_trait_ref: PerDefTable<Lazy<ty::TraitRef<'tcx>>>,
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
variances: PerDefTable<Lazy<[ty::Variance]>>,
generics: PerDefTable<Lazy<ty::Generics>>,
predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
predicates_defined_on: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
super_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,

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

ty: self.per_def.ty.encode(&mut self.opaque),
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
impl_trait_ref: self.per_def.impl_trait_ref.encode(&mut self.opaque),
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
variances: self.per_def.variances.encode(&mut self.opaque),
generics: self.per_def.generics.encode(&mut self.opaque),
predicates: self.per_def.predicates.encode(&mut self.opaque),
predicates_defined_on: self.per_def.predicates_defined_on.encode(&mut self.opaque),
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),

mir: self.per_def.mir.encode(&mut self.opaque),
promoted_mir: self.per_def.promoted_mir.encode(&mut self.opaque),
Expand Down Expand Up @@ -635,13 +641,7 @@ impl EncodeContext<'tcx> {
let data = VariantData {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
// FIXME(eddyb) deduplicate these with `encode_enum_variant_ctor`.
ctor: variant.ctor_def_id.map(|did| did.index),
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
variant.ctor_def_id.map(|ctor_def_id| self.lazy(&tcx.fn_sig(ctor_def_id)))
} else {
None
},
};

let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();
Expand All @@ -660,6 +660,11 @@ impl EncodeContext<'tcx> {
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
if variant.ctor_kind == CtorKind::Fn {
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
if let Some(ctor_def_id) = variant.ctor_def_id {
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
}
// FIXME(eddyb) is this ever used?
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
Expand All @@ -679,15 +684,11 @@ impl EncodeContext<'tcx> {
let def_id = variant.ctor_def_id.unwrap();
debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id);

// FIXME(eddyb) encode only the `CtorKind` for constructors.
let data = VariantData {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: Some(def_id.index),
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
Some(self.lazy(tcx.fn_sig(def_id)))
} else {
None
}
};

// Variant constructors have the same visibility as the parent enums, unless marked as
Expand All @@ -706,6 +707,7 @@ impl EncodeContext<'tcx> {
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
if variant.ctor_kind == CtorKind::Fn {
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
Expand Down Expand Up @@ -780,11 +782,6 @@ impl EncodeContext<'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: Some(def_id.index),
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
Some(self.lazy(tcx.fn_sig(def_id)))
} else {
None
}
};

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

fn encode_super_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_super_predicates({:?})", def_id);
record!(self.per_def.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id));
}

fn encode_info_for_trait_item(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
let tcx = self.tcx;
Expand Down Expand Up @@ -874,7 +877,6 @@ impl EncodeContext<'tcx> {
asyncness: m_sig.header.asyncness,
constness: hir::Constness::NotConst,
param_names,
sig: self.lazy(tcx.fn_sig(def_id)),
}
} else {
bug!()
Expand Down Expand Up @@ -906,6 +908,7 @@ impl EncodeContext<'tcx> {
ty::AssocKind::OpaqueTy => unreachable!(),
}
if trait_item.kind == ty::AssocKind::Method {
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
Expand Down Expand Up @@ -952,7 +955,6 @@ impl EncodeContext<'tcx> {
asyncness: sig.header.asyncness,
constness: sig.header.constness,
param_names: self.encode_fn_param_names_for_body(body),
sig: self.lazy(tcx.fn_sig(def_id)),
}
} else {
bug!()
Expand All @@ -973,6 +975,7 @@ impl EncodeContext<'tcx> {
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
if impl_item.kind == ty::AssocKind::Method {
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
Expand Down Expand Up @@ -1081,7 +1084,6 @@ impl EncodeContext<'tcx> {
asyncness: header.asyncness,
constness: header.constness,
param_names: self.encode_fn_param_names_for_body(body),
sig: self.lazy(tcx.fn_sig(def_id)),
};

EntryKind::Fn(self.lazy(data))
Expand Down Expand Up @@ -1109,7 +1111,6 @@ impl EncodeContext<'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor,
ctor_sig: None,
}), adt_def.repr)
}
hir::ItemKind::Union(..) => {
Expand All @@ -1120,7 +1121,6 @@ impl EncodeContext<'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: None,
ctor_sig: None,
}), adt_def.repr)
}
hir::ItemKind::Impl(_, _, defaultness, ..) => {
Expand Down Expand Up @@ -1154,7 +1154,6 @@ impl EncodeContext<'tcx> {
defaultness,
parent_impl: parent,
coerce_unsized_info,
trait_ref: trait_ref.map(|trait_ref| self.lazy(trait_ref)),
};

EntryKind::Impl(self.lazy(data))
Expand All @@ -1166,18 +1165,11 @@ impl EncodeContext<'tcx> {
paren_sugar: trait_def.paren_sugar,
has_auto_impl: self.tcx.trait_is_auto(def_id),
is_marker: trait_def.is_marker,
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
};

EntryKind::Trait(self.lazy(data))
}
hir::ItemKind::TraitAlias(..) => {
let data = TraitAliasData {
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
};

EntryKind::TraitAlias(self.lazy(data))
}
hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias,
hir::ItemKind::ExternCrate(_) |
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
});
Expand Down Expand Up @@ -1232,6 +1224,14 @@ impl EncodeContext<'tcx> {
hir::ItemKind::Impl(..) => self.encode_item_type(def_id),
_ => {}
}
if let hir::ItemKind::Fn(..) = item.kind {
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
}
if let hir::ItemKind::Impl(..) = item.kind {
if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) {
record!(self.per_def.impl_trait_ref[def_id] <- trait_ref);
}
}
self.encode_inherent_implementations(def_id);
match item.kind {
hir::ItemKind::Enum(..) |
Expand Down Expand Up @@ -1269,6 +1269,13 @@ impl EncodeContext<'tcx> {
}
_ => {} // not *wrong* for other kinds of items, but not needed
}
match item.kind {
hir::ItemKind::Trait(..) |
hir::ItemKind::TraitAlias(..) => {
self.encode_super_predicates(def_id);
}
_ => {}
}

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

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

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

ty::Closure(def_id, substs) => {
let sig = substs.as_closure().sig(def_id, self.tcx);
let data = ClosureData { sig: self.lazy(sig) };
EntryKind::Closure(self.lazy(data))
}
ty::Closure(..) => EntryKind::Closure,

_ => bug!("closure that is neither generator nor closure"),
});
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
record!(self.per_def.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
self.encode_item_type(def_id);
if let ty::Closure(def_id, substs) = ty.kind {
record!(self.per_def.fn_sig[def_id] <- substs.as_closure().sig(def_id, self.tcx));
}
self.encode_generics(def_id);
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
Expand Down Expand Up @@ -1553,7 +1561,6 @@ impl EncodeContext<'tcx> {
asyncness: hir::IsAsync::NotAsync,
constness: hir::Constness::NotConst,
param_names: self.encode_fn_param_names(names),
sig: self.lazy(tcx.fn_sig(def_id)),
};
EntryKind::ForeignFn(self.lazy(data))
}
Expand All @@ -1569,6 +1576,7 @@ impl EncodeContext<'tcx> {
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
record!(self.per_def.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
Expand Down
Loading

0 comments on commit 1d2e145

Please sign in to comment.