Skip to content

Commit 6c3de4f

Browse files
authored
VecDeque::partition_point checks first element in back slice only once
I noticed that the first element of the `back` slice is potentially checked twice for the `partition_point` method, first in an if-condition and second inside the respective body. In this change, the back slice is reduced by the first element and this offset is added to the result. I am not sure how to trigger benchmarks for this, I assume doing this PR is the way to go for bors. Please tell me if this is not how pull requests should be done.
1 parent ebcf860 commit 6c3de4f

File tree

1 file changed

+6
-2
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+6
-2
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2782,10 +2782,14 @@ impl<T, A: Allocator> VecDeque<T, A> {
27822782
where
27832783
P: FnMut(&T) -> bool,
27842784
{
2785-
let (front, back) = self.as_slices();
2785+
let (front, mut back) = self.as_slices();
27862786

27872787
if let Some(true) = back.first().map(|v| pred(v)) {
2788-
back.partition_point(pred) + front.len()
2788+
back = unsafe {
2789+
// SAFETY: if clause has proven a first element to skip exists
2790+
back.get_unchecked(1..)
2791+
};
2792+
back.partition_point(pred) + front.len() + 1
27892793
} else {
27902794
front.partition_point(pred)
27912795
}

0 commit comments

Comments
 (0)