Skip to content

Commit b122908

Browse files
committed
Auto merge of #81470 - tmiasko:remove-allocations, r=matthewjasper
Avoid memory allocation when removing dead blocks Use `reachable_as_bitset` to reuse a bitset from the traversal rather than allocating it seprately. Additionally check if there are any unreachable blocks before proceeding.
2 parents c4e33b5 + dc6ec35 commit b122908

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

compiler/rustc_mir/src/transform/simplify.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
//! return.
2929
3030
use crate::transform::MirPass;
31-
use rustc_index::bit_set::BitSet;
3231
use rustc_index::vec::{Idx, IndexVec};
3332
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
3433
use rustc_middle::mir::*;
@@ -288,17 +287,17 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
288287
}
289288

290289
pub fn remove_dead_blocks(body: &mut Body<'_>) {
291-
let mut seen = BitSet::new_empty(body.basic_blocks().len());
292-
for (bb, _) in traversal::preorder(body) {
293-
seen.insert(bb.index());
290+
let reachable = traversal::reachable_as_bitset(body);
291+
let num_blocks = body.basic_blocks().len();
292+
if num_blocks == reachable.count() {
293+
return;
294294
}
295295

296296
let basic_blocks = body.basic_blocks_mut();
297-
298-
let num_blocks = basic_blocks.len();
299297
let mut replacements: Vec<_> = (0..num_blocks).map(BasicBlock::new).collect();
300298
let mut used_blocks = 0;
301-
for alive_index in seen.iter() {
299+
for alive_index in reachable.iter() {
300+
let alive_index = alive_index.index();
302301
replacements[alive_index] = BasicBlock::new(used_blocks);
303302
if alive_index != used_blocks {
304303
// Swap the next alive block data with the current available slot. Since

0 commit comments

Comments
 (0)