From 6c3de4fc2a58f23b11242cc4d813e494c17cd267 Mon Sep 17 00:00:00 2001 From: urben1680 <55257931+urben1680@users.noreply.github.com> Date: Mon, 27 Jan 2025 23:54:51 +0100 Subject: [PATCH] `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. --- library/alloc/src/collections/vec_deque/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 1c33f8f60d82..8d50ad4fd96e 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2782,10 +2782,14 @@ impl VecDeque { 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) }