Skip to content

Commit

Permalink
Auto merge of rust-lang#136159 - urben1680:urben1680-vecdeque-partiti…
Browse files Browse the repository at this point in the history
…on-point-optimization, r=<try>

`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.
  • Loading branch information
bors committed Jan 28, 2025
2 parents fdd1a3b + 6c3de4f commit 7ec1805
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2782,10 +2782,14 @@ impl<T, A: Allocator> VecDeque<T, A> {
where
P: FnMut(&T) -> bool,
{
let (front, back) = self.as_slices();
let (front, mut back) = self.as_slices();

if let Some(true) = back.first().map(|v| pred(v)) {
back.partition_point(pred) + front.len()
back = unsafe {
// SAFETY: if clause has proven a first element to skip exists
back.get_unchecked(1..)
};
back.partition_point(pred) + front.len() + 1
} else {
front.partition_point(pred)
}
Expand Down

0 comments on commit 7ec1805

Please # to comment.