Skip to content

Commit 98dab33

Browse files
committed
Wrap some query results in Lrc.
So that the frequent clones in `try_get` are cheaper. Fixes #54274.
1 parent 5c9f7dc commit 98dab33

File tree

14 files changed

+73
-69
lines changed

14 files changed

+73
-69
lines changed

src/librustc/infer/outlives/verify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ impl<'cx, 'gcx, 'tcx> VerifyBoundCx<'cx, 'gcx, 'tcx> {
299299
let assoc_item = tcx.associated_item(assoc_item_def_id);
300300
let trait_def_id = assoc_item.container.assert_trait();
301301
let trait_predicates = tcx.predicates_of(trait_def_id).predicates
302-
.into_iter()
303-
.map(|(p, _)| p)
302+
.iter()
303+
.map(|(p, _)| *p)
304304
.collect();
305305
let identity_substs = Substs::identity_for_item(tcx, assoc_item_def_id);
306306
let identity_proj = tcx.mk_projection(assoc_item_def_id, identity_substs);

src/librustc/traits/object_safety.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
182182
};
183183
predicates
184184
.predicates
185-
.into_iter()
185+
.iter()
186186
.map(|(predicate, _)| predicate.subst_supertrait(self, &trait_ref))
187187
.any(|predicate| {
188188
match predicate {
@@ -302,9 +302,10 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
302302
return Some(MethodViolationCode::Generic);
303303
}
304304

305-
if self.predicates_of(method.def_id).predicates.into_iter()
305+
if self.predicates_of(method.def_id).predicates.iter()
306306
// A trait object can't claim to live more than the concrete type,
307307
// so outlives predicates will always hold.
308+
.cloned()
308309
.filter(|(p, _)| p.to_opt_type_outlives().is_none())
309310
.collect::<Vec<_>>()
310311
// Do a shallow visit so that `contains_illegal_self_type_reference`

src/librustc/traits/specialize/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_, '_, '_>, impl_def_id: DefId) -> Option<
407407

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

src/librustc/ty/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
21262126
}
21272127

21282128
#[inline]
2129-
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> GenericPredicates<'gcx> {
2129+
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Lrc<GenericPredicates<'gcx>> {
21302130
tcx.predicates_of(self.did)
21312131
}
21322132

