summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2026-09-14 10:48:32 +0200
committerHenry <mail@henrygressmann.de>2026-09-14 10:49:30 +0200
commit437a77c8d9e9286a7ca7993b6c8b85e7feb6ebdd (patch)
tree443640431d4a0f230474274f5adb0b98cdfad60e
parent795ec871806ea189818bbf2fd2a69455384d2004 (diff)
perf: optimize nightly tail dispatch
Similar to #55 but with only safe code. LLVM seems so optimize the discriminant conversion away. Signed-off-by: Henry <mail@henrygressmann.de>
-rw-r--r--crates/tinywasm/src/interpreter/executor/dispatch_become.rs36
-rw-r--r--crates/types/src/instructions.rs49
2 files changed, 59 insertions, 26 deletions
diff --git a/crates/tinywasm/src/interpreter/executor/dispatch_become.rs b/crates/tinywasm/src/interpreter/executor/dispatch_become.rs
index 0dc8d5e..c24f6ba 100644
--- a/crates/tinywasm/src/interpreter/executor/dispatch_become.rs
+++ b/crates/tinywasm/src/interpreter/executor/dispatch_become.rs
@@ -15,12 +15,14 @@ fn instruction_handler_mismatch() -> ! {
macro_rules! define_unbudgeted_tail_dispatch {
($executor:ident, $instr_ptr:ident, $dispatch_next:ident, $dispatch_flow:ident;
$($variant:ident $(($($arg:pat),*))? $({ $($field:ident),* })? => $body:expr),* $(,)?) => {
- fn handler_for(instruction: &Instruction) -> UnbudgetedHandler {
- use tinywasm_types::Instruction::*;
-
- match instruction {
- $($variant { .. } => Self::$variant,)*
- }
+ #[inline(always)]
+ fn handler_for(opcode: InstructionOpcode) -> UnbudgetedHandler {
+ static HANDLERS: [UnbudgetedHandler; InstructionOpcode::COUNT] = {
+ let mut handlers = [Unbudgeted::Unreachable as UnbudgetedHandler; InstructionOpcode::COUNT];
+ $(handlers[InstructionOpcode::$variant as usize] = Unbudgeted::$variant;)*
+ handlers
+ };
+ HANDLERS[opcode as usize]
}
$(
@@ -34,7 +36,7 @@ macro_rules! define_unbudgeted_tail_dispatch {
($next_instr_ptr:expr) => {{
let next_instr_ptr = $next_instr_ptr;
let instruction = $executor.func.instructions[next_instr_ptr];
- let handler = Self::handler_for(&instruction);
+ let handler = Self::handler_for(instruction.opcode());
become handler($executor, next_instr_ptr, instruction);
}};
}
@@ -63,12 +65,14 @@ macro_rules! define_unbudgeted_tail_dispatch {
macro_rules! define_bounded_tail_dispatch {
($executor:ident, $instr_ptr:ident, $dispatch_next:ident, $dispatch_flow:ident;
$($variant:ident $(($($arg:pat),*))? $({ $($field:ident),* })? => $body:expr),* $(,)?) => {
- fn handler_for(instruction: &Instruction) -> BoundedHandler {
- use tinywasm_types::Instruction::*;
-
- match instruction {
- $($variant { .. } => Self::$variant,)*
- }
+ #[inline(always)]
+ fn handler_for(opcode: InstructionOpcode) -> BoundedHandler {
+ static HANDLERS: [BoundedHandler; InstructionOpcode::COUNT] = {
+ let mut handlers = [Bounded::Unreachable as BoundedHandler; InstructionOpcode::COUNT];
+ $(handlers[InstructionOpcode::$variant as usize] = Bounded::$variant;)*
+ handlers
+ };
+ HANDLERS[opcode as usize]
}
$(
@@ -90,7 +94,7 @@ macro_rules! define_bounded_tail_dispatch {
}
let instruction = $executor.func.instructions[next_instr_ptr];
- let handler = Self::handler_for(&instruction);
+ let handler = Self::handler_for(instruction.opcode());
become handler($executor, next_instr_ptr, instruction, instructions_until_checkpoint - 1);
}};
}
@@ -130,7 +134,7 @@ impl Bounded {
fn run(executor: &mut Executor<'_>) -> ExecResult<()> {
let instr_ptr = executor.cf.instr_ptr;
let instruction = executor.func.instructions[instr_ptr];
- let handler = Self::handler_for(&instruction);
+ let handler = Self::handler_for(instruction.opcode());
handler(executor, instr_ptr, instruction, CHECKPOINT_INTERVAL - 1)
}
}
@@ -140,7 +144,7 @@ impl<'store> Executor<'store> {
pub(crate) fn run_to_completion(mut self) -> Result<()> {
let instr_ptr = self.cf.instr_ptr;
let instruction = self.func.instructions[instr_ptr];
- let handler = Unbudgeted::handler_for(&instruction);
+ let handler = Unbudgeted::handler_for(instruction.opcode());
Ok(handler(&mut self, instr_ptr, instruction)?)
}
diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs
index 6a357ea..3bc71a2 100644
--- a/crates/types/src/instructions.rs
+++ b/crates/types/src/instructions.rs
@@ -500,16 +500,45 @@ pub enum BinOp128 {
I64x2Mul,
}
-/// A TinyWasm bytecode instruction.
-///
-/// These instructions are an internal, version-specific representation and do not
-/// map one-to-one to WebAssembly instructions. Their variants and serialized form
-/// may change between TinyWasm releases.
-#[rustfmt::skip]
-#[derive(Clone, Copy, PartialEq)]
-#[cfg_attr(feature = "debug", derive(Debug))]
-#[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))]
-pub enum Instruction {
+macro_rules! define_instructions {
+ ($($variant:ident $(($($field:ty),* $(,)?))? $({ $($name:ident: $named_field:ty),* $(,)? })?),* $(,)?) => {
+ /// A TinyWasm bytecode instruction.
+ ///
+ /// These instructions are an internal, version-specific representation and do not
+ /// map one-to-one to WebAssembly instructions. Their variants and serialized form
+ /// may change between TinyWasm releases.
+ #[derive(Clone, Copy, PartialEq)]
+ #[cfg_attr(feature = "debug", derive(Debug))]
+ #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))]
+ pub enum Instruction {
+ $($variant $(($($field),*))? $({ $($name: $named_field),* })?),*
+ }
+
+ #[doc(hidden)]
+ #[repr(u16)]
+ #[derive(Clone, Copy)]
+ pub enum InstructionOpcode {
+ $($variant,)*
+ }
+
+ impl InstructionOpcode {
+ #[doc(hidden)]
+ pub const COUNT: usize = [$(Self::$variant),*].len();
+ }
+
+ impl Instruction {
+ /// Returns the compact opcode used to dispatch this instruction.
+ #[inline(always)]
+ pub const fn opcode(&self) -> InstructionOpcode {
+ match self {
+ $(Self::$variant { .. } => InstructionOpcode::$variant,)*
+ }
+ }
+ }
+ };
+}
+
+define_instructions! {
LocalCopy32(LocalAddr, LocalAddr), LocalCopy64(LocalAddr, LocalAddr), LocalCopy128(LocalAddr, LocalAddr),
AddConst32(i32), AndConst32(i32), XorConst32(i32), ShrUConst32(i32), AddConst64(Operand64Idx<i64>),
IncLocal32(I32LocalArg), IncLocal64(PackedOp64<LocalAddr, i64>),