]> git.lizzy.rs Git - rust.git/commitdiff
rustc/infer: improve vector allocations
authorljedrz <ljedrz@gmail.com>
Fri, 28 Sep 2018 15:32:22 +0000 (17:32 +0200)
committerljedrz <ljedrz@gmail.com>
Sat, 29 Sep 2018 12:57:41 +0000 (14:57 +0200)
src/librustc/infer/canonical/query_result.rs
src/librustc/infer/opaque_types/mod.rs

index 810d3231acdcd397178a75e53de2d2b3b015afab..f70064e07162968d71c423b7f5c3800e740f0300 100644 (file)
@@ -310,16 +310,18 @@ pub fn instantiate_nll_query_result_and_region_obligations<R>(
         }
 
         // ...also include the other query region constraints from the query.
-        output_query_region_constraints.reserve(query_result.value.region_constraints.len());
-        for r_c in query_result.value.region_constraints.iter() {
-            let &ty::OutlivesPredicate(k1, r2) = r_c.skip_binder(); // reconstructed below
-            let k1 = substitute_value(self.tcx, &result_subst, &k1);
-            let r2 = substitute_value(self.tcx, &result_subst, &r2);
-            if k1 != r2.into() {
-                output_query_region_constraints
-                    .push(ty::Binder::bind(ty::OutlivesPredicate(k1, r2)));
-            }
-        }
+        output_query_region_constraints.extend(
+            query_result.value.region_constraints.iter().filter_map(|r_c| {
+                let &ty::OutlivesPredicate(k1, r2) = r_c.skip_binder(); // reconstructed below
+                let k1 = substitute_value(self.tcx, &result_subst, &k1);
+                let r2 = substitute_value(self.tcx, &result_subst, &r2);
+                if k1 != r2.into() {
+                    Some(ty::Binder::bind(ty::OutlivesPredicate(k1, r2)))
+                } else {
+                    None
+                }
+            })
+        );
 
         let user_result: R =
             query_result.substitute_projected(self.tcx, &result_subst, |q_r| &q_r.value);
@@ -576,31 +578,30 @@ pub fn make_query_outlives<'tcx>(
     assert!(verifys.is_empty());
     assert!(givens.is_empty());
 
-    let mut outlives: Vec<_> = constraints
-            .into_iter()
-            .map(|(k, _)| match *k {
-                // Swap regions because we are going from sub (<=) to outlives
-                // (>=).
-                Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
-                    tcx.mk_region(ty::ReVar(v2)).into(),
-                    tcx.mk_region(ty::ReVar(v1)),
-                ),
-                Constraint::VarSubReg(v1, r2) => {
-                    ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
-                }
-                Constraint::RegSubVar(r1, v2) => {
-                    ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
-                }
-                Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
-            })
-            .map(ty::Binder::dummy) // no bound regions in the code above
-            .collect();
-
-    outlives.extend(
-        outlives_obligations
-            .map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
-            .map(ty::Binder::dummy), // no bound regions in the code above
-    );
+    let outlives: Vec<_> = constraints
+        .into_iter()
+        .map(|(k, _)| match *k {
+            // Swap regions because we are going from sub (<=) to outlives
+            // (>=).
+            Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
+                tcx.mk_region(ty::ReVar(v2)).into(),
+                tcx.mk_region(ty::ReVar(v1)),
+            ),
+            Constraint::VarSubReg(v1, r2) => {
+                ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
+            }
+            Constraint::RegSubVar(r1, v2) => {
+                ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
+            }
+            Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
+        })
+        .map(ty::Binder::dummy) // no bound regions in the code above
+        .chain(
+            outlives_obligations
+                .map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
+                .map(ty::Binder::dummy), // no bound regions in the code above
+        )
+        .collect();
 
     outlives
 }
index 88d375742670d2e68dbfe0b2f4d44842328b18b6..d365992aef5006f15deb01212ae7ac7c80153e44 100644 (file)
@@ -803,6 +803,7 @@ fn fold_opaque_ty(
         );
         debug!("instantiate_opaque_types: ty_var={:?}", ty_var);
 
+        self.obligations.reserve(bounds.predicates.len());
         for predicate in bounds.predicates {
             // Change the predicate to refer to the type variable,
             // which will be the concrete type instead of the opaque type.