From 7c059117f47391da34c4b02e86c338796d652d2e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 9 Feb 2021 02:00:51 +0000 Subject: [PATCH] Avoid resizing attn signature sets vec (#2184) ## Issue Addressed NA ## Proposed Changes Reduces allocations by initializing the `pubkeys` vec to its final size. I doubt this will make a substantial difference, but it's nice to do it this way. Seeing as `indexed_attestation.attesting_indices` has a [fixed length](https://github.com/sigp/lighthouse/blob/e4b62139d7e2d5b0499648663af8de25b368f0bd/consensus/types/src/indexed_attestation.rs#L22), there's no real risk of a memory blow-up by pre-allocating the size of the `Vec`. ## Additional Info NA --- .../per_block_processing/signature_sets.rs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/consensus/state_processing/src/per_block_processing/signature_sets.rs b/consensus/state_processing/src/per_block_processing/signature_sets.rs index cac8c4d1c2..52aa743a56 100644 --- a/consensus/state_processing/src/per_block_processing/signature_sets.rs +++ b/consensus/state_processing/src/per_block_processing/signature_sets.rs @@ -202,13 +202,12 @@ where T: EthSpec, F: Fn(usize) -> Option>, { - let pubkeys = indexed_attestation - .attesting_indices - .into_iter() - .map(|&validator_idx| { - Ok(get_pubkey(validator_idx as usize).ok_or(Error::ValidatorUnknown(validator_idx))?) - }) - .collect::>()?; + let mut pubkeys = Vec::with_capacity(indexed_attestation.attesting_indices.len()); + for &validator_idx in &indexed_attestation.attesting_indices { + pubkeys.push( + get_pubkey(validator_idx as usize).ok_or(Error::ValidatorUnknown(validator_idx))?, + ); + } let domain = spec.get_domain( indexed_attestation.data.target.epoch, @@ -236,13 +235,12 @@ where T: EthSpec, F: Fn(usize) -> Option>, { - let pubkeys = indexed_attestation - .attesting_indices - .into_iter() - .map(|&validator_idx| { - Ok(get_pubkey(validator_idx as usize).ok_or(Error::ValidatorUnknown(validator_idx))?) - }) - .collect::>()?; + let mut pubkeys = Vec::with_capacity(indexed_attestation.attesting_indices.len()); + for &validator_idx in &indexed_attestation.attesting_indices { + pubkeys.push( + get_pubkey(validator_idx as usize).ok_or(Error::ValidatorUnknown(validator_idx))?, + ); + } let domain = spec.get_domain( indexed_attestation.data.target.epoch,