From 9ca2449f3d33dd21465ba4a039329eedbbe48d92 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 2 May 2024 15:36:50 +0200 Subject: [PATCH 1/2] perf(interpreter): branch less in as_usize_or_fail --- crates/interpreter/src/instructions/macros.rs | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/crates/interpreter/src/instructions/macros.rs b/crates/interpreter/src/instructions/macros.rs index 310ccdc3c0..abac40bc27 100644 --- a/crates/interpreter/src/instructions/macros.rs +++ b/crates/interpreter/src/instructions/macros.rs @@ -300,14 +300,17 @@ macro_rules! push { /// Converts a `U256` value to a `u64`, saturating to `MAX` if the value is too large. #[macro_export] macro_rules! as_u64_saturated { - ($v:expr) => {{ - let x: &[u64; 4] = $v.as_limbs(); - if x[1] == 0 && x[2] == 0 && x[3] == 0 { - x[0] - } else { - u64::MAX + ($v:expr) => { + match $v.as_limbs() { + x => { + if (x[1] == 0) & (x[2] == 0) & (x[3] == 0) { + x[0] + } else { + u64::MAX + } + } } - }}; + }; } /// Converts a `U256` value to a `usize`, saturating to `MAX` if the value is too large. @@ -352,16 +355,15 @@ macro_rules! as_usize_or_fail_ret { ) }; - ($interp:expr, $v:expr, $reason:expr, $ret:expr) => {{ - let x = $v.as_limbs(); - if x[1] != 0 || x[2] != 0 || x[3] != 0 { - $interp.instruction_result = $reason; - return $ret; + ($interp:expr, $v:expr, $reason:expr, $ret:expr) => { + match $v.as_limbs() { + x => { + if (x[0] > usize::MAX as u64) | (x[1] != 0) | (x[2] != 0) | (x[3] != 0) { + $interp.instruction_result = $reason; + return $ret; + } + x[0] as usize + } } - let Ok(val) = usize::try_from(x[0]) else { - $interp.instruction_result = $reason; - return $ret; - }; - val - }}; + }; } From ff5286ff8f99776a04253c5112c1fc221fe2edbe Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 2 May 2024 15:43:58 +0200 Subject: [PATCH 2/2] chore: clippy --- bins/revme/src/cmd/statetest/runner.rs | 2 +- crates/interpreter/src/instructions/i256.rs | 2 +- crates/revm/src/db/states/transition_account.rs | 2 +- crates/revm/src/inspector/eip3155.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index c9f8aede3a..dcf8fcc3a5 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -361,7 +361,7 @@ pub fn execute_test_suite( .build(); let mut evm = Evm::builder() .with_db(&mut state) - .modify_env(|e| *e = env.clone()) + .modify_env(|e| e.clone_from(&env)) .with_spec_id(spec_id) .build(); diff --git a/crates/interpreter/src/instructions/i256.rs b/crates/interpreter/src/instructions/i256.rs index d806eacfb8..d7f72446d9 100644 --- a/crates/interpreter/src/instructions/i256.rs +++ b/crates/interpreter/src/instructions/i256.rs @@ -33,7 +33,7 @@ pub fn i256_sign(val: &U256) -> Sign { Sign::Minus } else { // SAFETY: false == 0 == Zero, true == 1 == Plus - unsafe { core::mem::transmute(*val != U256::ZERO) } + unsafe { core::mem::transmute::(*val != U256::ZERO) } } } diff --git a/crates/revm/src/db/states/transition_account.rs b/crates/revm/src/db/states/transition_account.rs index 1dc6bc33dd..599bc290b0 100644 --- a/crates/revm/src/db/states/transition_account.rs +++ b/crates/revm/src/db/states/transition_account.rs @@ -85,7 +85,7 @@ impl TransitionAccount { /// Update new values of transition. Don't override old values. /// Both account info and old storages need to be left intact. pub fn update(&mut self, other: Self) { - self.info = other.info.clone(); + self.info.clone_from(&other.info); self.status = other.status; // if transition is from some to destroyed drop the storage. diff --git a/crates/revm/src/inspector/eip3155.rs b/crates/revm/src/inspector/eip3155.rs index b5331157dc..aec20ffd8f 100644 --- a/crates/revm/src/inspector/eip3155.rs +++ b/crates/revm/src/inspector/eip3155.rs @@ -191,7 +191,7 @@ impl Inspector for TracerEip3155 { fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext) { self.gas_inspector.step(interp, context); - self.stack = interp.stack.data().clone(); + self.stack.clone_from(interp.stack.data()); self.memory = if self.include_memory { Some(hex::encode_prefixed(interp.shared_memory.context_memory())) } else {