@@ -148,7 +148,7 @@ pub struct ParticipationCache {
148148}
149149
150150impl ParticipationCache {
151- /// Instantiate `Self`, returning a cache that is fully initialized and ready-to-go .
151+ /// Instantiate `Self`, returning a fully initialized cache .
152152 ///
153153 /// ## Errors
154154 ///
@@ -167,11 +167,20 @@ impl ParticipationCache {
167167 . get_cached_active_validator_indices ( RelativeEpoch :: Current ) ?
168168 . len ( ) ;
169169
170+ // Both the current/previous epoch participations are set to a capacity that is slightly
171+ // larger than required. The difference will be due slashed-but-active validators.
170172 let mut current_epoch_participation =
171173 SingleEpochParticipationCache :: new ( num_current_epoch_active_vals, spec) ;
172174 let mut previous_epoch_participation =
173175 SingleEpochParticipationCache :: new ( num_previous_epoch_active_vals, spec) ;
174- let mut eligible_indices = Vec :: with_capacity ( num_previous_epoch_active_vals) ;
176+ // Contains the set of validators which are:
177+ //
178+ // - Active in the previous epoch.
179+ // - Slashed, but not yet withdrawable.
180+ //
181+ // Using the full length of `state.validators` is almost always overkill, but it ensures no
182+ // reallocations.
183+ let mut eligible_indices = Vec :: with_capacity ( state. validators ( ) . len ( ) ) ;
175184
176185 for ( val_index, val) in state. validators ( ) . iter ( ) . enumerate ( ) {
177186 if val. is_active_at ( current_epoch) {
@@ -195,6 +204,14 @@ impl ParticipationCache {
195204 }
196205 }
197206
207+ eligible_indices. shrink_to_fit ( ) ;
208+ current_epoch_participation
209+ . unslashed_participating_indices
210+ . shrink_to_fit ( ) ;
211+ previous_epoch_participation
212+ . unslashed_participating_indices
213+ . shrink_to_fit ( ) ;
214+
198215 Ok ( Self {
199216 current_epoch,
200217 current_epoch_participation,
0 commit comments