Avoid growing Vec for sync committee indices (#3301)

## Issue Addressed

NA

## Proposed Changes

This is a fairly simple micro-optimization to avoid using `Vec::grow`. I don't believe this will have a substantial effect on block processing times, however it was showing up in flamegraphs. I think it's worth making this change for general memory-hygiene.

## Additional Info

NA
This commit is contained in:
Paul Hauner 2022-07-01 03:44:37 +00:00
parent a7da0677d5
commit e5212f1320

View File

@ -779,14 +779,14 @@ impl<T: EthSpec> BeaconState<T> {
&mut self,
sync_committee: &SyncCommittee<T>,
) -> Result<Vec<usize>, Error> {
sync_committee
.pubkeys
.iter()
.map(|pubkey| {
let mut indices = Vec::with_capacity(sync_committee.pubkeys.len());
for pubkey in sync_committee.pubkeys.iter() {
indices.push(
self.get_validator_index(pubkey)?
.ok_or(Error::PubkeyCacheInconsistent)
})
.collect()
.ok_or(Error::PubkeyCacheInconsistent)?,
)
}
Ok(indices)
}
/// Compute the sync committee indices for the next sync committee.