Skip to content

Commit fbeaa76

Browse files
Rollup merge of #165913 - tmiasko:patch, r=dianqk
MirPatch: reduce new statement insertion complexity The previous implementation inserted each new statement in a block separately, which in the worst case moved all existing statements each time. Use a simple merging strategy to improve complexity from O(num existing statements × num new statements) to O(num existing statements + num new statements). Helps with #121354.
2 parents 65ce861 + b7ed414 commit fbeaa76

1 file changed

Lines changed: 48 additions & 13 deletions

File tree

  • compiler/rustc_mir_transform/src

‎compiler/rustc_mir_transform/src/patch.rs‎

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -289,20 +289,30 @@ impl<'tcx> MirPatch<'tcx> {
289289
// comment for `add_statement`.
290290
new_statements.sort_by_key(|s| s.0);
291291

292-
let mut delta = 0;
293-
let mut last_bb = START_BLOCK;
294-
for (mut loc, stmt) in new_statements {
295-
if loc.block != last_bb {
296-
delta = 0;
297-
last_bb = loc.block;
292+
while !new_statements.is_empty() {
293+
// Identify a subslice of statements to be inserted to the same block: new_statements[i..].
294+
let mut i = new_statements.len() - 1;
295+
let block = new_statements[i].0.block;
296+
while i != 0 && new_statements[i - 1].0.block == block {
297+
i -= 1;
298+
}
299+
if i + 1 == new_statements.len() {
300+
let (loc, s) = new_statements.pop().unwrap();
301+
let source_info = Self::source_info_for_index(&bbs[block], loc);
302+
bbs[block].statements.insert(loc.statement_index, Statement::new(source_info, s));
303+
} else {
304+
let new: Vec<(usize, Statement<'tcx>)> = new_statements
305+
.drain(i..)
306+
.enumerate()
307+
.map(|(idx, (loc, s))| {
308+
let source_info = Self::source_info_for_index(&bbs[block], loc);
309+
// Record the final statement index, after accounting for statements inserted before.
310+
(loc.statement_index + idx, Statement::new(source_info, s))
311+
})
312+
.collect();
313+
let old = std::mem::take(&mut bbs[block].statements);
314+
merge(old, new, &mut bbs[block].statements);
298315
}
299-
debug!("MirPatch: adding statement {:?} at loc {:?}+{}", stmt, loc, delta);
300-
loc.statement_index += delta;
301-
let source_info = Self::source_info_for_index(&bbs[loc.block], loc);
302-
bbs[loc.block]
303-
.statements
304-
.insert(loc.statement_index, Statement::new(source_info, stmt));
305-
delta += 1;
306316
}
307317

308318
// The order in which we patch terminators does not change the result.
@@ -329,3 +339,28 @@ impl<'tcx> MirPatch<'tcx> {
329339
Self::source_info_for_index(data, loc)
330340
}
331341
}
342+
343+
fn merge<'tcx>(
344+
xs: Vec<Statement<'tcx>>,
345+
ys: Vec<(usize, Statement<'tcx>)>,
346+
zs: &mut Vec<Statement<'tcx>>,
347+
) {
348+
zs.reserve(xs.len() + ys.len());
349+
let mut xs = xs.into_iter().peekable();
350+
let mut ys = ys.into_iter().peekable();
351+
352+
let mut zi = 0;
353+
while let (Some(_), Some((yi, _))) = (xs.peek(), ys.peek()) {
354+
if *yi != zi {
355+
zs.push(xs.next().unwrap());
356+
} else {
357+
zs.push(ys.next().unwrap().1);
358+
}
359+
zi += 1;
360+
}
361+
362+
zs.extend(xs);
363+
for (_, s) in ys {
364+
zs.push(s);
365+
}
366+
}

0 commit comments

Comments
 (0)