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