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(interpreter): branch less in as_usize_or_fail #1374

Merged
merged 2 commits into from
May 3, 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
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the match statement? optimizer trick?

Copy link
Collaborator Author

@DaniPopes DaniPopes May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see std::dbg!, allows all expressions as input since they can be temporaries as well

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, that makes a lot of sense

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this is cool, one less branch

$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
Loading