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](e4b62139d7/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
This commit is contained in:
Paul Hauner 2021-02-09 02:00:51 +00:00
parent 194609d210
commit 7c059117f4

View File

@ -202,13 +202,12 @@ where
T: EthSpec,
F: Fn(usize) -> Option<Cow<'a, PublicKey>>,
{
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::<Result<_>>()?;
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<Cow<'a, PublicKey>>,
{
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::<Result<_>>()?;
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,