Skip to content

Commit 410b529

Browse files
committed
Creating the vector using with_capacity to avoid re-allocation later on (#56905)
1 parent 47db51e commit 410b529

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/librustc_typeck/check/upvar.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
122122
};
123123

124124
self.tcx.with_freevars(closure_node_id, |freevars| {
125-
let mut freevar_list: Vec<ty::UpvarId> = Vec::new();
125+
let mut freevar_list: Vec<ty::UpvarId> = Vec::with_capacity(freevars.len());
126126
for freevar in freevars {
127127
let upvar_id = ty::UpvarId {
128128
var_path: ty::UpvarPath {
@@ -131,6 +131,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
131131
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
132132
};
133133
debug!("seed upvar_id {:?}", upvar_id);
134+
// Adding the upvar Id to the list of Upvars, which will be added
135+
// to the map for the closure at the end of the for loop.
134136
freevar_list.push(upvar_id);
135137

136138
let capture_kind = match capture_clause {
@@ -151,6 +153,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
151153
.upvar_capture_map
152154
.insert(upvar_id, capture_kind);
153155
}
156+
// Add the vector of freevars to the map keyed with the closure id.
157+
// This gives us an easier access to them without having to call
158+
// with_freevars again..
154159
self.tables
155160
.borrow_mut()
156161
.upvar_list

0 commit comments

Comments
 (0)