11//! This module contains the logic for pivot selection.
22
3- use crate :: intrinsics;
3+ use crate :: { hint , intrinsics} ;
44
55// Recursively select a pseudomedian if above this threshold.
66const PSEUDO_MEDIAN_REC_THRESHOLD : usize = 64 ;
@@ -9,6 +9,7 @@ const PSEUDO_MEDIAN_REC_THRESHOLD: usize = 64;
99///
1010/// This chooses a pivot by sampling an adaptive amount of points, approximating
1111/// the quality of a median of sqrt(n) elements.
12+ #[ inline]
1213pub fn choose_pivot < T , F : FnMut ( & T , & T ) -> bool > ( v : & [ T ] , is_less : & mut F ) -> usize {
1314 // We use unsafe code and raw pointers here because we're dealing with
1415 // heavy recursion. Passing safe slices around would involve a lot of
@@ -22,7 +23,7 @@ pub fn choose_pivot<T, F: FnMut(&T, &T) -> bool>(v: &[T], is_less: &mut F) -> us
2223 // SAFETY: a, b, c point to initialized regions of len_div_8 elements,
2324 // satisfying median3 and median3_rec's preconditions as v_base points
2425 // to an initialized region of n = len elements.
25- unsafe {
26+ let index = unsafe {
2627 let v_base = v. as_ptr ( ) ;
2728 let len_div_8 = len / 8 ;
2829
@@ -35,6 +36,11 @@ pub fn choose_pivot<T, F: FnMut(&T, &T) -> bool>(v: &[T], is_less: &mut F) -> us
3536 } else {
3637 median3_rec ( a, b, c, len_div_8, is_less) . offset_from_unsigned ( v_base)
3738 }
39+ } ;
40+ // SAFETY: preconditions must have been met for offset_from_unsigned()
41+ unsafe {
42+ hint:: assert_unchecked ( index < v. len ( ) ) ;
43+ index
3844 }
3945}
4046
0 commit comments