Skip to content

Commit 190367b

Browse files
committed
Auto merge of rust-lang#91339 - cbarrete:vecdeque-remove-grow-check, r=Mark-Simulacrum
Remove unnecessary check in VecDeque::grow All callers already check that the buffer is full before calling `grow()`. This is where it makes the most sense, since `grow()` is `inline(never)` and we don't want to pay for a function call just for that check. It could also be argued that it would be correct to call `grow()` even if the buffer wasn't full yet. This change breaks no code since `grow()` is not `pub`.
2 parents ff23ad3 + 29f5c98 commit 190367b

File tree

1 file changed

+11
-9
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+11
-9
lines changed

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -2179,19 +2179,21 @@ impl<T, A: Allocator> VecDeque<T, A> {
21792179
}
21802180
}
21812181

2182+
// Double the buffer size. This method is inline(never), so we expect it to only
2183+
// be called in cold paths.
21822184
// This may panic or abort
21832185
#[inline(never)]
21842186
fn grow(&mut self) {
2185-
if self.is_full() {
2186-
let old_cap = self.cap();
2187-
// Double the buffer size.
2188-
self.buf.reserve_exact(old_cap, old_cap);
2189-
assert!(self.cap() == old_cap * 2);
2190-
unsafe {
2191-
self.handle_capacity_increase(old_cap);
2192-
}
2193-
debug_assert!(!self.is_full());
2187+
// Extend or possibly remove this assertion when valid use-cases for growing the
2188+
// buffer without it being full emerge
2189+
debug_assert!(self.is_full());
2190+
let old_cap = self.cap();
2191+
self.buf.reserve_exact(old_cap, old_cap);
2192+
assert!(self.cap() == old_cap * 2);
2193+
unsafe {
2194+
self.handle_capacity_increase(old_cap);
21942195
}
2196+
debug_assert!(!self.is_full());
21952197
}
21962198

21972199
/// Modifies the `VecDeque` in-place so that `len()` is equal to `new_len`,

0 commit comments

Comments
 (0)