Skip to content

Commit 9cfd144

Browse files
authored
perf(interpreter): remove EOF branch in CODE{SIZE,COPY} (bluealloy#1308)
1 parent cc1b9f7 commit 9cfd144

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

crates/interpreter/src/instructions/system.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub fn caller<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) {
3131

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

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

50+
// Inform the optimizer that the bytecode cannot be EOF to remove a bounds check.
51+
assume!(!interpreter.contract.bytecode.is_eof());
4852
// Note: this can't panic because we resized memory to fit.
4953
interpreter.shared_memory.set_data(
5054
memory_offset,

crates/primitives/src/bytecode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ impl Bytecode {
5252
}
5353

5454
/// Return reference to the EOF if bytecode is EOF.
55-
pub fn eof(&self) -> Option<&Eof> {
55+
#[inline]
56+
pub const fn eof(&self) -> Option<&Eof> {
5657
match self {
5758
Self::Eof(eof) => Some(eof),
5859
_ => None,
5960
}
6061
}
6162

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

0 commit comments

Comments
 (0)