Skip to content

Commit

Permalink
perf: cast instruction functions to fn
Browse files Browse the repository at this point in the history
The compiler generates much more favorable assembly if all the
functions are casted to a `fn(_, _)` pointer before calling them.

See <bluealloy#310 (comment)>
for more information.
  • Loading branch information
DaniPopes committed Aug 24, 2023
1 parent 6de9e5f commit 0cc0ce6
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ macro_rules! opcodes {
map
};

// Requires `inline_const` and `const_mut_refs` unstable features
type Instruction = fn(&mut Interpreter, &mut dyn Host);

// Requires `inline_const` and `const_mut_refs` unstable features,
// but provides ~+2% extra performance.
// See: https://github.com/bluealloy/revm/issues/310#issuecomment-1664381513
/*
type Instruction = fn(&mut Interpreter, &mut dyn Host);
type InstructionTable = [Instruction; 256];
const fn make_instruction_table<SPEC: Spec>() -> InstructionTable {
Expand All @@ -51,10 +53,13 @@ macro_rules! opcodes {
/// Evaluates the opcode in the given context.
#[inline(always)]
pub(crate) fn eval<SPEC: Spec>(opcode: u8, interpreter: &mut Interpreter, host: &mut dyn Host) {
match opcode {
$($name => $f(interpreter, host),)*
_ => control::not_found(interpreter, host),
}
// See https://github.com/bluealloy/revm/issues/310#issuecomment-1664381513
// for previous efforts on optimizing this function.
let f: Instruction = match opcode {
$($name => $f as Instruction,)*
_ => control::not_found as Instruction,
};
f(interpreter, host);
}
};
}
Expand Down

0 comments on commit 0cc0ce6

Please sign in to comment.