Skip to content

Commit

Permalink
Fix performance issue with record_operands_moved
Browse files Browse the repository at this point in the history
Large arrays/tuples can have enough operands that removing items one at
a time is significantly slower than creating a hash set first.
  • Loading branch information
matthewjasper committed Jun 14, 2024
1 parent 1297dd3 commit 016ce8f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions compiler/rustc_mir_build/src/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ that contains only loops and breakable blocks. It tracks where a `break`,
use std::mem;

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

// look for moves of a local variable, like `MOVE(_X)`
let locals_moved = operands.iter().flat_map(|operand| match operand {
Operand::Copy(_) | Operand::Constant(_) => None,
Operand::Move(place) => place.as_local(),
});
let locals_moved: FxHashSet<Local> = operands
.iter()
.flat_map(|operand| match operand {
Operand::Copy(_) | Operand::Constant(_) => None,
Operand::Move(place) => place.as_local(),
})
.collect();

for local in locals_moved {
// Unschedule drops from the scope.
scope.drops.retain(|drop| drop.local != local || drop.kind != DropKind::Value);
}
// Unschedule drops from the scope.
scope
.drops
.retain(|drop| drop.kind != DropKind::Value || !locals_moved.contains(&drop.local));
scope.invalidate_cache();
}

Expand Down

0 comments on commit 016ce8f

Please sign in to comment.