]> Repositorios git - scryer-prolog.git/commitdiff
hint to the compiler/branch-predictor that
authorSkgland <[email protected]>
Sat, 21 Mar 2026 17:58:40 +0000 (18:58 +0100)
committerSkgland <[email protected]>
Sat, 21 Mar 2026 18:01:52 +0000 (19:01 +0100)
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

index 20ec38cbfa5ee5a465f77ab49d028c8e263f26f4..bcc270a3ce777b550d57666584f5d0e424af1b33 100644 (file)
@@ -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}");
+}