From e62ce98009fa4fc647220570ccb12927d3992ecb Mon Sep 17 00:00:00 2001
From: ljedrz <ljedrz@gmail.com>
Date: Wed, 17 Oct 2018 16:52:35 +0200
Subject: [PATCH] nll: improve allocations

---
 .../borrow_check/nll/constraint_generation.rs          |  5 ++++-
 .../borrow_check/nll/explain_borrow/mod.rs             | 10 ++++------
 .../nll/region_infer/error_reporting/region_name.rs    |  5 ++---
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/librustc_mir/borrow_check/nll/constraint_generation.rs b/src/librustc_mir/borrow_check/nll/constraint_generation.rs
index 30b263a923a..495e84528a3 100644
--- a/src/librustc_mir/borrow_check/nll/constraint_generation.rs
+++ b/src/librustc_mir/borrow_check/nll/constraint_generation.rs
@@ -141,6 +141,7 @@ impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx
         if let Some(all_facts) = self.all_facts {
             if let Place::Local(temp) = place {
                 if let Some(borrow_indices) = self.borrow_set.local_map.get(temp) {
+                    all_facts.killed.reserve(borrow_indices.len());
                     for &borrow_index in borrow_indices {
                         let location_index = self.location_table.mid_index(location);
                         all_facts.killed.push((borrow_index, location_index));
@@ -164,7 +165,9 @@ impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx
                 self.location_table.mid_index(location),
             ));
 
-            for successor_block in terminator.successors() {
+            let successor_blocks = terminator.successors();
+            all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
+            for successor_block in successor_blocks {
                 all_facts.cfg_edge.push((
                     self.location_table.mid_index(location),
                     self.location_table
diff --git a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
index 307112f8ba1..a0ccfb8dc4c 100644
--- a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
+++ b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
@@ -279,9 +279,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                         pending_locations.push(target.start_location());
                     },
                     TerminatorKind::SwitchInt { ref targets, .. } => {
-                        for target in targets {
-                            pending_locations.push(target.start_location());
-                        }
+                        pending_locations.extend(
+                            targets.into_iter().map(|target| target.start_location()));
                     },
                     TerminatorKind::Drop { target, unwind, .. } |
                     TerminatorKind::DropAndReplace { target, unwind, .. } |
@@ -303,9 +302,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                     },
                     TerminatorKind::FalseEdges { real_target, ref imaginary_targets, .. } => {
                         pending_locations.push(real_target.start_location());
-                        for target in imaginary_targets {
-                            pending_locations.push(target.start_location());
-                        }
+                        pending_locations.extend(
+                            imaginary_targets.into_iter().map(|target| target.start_location()));
                     },
                     _ => {},
                 }
diff --git a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs
index 65ba2f537bf..8d3cb727318 100644
--- a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs
+++ b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs
@@ -462,9 +462,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
         argument_hir_ty: &hir::Ty,
         counter: &mut usize,
     ) -> Option<RegionName> {
-        let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty)> = &mut Vec::new();
-
-        search_stack.push((argument_ty, argument_hir_ty));
+        let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty)> =
+            &mut vec![(argument_ty, argument_hir_ty)];
 
         while let Some((ty, hir_ty)) = search_stack.pop() {
             match (&ty.sty, &hir_ty.node) {