Skip to content

Commit 074c668

Browse files
committed
Auto merge of rust-lang#15543 - Veykril:less-alloc, r=Veykril
Remove some allocations in borrowck
2 parents ce650ce + 70e21dc commit 074c668

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

crates/hir-ty/src/mir/borrowck.rs

+40-37
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,27 @@ pub struct BorrowckResult {
4242
fn all_mir_bodies(
4343
db: &dyn HirDatabase,
4444
def: DefWithBodyId,
45-
) -> Box<dyn Iterator<Item = Result<Arc<MirBody>, MirLowerError>> + '_> {
45+
mut cb: impl FnMut(Arc<MirBody>),
46+
) -> Result<(), MirLowerError> {
4647
fn for_closure(
4748
db: &dyn HirDatabase,
4849
c: ClosureId,
49-
) -> Box<dyn Iterator<Item = Result<Arc<MirBody>, MirLowerError>> + '_> {
50+
cb: &mut impl FnMut(Arc<MirBody>),
51+
) -> Result<(), MirLowerError> {
5052
match db.mir_body_for_closure(c) {
5153
Ok(body) => {
52-
let closures = body.closures.clone();
53-
Box::new(
54-
iter::once(Ok(body))
55-
.chain(closures.into_iter().flat_map(|it| for_closure(db, it))),
56-
)
54+
cb(body.clone());
55+
body.closures.iter().map(|&it| for_closure(db, it, cb)).collect()
5756
}
58-
Err(e) => Box::new(iter::once(Err(e))),
57+
Err(e) => Err(e),
5958
}
6059
}
6160
match db.mir_body(def) {
6261
Ok(body) => {
63-
let closures = body.closures.clone();
64-
Box::new(
65-
iter::once(Ok(body)).chain(closures.into_iter().flat_map(|it| for_closure(db, it))),
66-
)
62+
cb(body.clone());
63+
body.closures.iter().map(|&it| for_closure(db, it, &mut cb)).collect()
6764
}
68-
Err(e) => Box::new(iter::once(Err(e))),
65+
Err(e) => Err(e),
6966
}
7067
}
7168

@@ -74,17 +71,15 @@ pub fn borrowck_query(
7471
def: DefWithBodyId,
7572
) -> Result<Arc<[BorrowckResult]>, MirLowerError> {
7673
let _p = profile::span("borrowck_query");
77-
let r = all_mir_bodies(db, def)
78-
.map(|body| {
79-
let body = body?;
80-
Ok(BorrowckResult {
81-
mutability_of_locals: mutability_of_locals(db, &body),
82-
moved_out_of_ref: moved_out_of_ref(db, &body),
83-
mir_body: body,
84-
})
85-
})
86-
.collect::<Result<Vec<_>, MirLowerError>>()?;
87-
Ok(r.into())
74+
let mut res = vec![];
75+
all_mir_bodies(db, def, |body| {
76+
res.push(BorrowckResult {
77+
mutability_of_locals: mutability_of_locals(db, &body),
78+
moved_out_of_ref: moved_out_of_ref(db, &body),
79+
mir_body: body,
80+
});
81+
})?;
82+
Ok(res.into())
8883
}
8984

9085
fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef> {
@@ -277,21 +272,35 @@ fn ever_initialized_map(
277272
);
278273
return;
279274
};
280-
let targets = match &terminator.kind {
281-
TerminatorKind::Goto { target } => vec![*target],
282-
TerminatorKind::SwitchInt { targets, .. } => targets.all_targets().to_vec(),
275+
let mut process = |target, is_ever_initialized| {
276+
if !result[target].contains_idx(l) || !result[target][l] && is_ever_initialized {
277+
result[target].insert(l, is_ever_initialized);
278+
dfs(db, body, target, l, result);
279+
}
280+
};
281+
match &terminator.kind {
282+
TerminatorKind::Goto { target } => process(*target, is_ever_initialized),
283+
TerminatorKind::SwitchInt { targets, .. } => {
284+
targets.all_targets().iter().for_each(|&it| process(it, is_ever_initialized));
285+
}
283286
TerminatorKind::UnwindResume
284287
| TerminatorKind::Abort
285288
| TerminatorKind::Return
286-
| TerminatorKind::Unreachable => vec![],
289+
| TerminatorKind::Unreachable => (),
287290
TerminatorKind::Call { target, cleanup, destination, .. } => {
288291
if destination.projection.len() == 0 && destination.local == l {
289292
is_ever_initialized = true;
290293
}
291-
target.into_iter().chain(cleanup.into_iter()).copied().collect()
294+
target
295+
.into_iter()
296+
.chain(cleanup.into_iter())
297+
.for_each(|&it| process(it, is_ever_initialized));
292298
}
293299
TerminatorKind::Drop { target, unwind, place: _ } => {
294-
Some(target).into_iter().chain(unwind.into_iter()).copied().collect()
300+
iter::once(target)
301+
.into_iter()
302+
.chain(unwind.into_iter())
303+
.for_each(|&it| process(it, is_ever_initialized));
295304
}
296305
TerminatorKind::DropAndReplace { .. }
297306
| TerminatorKind::Assert { .. }
@@ -300,13 +309,7 @@ fn ever_initialized_map(
300309
| TerminatorKind::FalseEdge { .. }
301310
| TerminatorKind::FalseUnwind { .. } => {
302311
never!("We don't emit these MIR terminators yet");
303-
vec![]
304-
}
305-
};
306-
for target in targets {
307-
if !result[target].contains_idx(l) || !result[target][l] && is_ever_initialized {
308-
result[target].insert(l, is_ever_initialized);
309-
dfs(db, body, target, l, result);
312+
()
310313
}
311314
}
312315
}

0 commit comments

Comments
 (0)