Skip to content

Commit 9d23c86

Browse files
committed
Reduce allocations in GVN.
1 parent 70ee6e4 commit 9d23c86

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

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

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

0 commit comments

Comments
 (0)