@@ -2369,8 +2369,8 @@ impl<'a, 'gcx, 'tcx> AdtDef {
23692369
def_id: sized_trait,
23702370
substs: tcx.mk_substs_trait(ty, &[])
23712371
}).to_predicate();
2372-
let predicates = tcx.predicates_of(self.did).predicates;
2373-
if predicates.into_iter().any(|(p, _)| p == sized_predicate) {
2372+
let predicates = &tcx.predicates_of(self.did).predicates;
2373+
if predicates.iter().any(|(p, _)| *p == sized_predicate) {
23742374
vec![]
23752375
} else {
23762376
vec![ty]

src/librustc/ty/query/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,18 @@ define_queries! { <'tcx>
127127
/// predicate gets in the way of some checks, which are intended
128128
/// to operate over only the actual where-clauses written by the
129129
/// user.)
130-
[] fn predicates_of: PredicatesOfItem(DefId) -> ty::GenericPredicates<'tcx>,
130+
[] fn predicates_of: PredicatesOfItem(DefId) -> Lrc<ty::GenericPredicates<'tcx>>,
131131

132132
/// Maps from the def-id of an item (trait/struct/enum/fn) to the
133133
/// predicates (where clauses) directly defined on it. This is
134134
/// equal to the `explicit_predicates_of` predicates plus the
135135
/// `inferred_outlives_of` predicates.
136-
[] fn predicates_defined_on: PredicatesDefinedOnItem(DefId) -> ty::GenericPredicates<'tcx>,
136+
[] fn predicates_defined_on: PredicatesDefinedOnItem(DefId)
137+
-> Lrc<ty::GenericPredicates<'tcx>>,
137138

138139
/// Returns the predicates written explicit by the user.
139140
[] fn explicit_predicates_of: ExplicitPredicatesOfItem(DefId)
140-
-> ty::GenericPredicates<'tcx>,
141+
-> Lrc<ty::GenericPredicates<'tcx>>,
141142

142143
/// Returns the inferred outlives predicates (e.g., for `struct
143144
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
@@ -149,12 +150,12 @@ define_queries! { <'tcx>
149150
/// evaluate them even during type conversion, often before the
150151
/// full predicates are available (note that supertraits have
151152
/// additional acyclicity requirements).
152-
[] fn super_predicates_of: SuperPredicatesOfItem(DefId) -> ty::GenericPredicates<'tcx>,
153+
[] fn super_predicates_of: SuperPredicatesOfItem(DefId) -> Lrc<ty::GenericPredicates<'tcx>>,
153154

154155
/// To avoid cycles within the predicates of a single item we compute
155156
/// per-type-parameter predicates for resolving `T::AssocTy`.
156157
[] fn type_param_predicates: type_param_predicates((DefId, DefId))
157-
-> ty::GenericPredicates<'tcx>,
158+
-> Lrc<ty::GenericPredicates<'tcx>>,
158159

159160
[] fn trait_def: TraitDefOfItem(DefId) -> &'tcx ty::TraitDef,
160161
[] fn adt_def: AdtDefOfItem(DefId) -> &'tcx ty::AdtDef,

src/librustc_metadata/cstore_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
103103
generics_of => {
104104
tcx.alloc_generics(cdata.get_generics(def_id.index, tcx.sess))
105105
}
106-
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
107-
predicates_defined_on => { cdata.get_predicates_defined_on(def_id.index, tcx) }
108-
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
106+
predicates_of => { Lrc::new(cdata.get_predicates(def_id.index, tcx)) }
107+
predicates_defined_on => { Lrc::new(cdata.get_predicates_defined_on(def_id.index, tcx)) }
108+
super_predicates_of => { Lrc::new(cdata.get_super_predicates(def_id.index, tcx)) }
109109
trait_def => {
110110
tcx.alloc_trait_def(cdata.get_trait_def(def_id.index, tcx.sess))
111111
}

src/librustc_traits/lowering/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ fn program_clauses_for_trait<'a, 'tcx>(
217217

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

220-
let where_clauses = &tcx.predicates_defined_on(def_id).predicates
221-
.into_iter()
220+
let predicates = &tcx.predicates_defined_on(def_id).predicates;
221+
let where_clauses = &predicates
222+
.iter()
222223
.map(|(wc, _)| wc.lower())
223224
.map(|wc| wc.subst(tcx, bound_vars))
224225
.collect::<Vec<_>>();
@@ -314,8 +315,9 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
314315
let trait_pred = ty::TraitPredicate { trait_ref }.lower();
315316

316317
// `WC`
317-
let where_clauses = tcx.predicates_of(def_id).predicates
318-
.into_iter()
318+
let predicates = &tcx.predicates_of(def_id).predicates;
319+
let where_clauses = predicates
320+
.iter()
319321
.map(|(wc, _)| wc.lower())
320322
.map(|wc| wc.subst(tcx, bound_vars));
321323

@@ -352,7 +354,7 @@ pub fn program_clauses_for_type_def<'a, 'tcx>(
352354

353355
// `WC`
354356
let where_clauses = tcx.predicates_of(def_id).predicates
355-
.into_iter()
357+
.iter()
356358
.map(|(wc, _)| wc.lower())
357359
.map(|wc| wc.subst(tcx, bound_vars))
358360
.collect::<Vec<_>>();

src/librustc_typeck/astconv.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc::traits;
2424
use rustc::ty::{self, Ty, TyCtxt, ToPredicate, TypeFoldable};
2525
use rustc::ty::{GenericParamDef, GenericParamDefKind};
2626
use rustc::ty::wf::object_region_bounds;
27+
use rustc_data_structures::sync::Lrc;
2728
use rustc_target::spec::abi;
2829
use std::collections::BTreeSet;
2930
use std::slice;
@@ -45,7 +46,7 @@ pub trait AstConv<'gcx, 'tcx> {
4546
/// Returns the set of bounds in scope for the type parameter with
4647
/// the given id.
4748
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
48-
-> ty::GenericPredicates<'tcx>;
49+
-> Lrc<ty::GenericPredicates<'tcx>>;
4950

5051
/// What lifetime should we use when a lifetime is omitted (and not elided)?
5152
fn re_infer(&self, span: Span, _def: Option<&ty::GenericParamDef>)
@@ -1119,8 +1120,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
11191120
{
11201121
let tcx = self.tcx();
11211122

1122-
let bounds = self.get_type_parameter_bounds(span, ty_param_def_id)
1123-
.predicates.into_iter().filter_map(|(p, _)| p.to_opt_poly_trait_ref());
1123+
let predicates = &self.get_type_parameter_bounds(span, ty_param_def_id).predicates;
1124+
let bounds = predicates.iter().filter_map(|(p, _)| p.to_opt_poly_trait_ref());
11241125

11251126
// Check that there is exactly one way to find an associated type with the
11261127
// correct name.

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,15 +1869,15 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
18691869
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
18701870

18711871
fn get_type_parameter_bounds(&self, _: Span, def_id: DefId)
1872-
-> ty::GenericPredicates<'tcx>
1872+
-> Lrc<ty::GenericPredicates<'tcx>>
18731873
{
18741874
let tcx = self.tcx;
18751875
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
18761876
let item_id = tcx.hir.ty_param_owner(node_id);
18771877
let item_def_id = tcx.hir.local_def_id(item_id);
18781878
let generics = tcx.generics_of(item_def_id);
18791879
let index = generics.param_def_id_to_index[&def_id];
1880-
ty::GenericPredicates {
1880+
Lrc::new(ty::GenericPredicates {
18811881
parent: None,
18821882
predicates: self.param_env.caller_bounds.iter().filter_map(|&predicate| {
18831883
match predicate {
@@ -1890,7 +1890,7 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
18901890
_ => None
18911891
}
18921892
}).collect()
1893-
}
1893+
})
18941894
}
18951895

18961896
fn re_infer(&self, span: Span, def: Option<&ty::GenericParamDef>)

src/librustc_typeck/check/wfcheck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,8 @@ fn check_false_global_bounds<'a, 'gcx, 'tcx>(
910910

911911
let def_id = fcx.tcx.hir.local_def_id(id);
912912
let predicates = fcx.tcx.predicates_of(def_id).predicates
913-
.into_iter()
914-
.map(|(p, _)| p)
913+
.iter()
914+
.map(|(p, _)| *p)
915915
.collect();
916916
// Check elaborated bounds
917917
let implied_obligations = traits::elaborate_predicates(fcx.tcx, predicates);

0 commit comments

Comments
 (0)