diff -r 33e44e5e6e34 libinterp/octave-value/ov-typeinfo.cc
--- a/libinterp/octave-value/ov-typeinfo.cc	Mon Feb 27 13:26:19 2023 -0500
+++ b/libinterp/octave-value/ov-typeinfo.cc	Mon Feb 27 13:49:44 2023 -0500
@@ -498,42 +498,42 @@ octave_value type_info::lookup_type (con
 type_info::unary_class_op_fcn
 type_info::lookup_unary_class_op (octave_value::unary_op op)
 {
-  void *f = m_unary_class_ops.checkelem (static_cast<int> (op));
+  void *f = m_unary_class_ops.checkelem_value (static_cast<int> (op));
   return reinterpret_cast<type_info::unary_class_op_fcn> (f);
 }
 
 type_info::unary_op_fcn
 type_info::lookup_unary_op (octave_value::unary_op op, int t)
 {
-  void *f = m_unary_ops.checkelem (static_cast<int> (op), t);
+  void *f = m_unary_ops.checkelem_value (static_cast<int> (op), t);
   return reinterpret_cast<type_info::unary_op_fcn> (f);
 }
 
 type_info::non_const_unary_op_fcn
 type_info::lookup_non_const_unary_op (octave_value::unary_op op, int t)
 {
-  void *f = m_non_const_unary_ops.checkelem (static_cast<int> (op), t);
+  void *f = m_non_const_unary_ops.checkelem_value (static_cast<int> (op), t);
   return reinterpret_cast<type_info::non_const_unary_op_fcn> (f);
 }
 
 type_info::binary_class_op_fcn
 type_info::lookup_binary_class_op (octave_value::binary_op op)
 {
-  void *f = m_binary_class_ops.checkelem (static_cast<int> (op));
+  void *f = m_binary_class_ops.checkelem_value (static_cast<int> (op));
   return reinterpret_cast<type_info::binary_class_op_fcn> (f);
 }
 
 type_info::binary_op_fcn
 type_info::lookup_binary_op (octave_value::binary_op op, int t1, int t2)
 {
-  void *f = m_binary_ops.checkelem (static_cast<int> (op), t1, t2);
+  void *f = m_binary_ops.checkelem_value (static_cast<int> (op), t1, t2);
   return reinterpret_cast<type_info::binary_op_fcn> (f);
 }
 
 type_info::binary_class_op_fcn
 type_info::lookup_binary_class_op (octave_value::compound_binary_op op)
 {
-  void *f = m_compound_binary_class_ops.checkelem (static_cast<int> (op));
+  void *f =m_compound_binary_class_ops.checkelem_value (static_cast<int> (op));
   return reinterpret_cast<type_info::binary_class_op_fcn> (f);
 }
 
@@ -541,14 +541,15 @@ type_info::binary_op_fcn
 type_info::lookup_binary_op (octave_value::compound_binary_op op,
                              int t1, int t2)
 {
-  void *f = m_compound_binary_ops.checkelem (static_cast<int> (op), t1, t2);
+  void *f = m_compound_binary_ops.checkelem_value (static_cast<int> (op),
+                                                   t1, t2);
   return reinterpret_cast<type_info::binary_op_fcn> (f);
 }
 
 type_info::cat_op_fcn
 type_info::lookup_cat_op (int t1, int t2)
 {
-  void *f = m_cat_ops.checkelem (t1, t2);
+  void *f = m_cat_ops.checkelem_value (t1, t2);
   return reinterpret_cast<type_info::cat_op_fcn> (f);
 }
 
@@ -556,27 +557,27 @@ type_info::assign_op_fcn
 type_info::lookup_assign_op (octave_value::assign_op op,
                              int t_lhs, int t_rhs)
 {
-  void *f = m_assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs);
+  void *f = m_assign_ops.checkelem_value (static_cast<int> (op), t_lhs, t_rhs);
   return reinterpret_cast<type_info::assign_op_fcn> (f);
 }
 
 type_info::assignany_op_fcn
 type_info::lookup_assignany_op (octave_value::assign_op op, int t_lhs)
 {
-  void *f = m_assignany_ops.checkelem (static_cast<int> (op), t_lhs);
+  void *f = m_assignany_ops.checkelem_value (static_cast<int> (op), t_lhs);
   return reinterpret_cast<type_info::assignany_op_fcn> (f);
 }
 
 int
 type_info::lookup_pref_assign_conv (int t_lhs, int t_rhs)
 {
-  return m_pref_assign_conv.checkelem (t_lhs, t_rhs);
+  return m_pref_assign_conv.checkelem_value (t_lhs, t_rhs);
 }
 
 octave_base_value::type_conv_fcn
 type_info::lookup_widening_op (int t, int t_result)
 {
-  void *f = m_widening_ops.checkelem (t, t_result);
+  void *f = m_widening_ops.checkelem_value (t, t_result);
   return reinterpret_cast<octave_base_value::type_conv_fcn> (f);
 }
 
