Skip to content

Commit

Permalink
perf(interpreter): remove EOF branch in CODE{SIZE,COPY} (bluealloy#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Apr 16, 2024
1 parent cc1b9f7 commit 9cfd144
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/interpreter/src/instructions/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub fn caller<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {

pub fn codesize<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
// Inform the optimizer that the bytecode cannot be EOF to remove a bounds check.
assume!(!interpreter.contract.bytecode.is_eof());
push!(interpreter, U256::from(interpreter.contract.bytecode.len()));
}

Expand All @@ -45,6 +47,8 @@ pub fn codecopy<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H)
let code_offset = as_usize_saturated!(code_offset);
resize_memory!(interpreter, memory_offset, len);

// Inform the optimizer that the bytecode cannot be EOF to remove a bounds check.
assume!(!interpreter.contract.bytecode.is_eof());
// Note: this can't panic because we resized memory to fit.
interpreter.shared_memory.set_data(
memory_offset,
Expand Down
6 changes: 4 additions & 2 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ impl Bytecode {
}

/// Return reference to the EOF if bytecode is EOF.
pub fn eof(&self) -> Option<&Eof> {
#[inline]
pub const fn eof(&self) -> Option<&Eof> {
match self {
Self::Eof(eof) => Some(eof),
_ => None,
}
}

/// Return true if bytecode is EOF.
pub fn is_eof(&self) -> bool {
#[inline]
pub const fn is_eof(&self) -> bool {
matches!(self, Self::Eof(_))
}

Expand Down

0 comments on commit 9cfd144

Please sign in to comment.