From 5fc21241d6dac992120d0815cec6599a60879ecc Mon Sep 17 00:00:00 2001 From: Skgland Date: Sat, 21 Mar 2026 18:58:40 +0100 Subject: [PATCH] hint to the compiler/branch-predictor that we expect p to be in bound if we are suffiently certain that p can never be oob unsafe get_unchecked could be used to eliminate bounds checking all together which should improve performance further. --- src/machine/dispatch.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index 20ec38cb..bcc270a3 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -1622,7 +1622,12 @@ impl Machine { break; } - match &self.code[self.machine_st.p] { + let Some(inst) = self.code.get(self.machine_st.p) else { + // a seperate function marked #[cold] to make the compiler/branch-predictor prefer the happy path + handle_code_index_oob(self.code.len(), self.machine_st.p); + }; + + match inst { &Instruction::BreakFromDispatchLoop => { break 'outer; } @@ -6032,3 +6037,9 @@ impl Machine { std::process::ExitCode::SUCCESS } } + +#[cold] // this is a seperate function so that we can annotate it as cold +#[track_caller] +fn handle_code_index_oob(code_len: usize, p: usize) -> ! { + panic!("code pointer p = {p} is oob for code area of size {code_len}"); +} -- 2.54.0