Skip to content

Commit

Permalink
perf: use mem::swap in stack swap/exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Apr 23, 2024
1 parent db5d39c commit d9d9e84
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ default-members = ["crates/revm"]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[profile.release]
lto = true
codegen-units = 1
debug = true

[profile.ethtests]
inherits = "test"
opt-level = 3
31 changes: 18 additions & 13 deletions crates/interpreter/src/interpreter/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,35 @@ impl Stack {
/// # Panics
///
/// Panics if `n` is 0.
#[inline]
#[inline(always)]
#[cfg_attr(debug_assertions, track_caller)]
pub fn swap(&mut self, n: usize) -> Result<(), InstructionResult> {
assume!(n > 0, "attempted to swap with 0");
let len = self.data.len();
if n >= len {
return Err(InstructionResult::StackUnderflow);
}
let last_index = len - 1;
self.data.swap(last_index, last_index - n);
Ok(())
self.exchange(0, n)
}

/// Exchange two values on the stack. where `N` is first index and second index
/// is calculated as N+M
/// Exchange two values on the stack.
///
/// `n` is the first index, and the second index is calculated as `n + m`.
///
/// # Panics
///
/// Panics if `m` is zero.
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
pub fn exchange(&mut self, n: usize, m: usize) -> Result<(), InstructionResult> {
assume!(m > 0, "overlapping exchange");
let len = self.data.len();
let n_m_index = n + m;
if n_m_index >= len {
return Err(InstructionResult::StackUnderflow);
}
let last_index = len - 1;
self.data.swap(last_index - n, last_index - n_m_index);
// SAFETY: `n` and `n_m` are checked to be within bounds, and they don't overlap.
unsafe {
// NOTE: `mem::swap` performs better than `slice::swap`/`ptr::swap`, as they cannot
// assume that the pointers are non-overlapping when we know they are not.
let top = self.data.as_mut_ptr().add(len - 1);
core::mem::swap(&mut *top.sub(n), &mut *top.sub(n_m_index));
}
Ok(())
}

Expand Down

0 comments on commit d9d9e84

Please sign in to comment.