From 93f094bfa54111f5689c346206d70ef45b4dd680 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 11 Feb 2024 21:00:10 +0200 Subject: [PATCH] perf: add a dyn Cheatcode trait to reduce generated code (#7082) --- Cargo.toml | 7 ++----- crates/cheatcodes/src/lib.rs | 33 ++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9126a020628b..cbde283b1f0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/crates/cheatcodes/src/lib.rs b/crates/cheatcodes/src/lib.rs index 856467f2990d..caaef1d8fee4 100644 --- a/crates/cheatcodes/src/lib.rs +++ b/crates/cheatcodes/src/lib.rs @@ -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. @@ -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(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() { @@ -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 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.