From d7519544694c1866ba4bf32cd307ed6f670e1267 Mon Sep 17 00:00:00 2001 From: Blitzerr Date: Tue, 18 Dec 2018 20:54:35 -0800 Subject: [PATCH] Creating the vector using with_capacity to avoid re-allocation later on (#56905) --- src/librustc_typeck/check/upvar.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs index b5647045fe925..a603eb2575466 100644 --- a/src/librustc_typeck/check/upvar.rs +++ b/src/librustc_typeck/check/upvar.rs @@ -132,7 +132,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { }; self.tcx.with_freevars(closure_node_id, |freevars| { - let mut freevar_list: Vec = Vec::new(); + let mut freevar_list: Vec = Vec::with_capacity(freevars.len()); for freevar in freevars { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { @@ -141,6 +141,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { closure_expr_id: LocalDefId::from_def_id(closure_def_id), }; debug!("seed upvar_id {:?}", upvar_id); + // Adding the upvar Id to the list of Upvars, which will be added + // to the map for the closure at the end of the for loop. freevar_list.push(upvar_id); let capture_kind = match capture_clause { @@ -161,6 +163,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { .upvar_capture_map .insert(upvar_id, capture_kind); } + // Add the vector of freevars to the map keyed with the closure id. + // This gives us an easier access to them without having to call + // with_freevars again.. self.tables .borrow_mut() .upvar_list