Skip to content

Commit

Permalink
perf: reduce clones in fuzzed_functions (#8178)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jun 17, 2024
1 parent f6ad1e5 commit f673066
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
26 changes: 12 additions & 14 deletions crates/evm/fuzz/src/invariant/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_json_abi::{Function, JsonAbi};
use alloy_primitives::{Address, Bytes};
use itertools::Either;
use parking_lot::Mutex;
use std::{collections::BTreeMap, sync::Arc};

Expand Down Expand Up @@ -116,22 +117,19 @@ impl FuzzRunIdentifiedContracts {

/// Helper to retrieve functions to fuzz for specified abi.
/// Returns specified targeted functions if any, else mutable abi functions.
pub(crate) fn abi_fuzzed_functions(
abi: &JsonAbi,
targeted_functions: &[Function],
) -> Vec<Function> {
pub(crate) fn abi_fuzzed_functions<'a>(
abi: &'a JsonAbi,
targeted_functions: &'a [Function],
) -> impl Iterator<Item = &'a Function> {
if !targeted_functions.is_empty() {
targeted_functions.to_vec()
Either::Left(targeted_functions.iter())
} else {
abi.functions()
.filter(|&func| {
!matches!(
func.state_mutability,
alloy_json_abi::StateMutability::Pure | alloy_json_abi::StateMutability::View
)
})
.cloned()
.collect()
Either::Right(abi.functions().filter(|&func| {
!matches!(
func.state_mutability,
alloy_json_abi::StateMutability::Pure | alloy_json_abi::StateMutability::View
)
}))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/fuzz/src/strategies/invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn override_call_strat(
let (_, contract_specs) = contracts.iter().nth(rand_index).unwrap();
contract_specs
});
let fuzzed_functions = abi_fuzzed_functions(abi, functions);
let fuzzed_functions: Vec<_> = abi_fuzzed_functions(abi, functions).cloned().collect();
any::<prop::sample::Index>().prop_map(move |index| index.get(&fuzzed_functions).clone())
};

Expand Down

0 comments on commit f673066

Please sign in to comment.