Skip to content

Commit 70940ca

Browse files
committed
Reduce allocations in GVN.
1 parent fe3fffa commit 70940ca

File tree

1 file changed

+12
-4
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+12
-4
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
126126
// Clone dominators as we need them while mutating the body.
127127
let dominators = body.basic_blocks.dominators().clone();
128128

129-
let mut state = VnState::new(tcx, param_env, &ssa, &dominators, &body.local_decls);
129+
let mut state = VnState::new(tcx, body, param_env, &ssa, &dominators, &body.local_decls);
130130
ssa.for_each_assignment_mut(
131131
body.basic_blocks.as_mut_preserves_cfg(),
132132
|local, value, location| {
@@ -264,20 +264,28 @@ struct VnState<'body, 'tcx> {
264264
impl<'body, 'tcx> VnState<'body, 'tcx> {
265265
fn new(
266266
tcx: TyCtxt<'tcx>,
267+
body: &Body<'tcx>,
267268
param_env: ty::ParamEnv<'tcx>,
268269
ssa: &'body SsaLocals,
269270
dominators: &'body Dominators<BasicBlock>,
270271
local_decls: &'body LocalDecls<'tcx>,
271272
) -> Self {
273+
// Compute a rough estimate of the number of values in the body from the number of
274+
// statements. This is meant to reduce the number of allocations, but it's all right if
275+
// we miss the exact amount. We estimate based on 2 values per statement (one in LHS and
276+
// one in RHS) and 4 values per terminator (for call operands).
277+
let num_values =
278+
2 * body.basic_blocks.iter().map(|bbdata| bbdata.statements.len()).sum::<usize>()
279+
+ 4 * body.basic_blocks.len();
272280
VnState {
273281
tcx,
274282
ecx: InterpCx::new(tcx, DUMMY_SP, param_env, DummyMachine),
275283
param_env,
276284
local_decls,
277285
locals: IndexVec::from_elem(None, local_decls),
278-
rev_locals: IndexVec::default(),
279-
values: FxIndexSet::default(),
280-
evaluated: IndexVec::new(),
286+
rev_locals: IndexVec::with_capacity(num_values),
287+
values: FxIndexSet::with_capacity_and_hasher(num_values, Default::default()),
288+
evaluated: IndexVec::with_capacity(num_values),
281289
next_opaque: Some(0),
282290
feature_unsized_locals: tcx.features().unsized_locals,
283291
ssa,

0 commit comments

Comments
 (0)