From 5114aee5cf08e48137ebfe7da85c9960a4e7bc37 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 17 Nov 2020 04:28:40 +0000 Subject: [PATCH] Avoid allocations on VariableList (#1921) ## Issue Addressed NA ## Proposed Changes Avoids lots of grow allocations when decoding a `VariableList` of fixed-length items. This is the function used for decoding the `state.validators` list. ## Additional Info NA --- consensus/ssz_types/src/variable_list.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/consensus/ssz_types/src/variable_list.rs b/consensus/ssz_types/src/variable_list.rs index 774d4a358..599e4f5b6 100644 --- a/consensus/ssz_types/src/variable_list.rs +++ b/consensus/ssz_types/src/variable_list.rs @@ -247,8 +247,10 @@ where bytes .chunks(T::ssz_fixed_len()) - .map(|chunk| T::from_ssz_bytes(chunk)) - .collect::, _>>() + .try_fold(Vec::with_capacity(num_items), |mut vec, chunk| { + vec.push(T::from_ssz_bytes(chunk)?); + Ok(vec) + }) .map(Into::into) } else { ssz::decode_list_of_variable_length_items(bytes, Some(max_len)).map(|vec| vec.into())