mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
Tell LLVM that partition_point
returns a valid fencepost
This was already done for a successful `binary_search`, but this way `partition_point` can get similar optimizations.
This commit is contained in:
parent
fe217c28ff
commit
c7af338e6f
@ -2426,15 +2426,20 @@ impl<T> [T] {
|
|||||||
where
|
where
|
||||||
F: FnMut(&'a T) -> Ordering,
|
F: FnMut(&'a T) -> Ordering,
|
||||||
{
|
{
|
||||||
|
// INVARIANTS:
|
||||||
|
// - 0 <= left <= left + size = right <= self.len()
|
||||||
|
// - f returns Less for everything in self[..left]
|
||||||
|
// - f returns Greater for everything in self[right..]
|
||||||
let mut size = self.len();
|
let mut size = self.len();
|
||||||
let mut left = 0;
|
let mut left = 0;
|
||||||
let mut right = size;
|
let mut right = size;
|
||||||
while left < right {
|
while left < right {
|
||||||
let mid = left + size / 2;
|
let mid = left + size / 2;
|
||||||
|
|
||||||
// SAFETY: the call is made safe by the following invariants:
|
// SAFETY: the while condition means `size` is strictly positive, so
|
||||||
// - `mid >= 0`
|
// `size/2 < size`. Thus `left + size/2 < left + size`, which
|
||||||
// - `mid < size`: `mid` is limited by `[left; right)` bound.
|
// coupled with the `left + size <= self.len()` invariant means
|
||||||
|
// we have `left + size/2 < self.len()`, and this is in-bounds.
|
||||||
let cmp = f(unsafe { self.get_unchecked(mid) });
|
let cmp = f(unsafe { self.get_unchecked(mid) });
|
||||||
|
|
||||||
// The reason why we use if/else control flow rather than match
|
// The reason why we use if/else control flow rather than match
|
||||||
@ -2452,6 +2457,10 @@ impl<T> [T] {
|
|||||||
|
|
||||||
size = right - left;
|
size = right - left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SAFETY: directly true from the overall invariant.
|
||||||
|
// Note that this is `<=`, unlike the assume in the `Ok` path.
|
||||||
|
unsafe { crate::intrinsics::assume(left <= self.len()) };
|
||||||
Err(left)
|
Err(left)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,3 +16,23 @@ pub fn binary_search_index_no_bounds_check(s: &[u8]) -> u8 {
|
|||||||
42
|
42
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Similarly, check that `partition_point` is known to return a valid fencepost.
|
||||||
|
|
||||||
|
// CHECK-LABEL: @unknown_split
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn unknown_split(x: &[i32], i: usize) -> (&[i32], &[i32]) {
|
||||||
|
// This just makes sure that the subsequent function is looking for the
|
||||||
|
// absence of something that might actually be there.
|
||||||
|
|
||||||
|
// CHECK: call core::panicking::panic
|
||||||
|
x.split_at(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CHECK-LABEL: @partition_point_split_no_bounds_check
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn partition_point_split_no_bounds_check(x: &[i32], needle: i32) -> (&[i32], &[i32]) {
|
||||||
|
// CHECK-NOT: call core::panicking::panic
|
||||||
|
let i = x.partition_point(|p| p < &needle);
|
||||||
|
x.split_at(i)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user