diff -r 33e44e5e6e34 liboctave/array/Array-base.cc
--- a/liboctave/array/Array-base.cc	Mon Feb 27 13:26:19 2023 -0500
+++ b/liboctave/array/Array-base.cc	Mon Feb 27 13:49:44 2023 -0500
@@ -226,14 +226,14 @@ template <typename T, typename Alloc>
 T&
 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j)
 {
-  return elem (compute_index (i, j));
+  return elem (compute_index_inline (i, j));
 }
 
 template <typename T, typename Alloc>
 T&
 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k)
 {
-  return elem (compute_index (i, j, k));
+  return elem (compute_index_inline (i, j, k));
 }
 
 template <typename T, typename Alloc>
@@ -260,7 +260,7 @@ template <typename T, typename Alloc>
 typename Array<T, Alloc>::crefT
 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j) const
 {
-  return elem (compute_index (i, j));
+  return elem (compute_index_inline (i, j));
 }
 
 template <typename T, typename Alloc>
@@ -268,7 +268,7 @@ typename Array<T, Alloc>::crefT
 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j,
                      octave_idx_type k) const
 {
-  return elem (compute_index (i, j, k));
+  return elem (compute_index_inline (i, j, k));
 }
 
 template <typename T, typename Alloc>
diff -r 33e44e5e6e34 liboctave/array/Array.h
--- a/liboctave/array/Array.h	Mon Feb 27 13:26:19 2023 -0500
+++ b/liboctave/array/Array.h	Mon Feb 27 13:49:44 2023 -0500
@@ -548,6 +548,74 @@ public:
   xelem (const Array<octave_idx_type>& ra_idx) const
   { return xelem (compute_index_unchecked (ra_idx)); }
 
+private:
+  octave_idx_type
+  compute_index_inline (octave_idx_type i, octave_idx_type j,
+                        octave_idx_type k) const
+  {
+    // Unlike compute_index we don't check which dimension that triggers
+    // the error to make the code path smoother.
+    //
+    // compute_index is called when there is any error, to give the
+    // proper error message or when the array does not have 3 dimensions.
+    octave_idx_type dim_0 = m_dimensions (0);
+    octave_idx_type dim_1 = m_dimensions (1);
+
+    octave_idx_type idx = (k*dim_1 + j)*dim_0 + i;
+
+    bool ok = ((m_dimensions.ndims () == 3) && (i >= 0) && (i < dim_0) &&
+               (j >= 0) && (j < dim_1) && (k >= 0) && (k < m_dimensions (2)));
+
+    if (OCTAVE_LIKELY (ok))
+      return idx;
+    else
+      return compute_index (i, j, k); // Called on ndim not 3 or errors
+  }
+
+  octave_idx_type
+  compute_index_inline (octave_idx_type i, octave_idx_type j) const
+  {
+    // Unlike compute_index we don't check which dimension that triggers
+    // the error to make the code path smoother.
+    //
+    // compute_index is called when there is any error, to give the
+    // proper error message or when the array does not have 2 dimension.
+    octave_idx_type dim_0 = m_dimensions (0);
+    octave_idx_type dim_1 = m_dimensions (1);
+
+    octave_idx_type idx = j*dim_0 + i;
+
+    bool ok = ((i >= 0) && (i < dim_0) && (j >= 0) &&
+               (j < dim_1) && (m_dimensions.ndims () == 2));
+
+    if (OCTAVE_LIKELY (ok))
+      return idx;
+    else
+      return compute_index (i, j); // Called on ndim not 2 or errors
+  }
+
+public:
+  T checkelem_value (octave_idx_type i, octave_idx_type j,
+                     octave_idx_type k) const
+  {
+    octave_idx_type idx = compute_index_inline(i, j, k);
+    return m_slice_data[idx];//xelem (idx);
+  }
+
+  T checkelem_value (octave_idx_type i, octave_idx_type j) const
+  {
+    octave_idx_type idx = compute_index_inline(i, j);
+    return xelem (idx);
+  }
+
+  T checkelem_value (octave_idx_type i) const
+  {
+    if (i >= 0 && i < m_slice_len)
+      return xelem (i);
+    else
+      return checkelem (i);
+  }
+
   // FIXME: would be nice to fix this so that we don't unnecessarily force
   //        a copy, but that is not so easy, and I see no clean way to do it.
 
diff -r 33e44e5e6e34 oct-conf-post-public.in.h
--- a/oct-conf-post-public.in.h	Mon Feb 27 13:26:19 2023 -0500
+++ b/oct-conf-post-public.in.h	Mon Feb 27 13:49:44 2023 -0500
@@ -104,6 +104,20 @@
 #  endif
 #endif
 
+/* Branch hint macros for use in if condititions.
+   Returns logical value of x. */
+#if defined (__GNUC__)
+#    define OCTAVE_LIKELY(x) __builtin_expect(!!(x),1)
+#else
+#    define OCTAVE_LIKELY(x) !!(x)
+#endif
+
+#if defined (__GNUC__)
+#  define OCTAVE_UNLIKELY(x) __builtin_expect(!!(x),0)
+#else
+#  define OCTAVE_UNLIKELY(x) !!(x)
+#endif
+
 #if defined (__cplusplus)
 template <typename T>
 static inline void
