Skip to content

Commit d751954

Browse files
committed
Creating the vector using with_capacity to avoid re-allocation later on (#56905)
1 parent a2b6401 commit d751954

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/librustc_typeck/check/upvar.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
132132
};
133133

134134
self.tcx.with_freevars(closure_node_id, |freevars| {
135-
let mut freevar_list: Vec<ty::UpvarId> = Vec::new();
135+
let mut freevar_list: Vec<ty::UpvarId> = Vec::with_capacity(freevars.len());
136136
for freevar in freevars {
137137
let upvar_id = ty::UpvarId {
138138
var_path: ty::UpvarPath {
@@ -141,6 +141,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
141141
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
142142
};
143143
debug!("seed upvar_id {:?}", upvar_id);
144+
// Adding the upvar Id to the list of Upvars, which will be added
145+
// to the map for the closure at the end of the for loop.
144146
freevar_list.push(upvar_id);
145147

146148
let capture_kind = match capture_clause {
@@ -161,6 +163,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
161163
.upvar_capture_map
162164
.insert(upvar_id, capture_kind);
163165
}
166+
// Add the vector of freevars to the map keyed with the closure id.
167+
// This gives us an easier access to them without having to call
168+
// with_freevars again..
164169
self.tables
165170
.borrow_mut()
166171
.upvar_list

0 commit comments

Comments
 (0)