Skip to content

Commit e9232b2

Browse files
committed
Remove some Vec allocations in an effort to improve perf
1 parent dbf8b6b commit e9232b2

File tree

14 files changed

+38
-54
lines changed

14 files changed

+38
-54
lines changed

src/librustc_infer/infer/outlives/verify.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
223223
// like `T` and `T::Item`. It may not work as well for things
224224
// like `<T as Foo<'a>>::Item`.
225225
let c_b = self.param_env.caller_bounds;
226-
let param_bounds = self.collect_outlives_from_predicate_list(&compare_ty, c_b);
226+
let param_bounds = self.collect_outlives_from_predicate_list(&compare_ty, c_b.into_iter());
227227

228228
// Next, collect regions we scraped from the well-formedness
229229
// constraints in the fn signature. To do that, we walk the list
@@ -315,15 +315,12 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
315315
let tcx = self.tcx;
316316
let assoc_item = tcx.associated_item(assoc_item_def_id);
317317
let trait_def_id = assoc_item.container.assert_trait();
318-
let trait_predicates =
319-
tcx.predicates_of(trait_def_id).predicates.iter().map(|(p, _)| *p).collect();
318+
let trait_predicates = tcx.predicates_of(trait_def_id).predicates.iter().map(|(p, _)| *p);
320319
let identity_substs = InternalSubsts::identity_for_item(tcx, assoc_item_def_id);
321320
let identity_proj = tcx.mk_projection(assoc_item_def_id, identity_substs);
322321
self.collect_outlives_from_predicate_list(
323322
move |ty| ty == identity_proj,
324-
traits::elaborate_predicates(tcx, trait_predicates)
325-
.map(|o| o.predicate)
326-
.collect::<Vec<_>>(),
323+
traits::elaborate_predicates(tcx, trait_predicates).map(|o| o.predicate),
327324
)
328325
.map(|b| b.1)
329326
}
@@ -337,10 +334,9 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
337334
fn collect_outlives_from_predicate_list(
338335
&self,
339336
compare_ty: impl Fn(Ty<'tcx>) -> bool,
340-
predicates: impl IntoIterator<Item = impl AsRef<ty::Predicate<'tcx>>>,
337+
predicates: impl Iterator<Item = impl AsRef<ty::Predicate<'tcx>>>,
341338
) -> impl Iterator<Item = ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>> {
342339
predicates
343-
.into_iter()
344340
.filter_map(|p| p.as_ref().to_opt_type_outlives())
345341
.filter_map(|p| p.no_bound_vars())
346342
.filter(move |p| compare_ty(p.0))

src/librustc_infer/traits/util.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,22 @@ pub fn elaborate_trait_ref<'tcx>(
9797
tcx: TyCtxt<'tcx>,
9898
trait_ref: ty::PolyTraitRef<'tcx>,
9999
) -> Elaborator<'tcx> {
100-
elaborate_predicates(tcx, vec![trait_ref.without_const().to_predicate()])
100+
elaborate_predicates(tcx, std::iter::once(trait_ref.without_const().to_predicate()))
101101
}
102102

103103
pub fn elaborate_trait_refs<'tcx>(
104104
tcx: TyCtxt<'tcx>,
105105
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
106106
) -> Elaborator<'tcx> {
107-
let predicates = trait_refs.map(|trait_ref| trait_ref.without_const().to_predicate()).collect();
107+
let predicates = trait_refs.map(|trait_ref| trait_ref.without_const().to_predicate());
108108
elaborate_predicates(tcx, predicates)
109109
}
110110

111111
pub fn elaborate_predicates<'tcx>(
112112
tcx: TyCtxt<'tcx>,
113-
mut predicates: Vec<ty::Predicate<'tcx>>,
113+
predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
114114
) -> Elaborator<'tcx> {
115-
let mut visited = PredicateSet::new(tcx);
116-
predicates.retain(|pred| visited.insert(pred));
117-
let obligations: Vec<_> =
115+
let obligations =
118116
predicates.into_iter().map(|predicate| predicate_obligation(predicate, None)).collect();
119117
elaborate_obligations(tcx, obligations)
120118
}

