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

feat(dyn-abi): add dyn-abi type parser benchmarks #179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/dyn-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde_json = { workspace = true, optional = true }
hex-literal.workspace = true
criterion.workspace = true
ethabi = "18"
rand = "0.8"

[features]
default = ["std"]
Expand All @@ -43,3 +44,8 @@ eip712 = ["alloy-sol-types/eip712-serde", "dep:derive_more", "dep:serde", "dep:s
name = "abi"
path = "benches/abi.rs"
harness = false

[[bench]]
name = "types"
path = "benches/types.rs"
harness = false
4 changes: 2 additions & 2 deletions crates/dyn-abi/benches/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ fn ethabi_encode(c: &mut Criterion) {
g.bench_function("single", |b| {
let input = encode_single_input();
b.iter(|| {
let token = ethabi::Token::String(black_box(&input).clone());
ethabi::encode(&[token])
let token = ethabi::Token::String(input.clone());
ethabi::encode(&[black_box(token)])
});
});

Expand Down
79 changes: 79 additions & 0 deletions crates/dyn-abi/benches/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use alloy_dyn_abi::DynSolType;
use criterion::{
criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
};
use rand::seq::SliceRandom;
use std::{hint::black_box, time::Duration};

static KEYWORDS: &[&str] = &[
"address", "bool", "string", "bytes", "bytes32", "uint", "uint256", "int", "int256",
];
static COMPLEX: &[&str] = &[
"((uint104,bytes,bytes8,bytes7,address,bool,address,int256,int32,bytes1,uint56,int136),uint80,uint104,address,bool,bytes14,int16,address,string,uint176,uint72,(uint120,uint192,uint256,int232,bool,bool,bool,bytes5,int56,address,uint224,int248,bytes10,int48,int8),string,string,bool,bool)",
"(address,string,(bytes,int48,bytes30,bool,address,bytes30,int48,address,bytes17,bool,uint32),bool,address,bytes28,bytes25,uint136)",
"(uint168,bytes21,address,(bytes,bool,string,address,bool,string,bytes,uint232,int128,int64,uint96,bytes7,int136),bool,uint200[5],bool,bytes,uint240,address,address,bytes15,bytes)"
];

fn parse(c: &mut Criterion) {
let mut g = group(c, "parse");
let rng = &mut rand::thread_rng();

g.bench_function("keywords", |b| {
b.iter(|| {
let kw = KEYWORDS.choose(rng).unwrap();
DynSolType::parse(black_box(*kw)).unwrap()
})
});
g.bench_function("complex", |b| {
b.iter(|| {
let complex = COMPLEX.choose(rng).unwrap();
DynSolType::parse(black_box(*complex)).unwrap()
})
});

g.finish();
}

fn format(c: &mut Criterion) {
let mut g = group(c, "format");
let rng = &mut rand::thread_rng();

g.bench_function("keywords", |b| {
let keyword_types = KEYWORDS
.iter()
.map(|s| DynSolType::parse(s).unwrap())
.collect::<Vec<_>>();
let keyword_types = keyword_types.as_slice();
assert!(!keyword_types.is_empty());
b.iter(|| {
let kw = unsafe { keyword_types.choose(rng).unwrap_unchecked() };
black_box(kw).sol_type_name()
})
});
g.bench_function("complex", |b| {
let complex_types = COMPLEX
.iter()
.map(|s| DynSolType::parse(s).unwrap())
.collect::<Vec<_>>();
let complex_types = complex_types.as_slice();
assert!(!complex_types.is_empty());
b.iter(|| {
let complex = unsafe { complex_types.choose(rng).unwrap_unchecked() };
black_box(complex).sol_type_name()
})
});

g.finish();
}

fn group<'a>(c: &'a mut Criterion, group_name: &str) -> BenchmarkGroup<'a, WallTime> {
let mut g = c.benchmark_group(group_name);
g.noise_threshold(0.03)
.warm_up_time(Duration::from_secs(1))
.measurement_time(Duration::from_secs(3))
.sample_size(200);
g
}

criterion_group!(benches, parse, format);
criterion_main!(benches);
17 changes: 17 additions & 0 deletions crates/dyn-abi/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ impl core::str::FromStr for DynSolType {
}

impl DynSolType {
/// Parses a Solidity type name string into a [`DynSolType`].
///
/// # Examples
///
/// ```
/// # use alloy_dyn_abi::DynSolType;
/// let type_name = "uint256";
/// let ty = DynSolType::parse(type_name)?;
/// assert_eq!(ty, DynSolType::Uint(256));
/// assert_eq!(ty.sol_type_name(), type_name);
/// # Ok::<_, alloy_dyn_abi::DynAbiError>(())
/// ```
#[inline]
pub fn parse(s: &str) -> DynAbiResult<Self> {
TypeSpecifier::try_from(s).and_then(|t| t.resolve_basic_solidity())
}

/// Check that a given [`DynSolValue`] matches this type.
pub fn matches(&self, value: &DynSolValue) -> bool {
match self {
Expand Down