about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCédric Barreteau <cbarrete@users.noreply.github.com>2021-11-28 17:59:17 -0500
committerCédric Barreteau <cbarrete@users.noreply.github.com>2021-11-29 21:14:20 -0500
commit29f5c98a1737b4fd86cd56ebbc5e1a5cab68d9ca (patch)
treebca3eef5ac501fc1db275bb3f5b85dad60b9503e
parent6db0a0e9a4a2f55b1a85954e114ada0b45c32e45 (diff)
downloadrust-29f5c98a1737b4fd86cd56ebbc5e1a5cab68d9ca.tar.gz
rust-29f5c98a1737b4fd86cd56ebbc5e1a5cab68d9ca.zip
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`.
-rw-r--r--library/alloc/src/collections/vec_deque/mod.rs20
1 files changed, 11 insertions, 9 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs
index de607c8fdab..00862615c3c 100644
--- a/library/alloc/src/collections/vec_deque/mod.rs
+++ b/library/alloc/src/collections/vec_deque/mod.rs
@@ -2179,19 +2179,21 @@ impl<T, A: Allocator> VecDeque<T, A> {
         }
     }
 
+    // Double the buffer size. This method is inline(never), so we expect it to only
+    // be called in cold paths.
     // This may panic or abort
     #[inline(never)]
     fn grow(&mut self) {
-        if self.is_full() {
-            let old_cap = self.cap();
-            // Double the buffer size.
-            self.buf.reserve_exact(old_cap, old_cap);
-            assert!(self.cap() == old_cap * 2);
-            unsafe {
-                self.handle_capacity_increase(old_cap);
-            }
-            debug_assert!(!self.is_full());
+        // Extend or possibly remove this assertion when valid use-cases for growing the
+        // buffer without it being full emerge
+        debug_assert!(self.is_full());
+        let old_cap = self.cap();
+        self.buf.reserve_exact(old_cap, old_cap);
+        assert!(self.cap() == old_cap * 2);
+        unsafe {
+            self.handle_capacity_increase(old_cap);
         }
+        debug_assert!(!self.is_full());
     }
 
     /// Modifies the `VecDeque` in-place so that `len()` is equal to `new_len`,