Skip to content
Merged
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
20 changes: 17 additions & 3 deletions crates/interpreter/src/interpreter/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,16 @@ impl Stack {
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
pub fn pop(&mut self) -> Result<U256, InstructionResult> {
self.data.pop().ok_or(InstructionResult::StackUnderflow)
//Todo: can you likely instrincts to show the else branch is more likely to be hit
let len = self.data.len();
if primitives::hints_util::unlikely(len == 0) {
Err(InstructionResult::StackUnderflow)
} else {
unsafe {
self.data.set_len(len - 1);
Ok(core::ptr::read(self.data.as_ptr().add(len - 1)))
}
}
}

/// Removes the topmost element from the stack and returns it.
Expand Down Expand Up @@ -215,10 +224,15 @@ impl Stack {
pub fn push(&mut self, value: U256) -> bool {
// In debug builds, verify we have sufficient capacity provisioned.
debug_assert!(self.data.capacity() >= STACK_LIMIT);
if self.data.len() == STACK_LIMIT {
let len = self.data.len();
if len == STACK_LIMIT {
return false;
}
self.data.push(value);
unsafe {
let end = self.data.as_mut_ptr().add(len);
core::ptr::write(end, value);
self.data.set_len(len + 1);
}
Copy link
Member

Choose a reason for hiding this comment

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

This is nice! we didn't optimize this function as it is not used that much, PUSH opcodes use optimized push_slice

true
}

Expand Down
Loading