Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: arena-allocate the slice in ty::GenericsPredicate, not the whole struct. #65535

Merged
merged 1 commit into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ macro_rules! arena_types {
rustc::hir::def_id::DefId,
>,
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
[few] lint_levels: rustc::lint::LintLevelMap,
[few] stability_index: rustc::middle::stability::Index<'tcx>,
[few] features: syntax::feature_gate::Features,
Expand Down
13 changes: 5 additions & 8 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ rustc_queries! {
/// predicate gets in the way of some checks, which are intended
/// to operate over only the actual where-clauses written by the
/// user.)
query predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
query predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
cache_on_disk_if { key.is_local() }
}

Expand Down Expand Up @@ -184,12 +184,10 @@ rustc_queries! {
/// predicates (where-clauses) directly defined on it. This is
/// equal to the `explicit_predicates_of` predicates plus the
/// `inferred_outlives_of` predicates.
query predicates_defined_on(_: DefId)
-> &'tcx ty::GenericPredicates<'tcx> {}
query predicates_defined_on(_: DefId) -> ty::GenericPredicates<'tcx> {}

/// Returns the predicates written explicitly by the user.
query explicit_predicates_of(_: DefId)
-> &'tcx ty::GenericPredicates<'tcx> {}
query explicit_predicates_of(_: DefId) -> ty::GenericPredicates<'tcx> {}

/// Returns the inferred outlives predicates (e.g., for `struct
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
Expand All @@ -201,14 +199,13 @@ rustc_queries! {
/// evaluate them even during type conversion, often before the
/// full predicates are available (note that supertraits have
/// additional acyclicity requirements).
query super_predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
query super_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
desc { |tcx| "computing the supertraits of `{}`", tcx.def_path_str(key) }
}

/// To avoid cycles within the predicates of a single item we compute
/// per-type-parameter predicates for resolving `T::AssocTy`.
query type_param_predicates(key: (DefId, DefId))
-> &'tcx ty::GenericPredicates<'tcx> {
query type_param_predicates(key: (DefId, DefId)) -> ty::GenericPredicates<'tcx> {
no_force
desc { |tcx| "computing the bounds for type parameter `{}`", {
let id = tcx.hir().as_local_hir_id(key.1).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<String>

// The predicates will contain default bounds like `T: Sized`. We need to
// remove these bounds, and add `T: ?Sized` to any untouched type parameters.
let predicates = &tcx.predicates_of(impl_def_id).predicates;
let predicates = tcx.predicates_of(impl_def_id).predicates;
let mut pretty_predicates = Vec::with_capacity(
predicates.len() + types_without_default_bounds.len());

Expand Down
35 changes: 20 additions & 15 deletions src/librustc/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::intrinsics;
use crate::ty::{self, Ty, TyCtxt};
use crate::ty::subst::SubstsRef;
use crate::mir::interpret::Allocation;
use syntax_pos::Span;

/// The shorthand encoding uses an enum's variant index `usize`
/// and is offset by this value so it never matches a real variant.
Expand Down Expand Up @@ -92,16 +93,16 @@ pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
Ok(())
}

pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
predicates: &ty::GenericPredicates<'tcx>,
cache: C)
-> Result<(), E::Error>
pub fn encode_spanned_predicates<'tcx, E, C>(
encoder: &mut E,
predicates: &'tcx [(ty::Predicate<'tcx>, Span)],
cache: C,
) -> Result<(), E::Error>
where E: TyEncoder,
C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
{
predicates.parent.encode(encoder)?;
predicates.predicates.len().encode(encoder)?;
for (predicate, span) in &predicates.predicates {
predicates.len().encode(encoder)?;
for (predicate, span) in predicates {
encode_with_shorthand(encoder, predicate, &cache)?;
span.encode(encoder)?;
}
Expand Down Expand Up @@ -182,13 +183,15 @@ where
}

#[inline]
pub fn decode_predicates<D>(decoder: &mut D) -> Result<ty::GenericPredicates<'tcx>, D::Error>
pub fn decode_spanned_predicates<D>(
decoder: &mut D,
) -> Result<&'tcx [(ty::Predicate<'tcx>, Span)], D::Error>
where
D: TyDecoder<'tcx>,
{
Ok(ty::GenericPredicates {
parent: Decodable::decode(decoder)?,
predicates: (0..decoder.read_usize()?).map(|_| {
let tcx = decoder.tcx();
Ok(tcx.arena.alloc_from_iter(
(0..decoder.read_usize()?).map(|_| {
// Handle shorthands first, if we have an usize > 0x80.
let predicate = if decoder.positioned_at_shorthand() {
let pos = decoder.read_usize()?;
Expand All @@ -202,7 +205,7 @@ where
Ok((predicate, Decodable::decode(decoder)?))
})
.collect::<Result<Vec<_>, _>>()?,
})
))
}

#[inline]
Expand Down Expand Up @@ -339,6 +342,8 @@ macro_rules! implement_ty_decoder {
use $crate::ty::subst::SubstsRef;
use $crate::hir::def_id::{CrateNum};

use syntax_pos::Span;

use super::$DecoderName;

impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
Expand Down Expand Up @@ -393,11 +398,11 @@ macro_rules! implement_ty_decoder {
}
}

impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
impl<$($typaram),*> SpecializedDecoder<&'tcx [(ty::Predicate<'tcx>, Span)]>
for $DecoderName<$($typaram),*> {
fn specialized_decode(&mut self)
-> Result<ty::GenericPredicates<'tcx>, Self::Error> {
decode_predicates(self)
-> Result<&'tcx [(ty::Predicate<'tcx>, Span)], Self::Error> {
decode_spanned_predicates(self)
}
}

