Skip to content

Commit

Permalink
optimize Vec<T>::parse(), for Streeamable, by pre-allocating its size
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Nov 4, 2023
1 parent 1517f0f commit 54249e2
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions chia-traits/src/streamable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ impl<T: Streamable> Streamable for Vec<T> {
fn parse(input: &mut Cursor<&[u8]>) -> Result<Self> {
let len = u32::parse(input)?;

// TODO: pre-allocate capacity, but we'd need safe-guards for overflow
// attacks
let mut ret = Vec::<T>::new();
let limit = 2 * 1024 * 1024 / std::mem::size_of::<T>();
let mut ret = Vec::<T>::with_capacity(std::cmp::min(limit, len as usize));
for _ in 0..len {
ret.push(T::parse(input)?);
}
Expand Down

0 comments on commit 54249e2

Please sign in to comment.