Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Reuse a Vec in mir simplification #68551

Merged
merged 6 commits into from
Mar 12, 2020
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/librustc_mir/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {

let mut start = START_BLOCK;

let mut merged_blocks = Vec::new();
loop {
let mut changed = false;

Expand All @@ -114,18 +115,28 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
self.collapse_goto_chain(successor, &mut changed);
}

let mut new_stmts = vec![];
let mut inner_changed = true;
while inner_changed {
inner_changed = false;
inner_changed |= self.simplify_branch(&mut terminator);
inner_changed |= self.merge_successor(&mut new_stmts, &mut terminator);
inner_changed |= self.merge_successor(&mut merged_blocks, &mut terminator);
changed |= inner_changed;
}

let data = &mut self.basic_blocks[bb];
data.statements.extend(new_stmts);
data.terminator = Some(terminator);
let merged_block_count =
ecstatic-morse marked this conversation as resolved.
Show resolved Hide resolved
merged_blocks.iter().map(|&i| self.basic_blocks[i].statements.len()).sum();

if merged_block_count > 0 {
let mut statements = std::mem::take(&mut self.basic_blocks[bb].statements);
statements.reserve(merged_block_count);
for &from in &merged_blocks {
statements.append(&mut self.basic_blocks[from].statements);
}
self.basic_blocks[bb].statements = statements;
}
merged_blocks.clear();
Marwes marked this conversation as resolved.
Show resolved Hide resolved

self.basic_blocks[bb].terminator = Some(terminator);

changed |= inner_changed;
}
Expand Down Expand Up @@ -199,7 +210,7 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
// merge a block with 1 `goto` predecessor to its parent
fn merge_successor(
&mut self,
new_stmts: &mut Vec<Statement<'tcx>>,
merged_blocks: &mut Vec<BasicBlock>,
terminator: &mut Terminator<'tcx>,
) -> bool {
let target = match terminator.kind {
Expand All @@ -216,7 +227,8 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
return false;
}
};
new_stmts.extend(self.basic_blocks[target].statements.drain(..));

merged_blocks.push(target);
self.pred_count[target] = 0;

true
Expand Down