diff --git a/ethers-solc/Cargo.toml b/ethers-solc/Cargo.toml index c3b51dab..0af8e956 100644 --- a/ethers-solc/Cargo.toml +++ b/ethers-solc/Cargo.toml @@ -69,6 +69,7 @@ harness = false [[bench]] name = "read_all" +required-features = ["project-util"] harness = false [[test]] diff --git a/ethers-solc/benches/read_all.rs b/ethers-solc/benches/read_all.rs index 1184bcad..6c38e0ec 100644 --- a/ethers-solc/benches/read_all.rs +++ b/ethers-solc/benches/read_all.rs @@ -4,7 +4,7 @@ extern crate criterion; use criterion::Criterion; use ethers_core::rand; -use ethers_solc::artifacts::Source; +use ethers_solc::{artifacts::Source, project_util::TempProject}; use rand::{distributions::Alphanumeric, Rng}; use std::{ fs::File, @@ -14,10 +14,26 @@ use std::{ fn read_all_benchmark(c: &mut Criterion) { let root = tempfile::tempdir().unwrap(); - let inputs = prepare_contracts(root.path(), 8); + let inputs = prepare_contracts(root.path(), 35); let mut group = c.benchmark_group("read many"); - group.sample_size(10); + group.bench_function("sequential", |b| { + b.iter(|| { + Source::read_all(&inputs).unwrap(); + }); + }); + group.bench_function("parallel", |b| { + b.iter(|| { + Source::par_read_all(&inputs).unwrap(); + }); + }); +} + +fn read_solmate(c: &mut Criterion) { + let prj = TempProject::checkout("transmissions11/solmate").unwrap(); + let inputs = ethers_solc::utils::source_files(prj.sources_path()); + + let mut group = c.benchmark_group("read solmate"); group.bench_function("sequential", |b| { b.iter(|| { Source::read_all(&inputs).unwrap(); @@ -40,7 +56,7 @@ fn prepare_contracts(root: &Path, num: usize) -> Vec { let mut rng = rand::thread_rng(); // let's assume a solidity file is between 2kb and 16kb - let n: usize = rng.gen_range(2..17); + let n: usize = rng.gen_range(4..17); let s: String = rng.sample_iter(&Alphanumeric).take(n * 1024).map(char::from).collect(); writer.write_all(s.as_bytes()).unwrap(); writer.flush().unwrap(); @@ -49,5 +65,5 @@ fn prepare_contracts(root: &Path, num: usize) -> Vec { files } -criterion_group!(benches, read_all_benchmark); +criterion_group!(benches, read_all_benchmark, read_solmate); criterion_main!(benches); diff --git a/ethers-solc/src/artifacts/mod.rs b/ethers-solc/src/artifacts/mod.rs index b4ae1ff8..4e5b7fe4 100644 --- a/ethers-solc/src/artifacts/mod.rs +++ b/ethers-solc/src/artifacts/mod.rs @@ -1187,10 +1187,6 @@ pub struct Source { } impl Source { - /// this is a heuristically measured threshold at which we can generally expect a speedup by - /// using rayon's `par_iter`, See `Self::read_all_files` - pub const NUM_READ_PAR: usize = 8; - /// Reads the file content pub fn read(file: impl AsRef) -> Result { let file = file.as_ref(); @@ -1206,17 +1202,7 @@ impl Source { /// /// Depending on the len of the vec it will try to read the files in parallel pub fn read_all_files(files: Vec) -> Result { - use rayon::prelude::*; - - if files.len() < Self::NUM_READ_PAR { - Self::read_all(files) - } else { - files - .par_iter() - .map(Into::into) - .map(|file| Self::read(&file).map(|source| (file, source))) - .collect() - } + Self::read_all(files) } /// Reads all files @@ -1235,7 +1221,7 @@ impl Source { /// Parallelized version of `Self::read_all` that reads all files using a parallel iterator /// /// NOTE: this is only expected to be faster than `Self::read_all` if the given iterator - /// contains at least several paths. see also `Self::read_all_files`. + /// contains at least several paths or the files are rather large. pub fn par_read_all(files: I) -> Result where I: IntoIterator,