From: Skgland Date: Sat, 21 Mar 2026 17:58:40 +0000 (+0100) Subject: hint to the compiler/branch-predictor that X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=5fc21241d6dac992120d0815cec6599a60879ecc;p=scryer-prolog.git 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. --- 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}"); +}