Expand Down
14 changes: 0 additions & 14 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ impl<'tcx> CtxtInterners<'tcx> {
}
}

pub struct Common<'tcx> {
pub empty_predicates: ty::GenericPredicates<'tcx>,
}

pub struct CommonTypes<'tcx> {
pub unit: Ty<'tcx>,
pub bool: Ty<'tcx>,
Expand Down Expand Up @@ -1039,9 +1035,6 @@ pub struct GlobalCtxt<'tcx> {

pub prof: SelfProfilerRef,

/// Common objects.
pub common: Common<'tcx>,

/// Common types, pre-interned for your convenience.
pub types: CommonTypes<'tcx>,

Expand Down Expand Up @@ -1213,12 +1206,6 @@ impl<'tcx> TyCtxt<'tcx> {
s.fatal(&err);
});
let interners = CtxtInterners::new(&arenas.interner);
let common = Common {
empty_predicates: ty::GenericPredicates {
parent: None,
predicates: vec![],
},
};
let common_types = CommonTypes::new(&interners);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
Expand Down Expand Up @@ -1273,7 +1260,6 @@ impl<'tcx> TyCtxt<'tcx> {
interners,
dep_graph,
prof: s.prof.clone(),
common,
types: common_types,
lifetimes: common_lifetimes,
consts: common_consts,
Expand Down
11 changes: 4 additions & 7 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,15 +1018,12 @@ impl<'tcx> Generics {
}

/// Bounds on generics.
#[derive(Clone, Default, Debug, HashStable)]
#[derive(Copy, Clone, Default, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct GenericPredicates<'tcx> {
pub parent: Option<DefId>,
pub predicates: Vec<(Predicate<'tcx>, Span)>,
pub predicates: &'tcx [(Predicate<'tcx>, Span)],
}

impl<'tcx> rustc_serialize::UseSpecializedEncodable for GenericPredicates<'tcx> {}
impl<'tcx> rustc_serialize::UseSpecializedDecodable for GenericPredicates<'tcx> {}

impl<'tcx> GenericPredicates<'tcx> {
pub fn instantiate(
&self,
Expand Down Expand Up @@ -2321,7 +2318,7 @@ impl<'tcx> AdtDef {
}

#[inline]
pub fn predicates(&self, tcx: TyCtxt<'tcx>) -> &'tcx GenericPredicates<'tcx> {
pub fn predicates(&self, tcx: TyCtxt<'tcx>) -> GenericPredicates<'tcx> {
tcx.predicates_of(self.did)
}

Expand Down Expand Up @@ -2561,7 +2558,7 @@ impl<'tcx> AdtDef {
def_id: sized_trait,
substs: tcx.mk_substs_trait(ty, &[])
}).to_predicate();
let predicates = &tcx.predicates_of(self.did).predicates;
let predicates = tcx.predicates_of(self.did).predicates;
if predicates.iter().any(|(p, _)| *p == sized_predicate) {
vec![]
} else {
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,16 @@ where
}
}

impl<'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>> for CacheEncoder<'a, 'tcx, E>
impl<'a, 'tcx, E> SpecializedEncoder<&'tcx [(ty::Predicate<'tcx>, Span)]>
for CacheEncoder<'a, 'tcx, E>
where
E: 'a + TyEncoder,
{
#[inline]
fn specialized_encode(&mut self,
predicates: &ty::GenericPredicates<'tcx>)
predicates: &&'tcx [(ty::Predicate<'tcx>, Span)])
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates,
ty_codec::encode_spanned_predicates(self, predicates,
|encoder| &mut encoder.predicate_shorthands)
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,12 +1218,6 @@ EnumTypeFoldableImpl! {
}
}

BraceStructTypeFoldableImpl! {
impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
parent, predicates
}
}

impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
// This code is hot enough that it's worth specializing for a list of
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
if cx.tcx.features().trivial_bounds {
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
let predicates = cx.tcx.predicates_of(def_id);
for &(predicate, span) in &predicates.predicates {
for &(predicate, span) in predicates.predicates {
let predicate_kind_name = match predicate {
Trait(..) => "Trait",
TypeOutlives(..) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
}
ty::Opaque(def, _) => {
let mut has_emitted = false;
for (predicate, _) in &cx.tcx.predicates_of(def).predicates {
for (predicate, _) in cx.tcx.predicates_of(def).predicates {
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
let trait_ref = poly_trait_predicate.skip_binder().trait_ref;
let def_id = trait_ref.def_id;
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
generics_of => {
tcx.arena.alloc(cdata.get_generics(def_id.index, tcx.sess))
}
predicates_of => { tcx.arena.alloc(cdata.get_predicates(def_id.index, tcx)) }
predicates_defined_on => {
tcx.arena.alloc(cdata.get_predicates_defined_on(def_id.index, tcx))
}
super_predicates_of => { tcx.arena.alloc(cdata.get_super_predicates(def_id.index, tcx)) }
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
predicates_defined_on => { cdata.get_predicates_defined_on(def_id.index, tcx) }
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
trait_def => {
tcx.arena.alloc(cdata.get_trait_def(def_id.index, tcx.sess))
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ impl<'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'tcx> {
}
}

impl<'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'tcx> {
impl<'tcx> SpecializedEncoder<&'tcx [(ty::Predicate<'tcx>, Span)]> for EncodeContext<'tcx> {
fn specialized_encode(&mut self,
predicates: &ty::GenericPredicates<'tcx>)
predicates: &&'tcx [(ty::Predicate<'tcx>, Span)])
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
ty_codec::encode_spanned_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
}
}

Expand Down Expand Up @@ -826,13 +826,13 @@ impl EncodeContext<'tcx> {

fn encode_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_predicates({:?})", def_id);
record!(self.per_def.predicates[def_id] <- &*self.tcx.predicates_of(def_id));
record!(self.per_def.predicates[def_id] <- self.tcx.predicates_of(def_id));
}

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

fn encode_info_for_trait_item(&mut self, def_id: DefId) {
Expand Down Expand Up @@ -1166,14 +1166,14 @@ 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)),
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)),
super_predicates: self.lazy(tcx.super_predicates_of(def_id)),
};

EntryKind::TraitAlias(self.lazy(data))
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -
let mut current = def_id;
loop {
let predicates = tcx.predicates_of(current);
for (predicate, _) in &predicates.predicates {
for (predicate, _) in predicates.predicates {
match predicate {
| Predicate::RegionOutlives(_)
| Predicate::TypeOutlives(_)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ trait DefIdVisitor<'tcx> {
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> bool {
self.skeleton().visit_trait(trait_ref)
}
fn visit_predicates(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> bool {
fn visit_predicates(&mut self, predicates: ty::GenericPredicates<'tcx>) -> bool {
self.skeleton().visit_predicates(predicates)
}
}
Expand All @@ -88,7 +88,7 @@ where
(!self.def_id_visitor.shallow() && substs.visit_with(self))
}

fn visit_predicates(&mut self, predicates: &ty::GenericPredicates<'tcx>) -> bool {
fn visit_predicates(&mut self, predicates: ty::GenericPredicates<'tcx>) -> bool {
let ty::GenericPredicates { parent: _, predicates } = predicates;
for (predicate, _span) in predicates {
match predicate {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_traits/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn program_clauses_for_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Clauses<'_> {

let implemented_from_env = Clause::ForAll(ty::Binder::bind(implemented_from_env));

let predicates = &tcx.predicates_defined_on(def_id).predicates;
let predicates = tcx.predicates_defined_on(def_id).predicates;

// Warning: these where clauses are not substituted for bound vars yet,
// so that we don't need to adjust binders in the `FromEnv` rules below
Expand Down Expand Up @@ -319,7 +319,7 @@ fn program_clauses_for_impl(tcx: TyCtxt<'tcx>, def_id: DefId) -> Clauses<'tcx> {
let trait_pred = ty::TraitPredicate { trait_ref }.lower();

// `WC`
let predicates = &tcx.predicates_of(def_id).predicates;
let predicates = tcx.predicates_of(def_id).predicates;
let where_clauses = predicates
.iter()
.map(|(wc, _)| wc.lower())
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ pub trait AstConv<'tcx> {
/// but this can lead to cycle errors. The problem is that we have
/// to do this resolution *in order to create the predicates in
/// the first place*. Hence, we have this "special pass".
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
-> &'tcx ty::GenericPredicates<'tcx>;
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId) -> ty::GenericPredicates<'tcx>;

/// Returns the lifetime to use when a lifetime is omitted (and not elided).
fn re_infer(
Expand Down
Loading