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

perf: add a dyn Cheatcode trait to reduce generated code #7082

Merged
merged 1 commit into from
Feb 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading