Skip to content

Commit cabc106

Browse files
authored
Unrolled build for rust-lang#123089
Rollup merge of rust-lang#123089 - Philippe-Cholet:vecdeque_pop_assume_cap, r=Nilstrieb Add invariant to VecDeque::pop_* that len < cap if pop successful Similar to rust-lang#114370 for VecDeque instead of Vec. I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370). This is my first time with codegen tests, I based the test on what was done for Vec.
2 parents 75fd074 + 7a2678d commit cabc106

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
16141614
let old_head = self.head;
16151615
self.head = self.to_physical_idx(1);
16161616
self.len -= 1;
1617-
Some(unsafe { self.buffer_read(old_head) })
1617+
unsafe {
1618+
core::hint::assert_unchecked(self.len < self.capacity());
1619+
Some(self.buffer_read(old_head))
1620+
}
16181621
}
16191622
}
16201623

@@ -1638,7 +1641,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
16381641
None
16391642
} else {
16401643
self.len -= 1;
1641-
Some(unsafe { self.buffer_read(self.to_physical_idx(self.len)) })
1644+
unsafe {
1645+
core::hint::assert_unchecked(self.len < self.capacity());
1646+
Some(self.buffer_read(self.to_physical_idx(self.len)))
1647+
}
16421648
}
16431649
}
16441650

tests/codegen/vecdeque_pop_push.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
use std::collections::VecDeque;
6+
7+
#[no_mangle]
8+
// CHECK-LABEL: @noop_back(
9+
pub fn noop_back(v: &mut VecDeque<u8>) {
10+
// CHECK-NOT: grow
11+
// CHECK: tail call void @llvm.assume
12+
// CHECK-NOT: grow
13+
// CHECK: ret
14+
if let Some(x) = v.pop_back() {
15+
v.push_back(x);
16+
}
17+
}
18+
19+
#[no_mangle]
20+
// CHECK-LABEL: @noop_front(
21+
pub fn noop_front(v: &mut VecDeque<u8>) {
22+
// CHECK-NOT: grow
23+
// CHECK: tail call void @llvm.assume
24+
// CHECK-NOT: grow
25+
// CHECK: ret
26+
if let Some(x) = v.pop_front() {
27+
v.push_front(x);
28+
}
29+
}
30+
31+
#[no_mangle]
32+
// CHECK-LABEL: @move_byte_front_to_back(
33+
pub fn move_byte_front_to_back(v: &mut VecDeque<u8>) {
34+
// CHECK-NOT: grow
35+
// CHECK: tail call void @llvm.assume
36+
// CHECK-NOT: grow
37+
// CHECK: ret
38+
if let Some(x) = v.pop_front() {
39+
v.push_back(x);
40+
}
41+
}
42+
43+
#[no_mangle]
44+
// CHECK-LABEL: @move_byte_back_to_front(
45+
pub fn move_byte_back_to_front(v: &mut VecDeque<u8>) {
46+
// CHECK-NOT: grow
47+
// CHECK: tail call void @llvm.assume
48+
// CHECK-NOT: grow
49+
// CHECK: ret
50+
if let Some(x) = v.pop_back() {
51+
v.push_front(x);
52+
}
53+
}
54+
55+
#[no_mangle]
56+
// CHECK-LABEL: @push_back_byte(
57+
pub fn push_back_byte(v: &mut VecDeque<u8>) {
58+
// CHECK: call {{.*}}grow
59+
v.push_back(3);
60+
}
61+
62+
#[no_mangle]
63+
// CHECK-LABEL: @push_front_byte(
64+
pub fn push_front_byte(v: &mut VecDeque<u8>) {
65+
// CHECK: call {{.*}}grow
66+
v.push_front(3);
67+
}

0 commit comments

Comments
 (0)