src/librustc_mir/transform/const_prop.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
122122
.predicates_of(source.def_id())
123123
.predicates
124124
.iter()
125-
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None })
126-
.collect();
125+
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
127126
if !traits::normalize_and_test_predicates(
128127
tcx,
129128
traits::elaborate_predicates(tcx, predicates).map(|o| o.predicate).collect(),

src/librustc_trait_selection/opaque_types.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
418418
let opaque_type = tcx.mk_opaque(def_id, opaque_defn.substs);
419419

420420
let required_region_bounds =
421-
required_region_bounds(tcx, opaque_type, bounds.predicates);
421+
required_region_bounds(tcx, opaque_type, bounds.predicates.into_iter());
422422
debug_assert!(!required_region_bounds.is_empty());
423423

424424
for required_region in required_region_bounds {
@@ -1127,7 +1127,8 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
11271127

11281128
debug!("instantiate_opaque_types: bounds={:?}", bounds);
11291129

1130-
let required_region_bounds = required_region_bounds(tcx, ty, bounds.predicates.clone());
1130+
let required_region_bounds =
1131+
required_region_bounds(tcx, ty, bounds.predicates.iter().cloned());
11311132
debug!("instantiate_opaque_types: required_region_bounds={:?}", required_region_bounds);
11321133

11331134
// Make sure that we are in fact defining the *entire* type
@@ -1245,17 +1246,15 @@ pub fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: DefId, opaque_hir_id: hir
12451246
crate fn required_region_bounds(
12461247
tcx: TyCtxt<'tcx>,
12471248
erased_self_ty: Ty<'tcx>,
1248-
predicates: Vec<ty::Predicate<'tcx>>,
1249+
predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
12491250
) -> Vec<ty::Region<'tcx>> {
1250-
debug!(
1251-
"required_region_bounds(erased_self_ty={:?}, predicates={:?})",
1252-
erased_self_ty, predicates
1253-
);
1251+
debug!("required_region_bounds(erased_self_ty={:?})", erased_self_ty);
12541252

12551253
assert!(!erased_self_ty.has_escaping_bound_vars());
12561254

12571255
traits::elaborate_predicates(tcx, predicates)
12581256
.filter_map(|obligation| {
1257+
debug!("required_region_bounds(obligation={:?})", obligation);
12591258
match obligation.predicate {
12601259
ty::Predicate::Projection(..)
12611260
| ty::Predicate::Trait(..)

src/librustc_trait_selection/traits/auto_trait.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ impl AutoTraitFinder<'tcx> {
360360

361361
computed_preds.extend(user_computed_preds.iter().cloned());
362362
let normalized_preds =
363-
elaborate_predicates(tcx, computed_preds.iter().cloned().collect())
364-
.map(|o| o.predicate);
363+
elaborate_predicates(tcx, computed_preds.iter().cloned()).map(|o| o.predicate);
365364
new_env =
366365
ty::ParamEnv::new(tcx.mk_predicates(normalized_preds), param_env.reveal, None);
367366
}

src/librustc_trait_selection/traits/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
10071007
}
10081008
};
10091009

1010-
for obligation in super::elaborate_predicates(self.tcx, vec![*cond]) {
1010+
for obligation in super::elaborate_predicates(self.tcx, std::iter::once(*cond)) {
10111011
if let ty::Predicate::Trait(implication, _) = obligation.predicate {
10121012
let error = error.to_poly_trait_ref();
10131013
let implication = implication.to_poly_trait_ref();

src/librustc_trait_selection/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub fn normalize_param_env_or_error<'tcx>(
297297
);
298298

299299
let mut predicates: Vec<_> =
300-
util::elaborate_predicates(tcx, unnormalized_env.caller_bounds.to_vec())
300+
util::elaborate_predicates(tcx, unnormalized_env.caller_bounds.into_iter().cloned())
301301
.map(|obligation| obligation.predicate)
302302
.collect();
303303

src/librustc_trait_selection/traits/object_safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
302302
// Search for a predicate like `Self : Sized` amongst the trait bounds.
303303
let predicates = tcx.predicates_of(def_id);
304304
let predicates = predicates.instantiate_identity(tcx).predicates;
305-
elaborate_predicates(tcx, predicates).any(|obligation| match obligation.predicate {
305+
elaborate_predicates(tcx, predicates.into_iter()).any(|obligation| match obligation.predicate {
306306
ty::Predicate::Trait(ref trait_pred, _) => {
307307
trait_pred.def_id() == sized_def_id && trait_pred.skip_binder().self_ty().is_param(0)
308308
}

src/librustc_trait_selection/traits/project.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ fn assemble_candidates_from_trait_def<'cx, 'tcx>(
900900
// If so, extract what we know from the trait and try to come up with a good answer.
901901
let trait_predicates = tcx.predicates_of(def_id);
902902
let bounds = trait_predicates.instantiate(tcx, substs);
903-
let bounds = elaborate_predicates(tcx, bounds.predicates).map(|o| o.predicate);
903+
let bounds = elaborate_predicates(tcx, bounds.predicates.into_iter()).map(|o| o.predicate);
904904
assemble_candidates_from_predicates(
905905
selcx,
906906
obligation,
@@ -911,16 +911,14 @@ fn assemble_candidates_from_trait_def<'cx, 'tcx>(
911911
)
912912
}
913913

914-
fn assemble_candidates_from_predicates<'cx, 'tcx, I>(
914+
fn assemble_candidates_from_predicates<'cx, 'tcx>(
915915
selcx: &mut SelectionContext<'cx, 'tcx>,
916916
obligation: &ProjectionTyObligation<'tcx>,
917917
obligation_trait_ref: &ty::TraitRef<'tcx>,
918918
candidate_set: &mut ProjectionTyCandidateSet<'tcx>,
919919
ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionTyCandidate<'tcx>,
920-
env_predicates: I,
921-
) where
922-
I: IntoIterator<Item = ty::Predicate<'tcx>>,
923-
{
920+
env_predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
921+
) {
924922
debug!("assemble_candidates_from_predicates(obligation={:?})", obligation);
925923
let infcx = selcx.infcx();
926924
for predicate in env_predicates {
@@ -1153,10 +1151,8 @@ fn confirm_object_candidate<'cx, 'tcx>(
11531151
object_ty
11541152
),
11551153
};
1156-
let env_predicates = data
1157-
.projection_bounds()
1158-
.map(|p| p.with_self_ty(selcx.tcx(), object_ty).to_predicate())
1159-
.collect();
1154+
let env_predicates =
1155+
data.projection_bounds().map(|p| p.with_self_ty(selcx.tcx(), object_ty).to_predicate());
11601156
let env_predicate = {
11611157
let env_predicates = elaborate_predicates(selcx.tcx(), env_predicates);
11621158

src/librustc_trait_selection/traits/select.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14431443
bounds
14441444
);
14451445

1446-
let elaborated_predicates = util::elaborate_predicates(self.tcx(), bounds.predicates);
1446+
let elaborated_predicates =
1447+
util::elaborate_predicates(self.tcx(), bounds.predicates.into_iter());
14471448
let matching_bound = elaborated_predicates.filter_to_traits().find(|bound| {
14481449
self.infcx.probe(|_| {
14491450
self.match_projection(

src/librustc_trait_selection/traits/wf.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,13 @@ pub fn object_region_bounds<'tcx>(
627627
// a placeholder type.
628628
let open_ty = tcx.mk_ty_infer(ty::FreshTy(0));
629629

630-
let predicates = existential_predicates
631-
.iter()
632-
.filter_map(|predicate| {
633-
if let ty::ExistentialPredicate::Projection(_) = *predicate.skip_binder() {
634-
None
635-
} else {
636-
Some(predicate.with_self_ty(tcx, open_ty))
637-
}
638-
})
639-
.collect();
630+
let predicates = existential_predicates.iter().filter_map(|predicate| {
631+
if let ty::ExistentialPredicate::Projection(_) = *predicate.skip_binder() {
632+
None
633+
} else {
634+
Some(predicate.with_self_ty(tcx, open_ty))
635+
}
636+
});
640637

641638
required_region_bounds(tcx, open_ty, predicates)
642639
}

src/librustc_typeck/check/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
572572
None => return None,
573573
};
574574

575-
traits::elaborate_predicates(self.tcx, predicates.predicates.clone())
575+
traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied())
576576
.filter_map(|obligation| match obligation.predicate {
577577
ty::Predicate::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => {
578578
let span = predicates

src/librustc_typeck/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, span: Span, id: hir::HirId) {
12251225
let empty_env = ty::ParamEnv::empty();
12261226

12271227
let def_id = fcx.tcx.hir().local_def_id(id);
1228-
let predicates = fcx.tcx.predicates_of(def_id).predicates.iter().map(|(p, _)| *p).collect();
1228+
let predicates = fcx.tcx.predicates_of(def_id).predicates.iter().map(|(p, _)| *p);
12291229
// Check elaborated bounds.
12301230
let implied_obligations = traits::elaborate_predicates(fcx.tcx, predicates);
12311231

src/librustc_typeck/impl_wf_check/min_specialization.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ fn check_predicates<'tcx>(
322322
// which is sound because we forbid impls like the following
323323
//
324324
// impl<D: Debug> AlwaysApplicable for D { }
325-
let always_applicable_traits: Vec<_> = impl1_predicates
325+
let always_applicable_traits = impl1_predicates
326326
.predicates
327327
.iter()
328328
.filter(|predicate| {
@@ -331,8 +331,7 @@ fn check_predicates<'tcx>(
331331
Some(TraitSpecializationKind::AlwaysApplicable)
332332
)
333333
})
334-
.copied()
335-
.collect();
334+
.copied();
336335

337336
// Include the well-formed predicates of the type parameters of the impl.
338337
for ty in tcx.impl_trait_ref(impl1_def_id).unwrap().substs.types() {

0 commit comments

Comments
 (0)