Skip to content

Commit

Permalink
Allocate Vec capacity based on size hints during decoding (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordy25519 committed Apr 14, 2023
1 parent 4e14ff8 commit a148a4a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ethabi/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ fn decode_impl(types: &[ParamType], data: &[u8], validate: bool) -> Result<(Vec<
}

let mut tokens = vec![];
tokens.try_reserve_exact(types.len()).map_err(|_| Error::InvalidData)?;

let mut offset = 0;

for param in types {
Expand Down Expand Up @@ -178,6 +180,7 @@ fn decode_param(param: &ParamType, data: &[u8], offset: usize, validate: bool) -
let tail = &data[tail_offset..];

let mut tokens = vec![];
tokens.try_reserve_exact(len).map_err(|_| Error::InvalidData)?;
let mut new_offset = 0;

for _ in 0..len {
Expand All @@ -204,6 +207,7 @@ fn decode_param(param: &ParamType, data: &[u8], offset: usize, validate: bool) -
};

let mut tokens = vec![];
tokens.try_reserve_exact(len).map_err(|_| Error::InvalidData)?;

for _ in 0..len {
let res = decode_param(t, tail, new_offset, validate)?;
Expand Down
9 changes: 2 additions & 7 deletions ethabi/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ impl Serialize for TopicFilter {
}

/// Acceptable topic possibilities.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Default, PartialEq, Eq)]
pub enum Topic<T> {
/// Match any.
#[default]
Any,
/// Match any of the hashes.
OneOf(Vec<T>),
Expand Down Expand Up @@ -82,12 +83,6 @@ impl<T> Topic<T> {
}
}

impl<T> Default for Topic<T> {
fn default() -> Self {
Topic::Any
}
}

impl<T> From<Option<T>> for Topic<T> {
fn from(o: Option<T>) -> Self {
match o {
Expand Down

0 comments on commit a148a4a

Please sign in to comment.