Skip to content

Commit

Permalink
perf(interpreter): branch less in as_usize_or_fail (bluealloy#1374)
Browse files Browse the repository at this point in the history
* perf(interpreter): branch less in as_usize_or_fail

* chore: clippy
  • Loading branch information
DaniPopes authored May 3, 2024
1 parent 99e177d commit 569760d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/i256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<bool, Sign>(*val != U256::ZERO) }
}
}

Expand Down
38 changes: 20 additions & 18 deletions crates/interpreter/src/instructions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}};
};
}
2 changes: 1 addition & 1 deletion crates/revm/src/db/states/transition_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/inspector/eip3155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<DB: Database> Inspector<DB> for TracerEip3155 {

fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
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 {
Expand Down

0 comments on commit 569760d

Please sign in to comment.