Skip to content

Commit 016ce8f

Browse files
committed
Fix performance issue with record_operands_moved
Large arrays/tuples can have enough operands that removing items one at a time is significantly slower than creating a hash set first.
1 parent 1297dd3 commit 016ce8f

File tree

1 file changed

+12
-9
lines changed
  • compiler/rustc_mir_build/src/build

1 file changed

+12
-9
lines changed

compiler/rustc_mir_build/src/build/scope.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ that contains only loops and breakable blocks. It tracks where a `break`,
8484
use std::mem;
8585

8686
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
87-
use rustc_data_structures::fx::FxHashMap;
87+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8888
use rustc_hir::HirId;
8989
use rustc_index::{IndexSlice, IndexVec};
9090
use rustc_middle::middle::region;
@@ -1110,15 +1110,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11101110
assert_eq!(scope.region_scope, local_scope, "local scope is not the topmost scope!",);
11111111

11121112
// look for moves of a local variable, like `MOVE(_X)`
1113-
let locals_moved = operands.iter().flat_map(|operand| match operand {
1114-
Operand::Copy(_) | Operand::Constant(_) => None,
1115-
Operand::Move(place) => place.as_local(),
1116-
});
1113+
let locals_moved: FxHashSet<Local> = operands
1114+
.iter()
1115+
.flat_map(|operand| match operand {
1116+
Operand::Copy(_) | Operand::Constant(_) => None,
1117+
Operand::Move(place) => place.as_local(),
1118+
})
1119+
.collect();
11171120

1118-
for local in locals_moved {
1119-
// Unschedule drops from the scope.
1120-
scope.drops.retain(|drop| drop.local != local || drop.kind != DropKind::Value);
1121-
}
1121+
// Unschedule drops from the scope.
1122+
scope
1123+
.drops
1124+
.retain(|drop| drop.kind != DropKind::Value || !locals_moved.contains(&drop.local));
11221125
scope.invalidate_cache();
11231126
}
11241127

0 commit comments

Comments
 (0)