@@ -126,7 +126,7 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
126
126
// Clone dominators as we need them while mutating the body.
127
127
let dominators = body. basic_blocks . dominators ( ) . clone ( ) ;
128
128
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 ) ;
130
130
ssa. for_each_assignment_mut (
131
131
body. basic_blocks . as_mut_preserves_cfg ( ) ,
132
132
|local, value, location| {
@@ -264,20 +264,28 @@ struct VnState<'body, 'tcx> {
264
264
impl < ' body , ' tcx > VnState < ' body , ' tcx > {
265
265
fn new (
266
266
tcx : TyCtxt < ' tcx > ,
267
+ body : & Body < ' tcx > ,
267
268
param_env : ty:: ParamEnv < ' tcx > ,
268
269
ssa : & ' body SsaLocals ,
269
270
dominators : & ' body Dominators < BasicBlock > ,
270
271
local_decls : & ' body LocalDecls < ' tcx > ,
271
272
) -> 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 ( ) ;
272
280
VnState {
273
281
tcx,
274
282
ecx : InterpCx :: new ( tcx, DUMMY_SP , param_env, DummyMachine ) ,
275
283
param_env,
276
284
local_decls,
277
285
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 ) ,
281
289
next_opaque : Some ( 0 ) ,
282
290
feature_unsized_locals : tcx. features ( ) . unsized_locals ,
283
291
ssa,
0 commit comments