From e7e3001557cc9a2e13bce599e374b474392eb779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 20 Apr 2020 10:42:12 -0700 Subject: [PATCH] Remove some allocations in predicate evaluation --- src/librustc_infer/traits/util.rs | 7 +++---- src/librustc_trait_selection/traits/auto_trait.rs | 12 +++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 8aacea735f476..55bea57f3e243 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -149,21 +149,20 @@ impl Elaborator<'tcx> { // Get predicates declared on the trait. let predicates = tcx.super_predicates_of(data.def_id()); - let obligations = predicates.predicates.iter().map(|(pred, span)| { + let obligations = predicates.predicates.into_iter().map(|(pred, span)| { predicate_obligation( pred.subst_supertrait(tcx, &data.to_poly_trait_ref()), Some(*span), ) }); - debug!("super_predicates: data={:?} predicates={:?}", data, &obligations); + debug!("super_predicates: data={:?}", data); // Only keep those bounds that we haven't already seen. // This is necessary to prevent infinite recursion in some // cases. One common case is when people define // `trait Sized: Sized { }` rather than `trait Sized { }`. let visited = &mut self.visited; - let obligations = - obligations.filter(|obligation| visited.insert(&obligation.predicate)); + let obligations = obligations.filter(|o| visited.insert(&o.predicate)); self.stack.extend(obligations); } diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index b28658e0f1e58..6326a87c5edc3 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -281,7 +281,7 @@ impl AutoTraitFinder<'tcx> { }, })); - let mut computed_preds: FxHashSet<_> = param_env.caller_bounds.iter().cloned().collect(); + let computed_preds = param_env.caller_bounds.iter().cloned(); let mut user_computed_preds: FxHashSet<_> = user_env.caller_bounds.iter().cloned().collect(); @@ -358,9 +358,11 @@ impl AutoTraitFinder<'tcx> { _ => panic!("Unexpected error for '{:?}': {:?}", ty, result), }; - computed_preds.extend(user_computed_preds.iter().cloned()); - let normalized_preds = - elaborate_predicates(tcx, computed_preds.iter().cloned()).map(|o| o.predicate); + let normalized_preds = elaborate_predicates( + tcx, + computed_preds.clone().chain(user_computed_preds.iter().cloned()), + ) + .map(|o| o.predicate); new_env = ty::ParamEnv::new(tcx.mk_predicates(normalized_preds), param_env.reveal, None); } @@ -738,7 +740,7 @@ impl AutoTraitFinder<'tcx> { if p.ty().skip_binder().has_infer_types() { if !self.evaluate_nested_obligations( ty, - v.clone().iter().cloned(), + v.into_iter(), computed_preds, fresh_preds, predicates,