From e5212f132021d58de9637355729096ccb511886e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 1 Jul 2022 03:44:37 +0000 Subject: [PATCH] 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 --- consensus/types/src/beacon_state.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index 3a0f7d02e..66656d358 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -779,14 +779,14 @@ impl BeaconState { &mut self, sync_committee: &SyncCommittee, ) -> Result, 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.