Skip to content

Commit

Permalink
Pre-allocate len of a fixed vec during Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Nov 25, 2024
1 parent 7758a1e commit bac5bdc
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,19 @@ where
)));
}

bytes
.chunks(T::ssz_fixed_len())
.map(|chunk| T::from_ssz_bytes(chunk))
.collect::<Result<Vec<T>, _>>()
.and_then(|vec| {
Self::new(vec).map_err(|e| {
ssz::DecodeError::BytesInvalid(format!(
"Wrong number of FixedVector elements: {:?}",
e
))
})
})
let vec = bytes.chunks(T::ssz_fixed_len()).try_fold(
Vec::with_capacity(num_items),
|mut vec, chunk| {
vec.push(T::from_ssz_bytes(chunk)?);
Ok(vec)
},
)?;
Self::new(vec).map_err(|e| {
ssz::DecodeError::BytesInvalid(format!(
"Wrong number of FixedVector elements: {:?}",
e
))
})
} else {
let vec = ssz::decode_list_of_variable_length_items(bytes, Some(fixed_len))?;
Self::new(vec).map_err(|e| {
Expand Down

0 comments on commit bac5bdc

Please sign in to comment.