Skip to content

Commit 9e59ff9

Browse files
authored
Pre-allocate len of a fixed vec during Decode (#34)
1 parent 7758a1e commit 9e59ff9

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/fixed_vector.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,19 @@ where
304304
)));
305305
}
306306

307-
bytes
308-
.chunks(T::ssz_fixed_len())
309-
.map(|chunk| T::from_ssz_bytes(chunk))
310-
.collect::<Result<Vec<T>, _>>()
311-
.and_then(|vec| {
312-
Self::new(vec).map_err(|e| {
313-
ssz::DecodeError::BytesInvalid(format!(
314-
"Wrong number of FixedVector elements: {:?}",
315-
e
316-
))
317-
})
318-
})
307+
let vec = bytes.chunks(T::ssz_fixed_len()).try_fold(
308+
Vec::with_capacity(num_items),
309+
|mut vec, chunk| {
310+
vec.push(T::from_ssz_bytes(chunk)?);
311+
Ok(vec)
312+
},
313+
)?;
314+
Self::new(vec).map_err(|e| {
315+
ssz::DecodeError::BytesInvalid(format!(
316+
"Wrong number of FixedVector elements: {:?}",
317+
e
318+
))
319+
})
319320
} else {
320321
let vec = ssz::decode_list_of_variable_length_items(bytes, Some(fixed_len))?;
321322
Self::new(vec).map_err(|e| {

0 commit comments

Comments
 (0)