Skip to content

Commit

Permalink
perf: add a dyn Cheatcode trait to reduce generated code (#7082)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 11, 2024
1 parent 92ba67f commit 93f094b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ alloy-rlp = "0.3.3"
solang-parser = "=0.3.3"

## misc
chrono = { version = "0.4", default-features = false, features = [
"clock",
"std",
] }
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
color-eyre = "0.6"
derive_more = "0.99"
eyre = "0.6"
Expand All @@ -194,7 +191,7 @@ toml = "0.8"
tracing = "0.1"
tracing-subscriber = "0.3"
evm-disassembler = "0.4"
vergen = "8"
vergen = { version = "8", default-features = false }
# TODO: bumping to >=0.13.2 breaks ecrecover: https://github.com/foundry-rs/foundry/pull/6969
# TODO: unpin on next revm release: https://github.com/bluealloy/revm/pull/870
k256 = "=0.13.1"
Expand Down
33 changes: 26 additions & 7 deletions crates/cheatcodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod utils;
pub use test::expect::ExpectedCallTracker;

/// Cheatcode implementation.
pub(crate) trait Cheatcode: CheatcodeDef {
pub(crate) trait Cheatcode: CheatcodeDef + DynCheatcode {
/// Applies this cheatcode to the given state.
///
/// Implement this function if you don't need access to the EVM data.
Expand All @@ -65,13 +65,17 @@ pub(crate) trait Cheatcode: CheatcodeDef {
trace_return(&result);
return result;

// Separate functions to avoid inline and monomorphization bloat.
fn trace_span<T: Cheatcode>(cheat: &T) -> tracing::Span {
if enabled!(tracing::Level::TRACE) {
trace_span!(target: "cheatcodes", "apply", cheat=?cheat)
} else {
debug_span!(target: "cheatcodes", "apply", id=%T::CHEATCODE.func.id)
// Separate and non-generic functions to avoid inline and monomorphization bloat.
fn trace_span(cheat: &dyn DynCheatcode) -> tracing::Span {
let span = debug_span!(target: "cheatcodes", "apply");
if !span.is_disabled() {
if enabled!(tracing::Level::TRACE) {
span.record("cheat", tracing::field::debug(cheat.as_debug()));
} else {
span.record("id", cheat.cheatcode().func.id);
}
}
span
}

fn trace_call() {
Expand All @@ -90,6 +94,21 @@ pub(crate) trait Cheatcode: CheatcodeDef {
}
}

pub(crate) trait DynCheatcode {
fn cheatcode(&self) -> &'static foundry_cheatcodes_spec::Cheatcode<'static>;
fn as_debug(&self) -> &dyn std::fmt::Debug;
}

impl<T: Cheatcode> DynCheatcode for T {
fn cheatcode(&self) -> &'static foundry_cheatcodes_spec::Cheatcode<'static> {
T::CHEATCODE
}

fn as_debug(&self) -> &dyn std::fmt::Debug {
self
}
}

/// The cheatcode context, used in [`Cheatcode`].
pub(crate) struct CheatsCtxt<'a, 'b, 'c, DB: DatabaseExt> {
/// The cheatcodes inspector state.
Expand Down

0 comments on commit 93f094b

Please sign in to comment.