Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate Vec capacity based on size hints during decoding #293

Merged
merged 5 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}
Comment on lines -85 to -89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? I think you are changing behavior because the previous version still implemented Default when T wasn't Default while the new one doesn't. Either way, it's unrelated to the rest of the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no behaviour change, the compiler recommended this change in the CI failure.
this compiles fine:

	#[test]
	fn default_for_topic_T() {
		struct Foo;
		let f = Topic::<Foo>::default();
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only changed it due to the CI failure, happy to keep or revert it either way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does CI fail because of that or is a warning? Apparently derive got smarter and it doesn't change behavior. I'm fine with keeping the change then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failed, I actually think it was a clippy suggestion but it is configured to fail the job


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