]> git.lizzy.rs Git - rust.git/commitdiff
Switch Vec from doubling size on growth to using RawVec's reserve
authorgnzlbg <gonzalobg88@gmail.com>
Mon, 14 May 2018 11:58:28 +0000 (13:58 +0200)
committergnzlbg <gonzalobg88@gmail.com>
Mon, 14 May 2018 11:58:28 +0000 (13:58 +0200)
On growth, Vec does not require to exactly double its size for correctness,
like, for example, VecDeque does.

Using reserve instead better expresses this intent. It also allows to reuse
Excess capacity on growth and for better growth-policies to be provided by
RawVec.

src/liballoc/vec.rs

index 690cbcb559bbf6099f4e67b0907bebf9e01051c6..ffaff20bcc9f265c26da16efe2f935332ac0549a 100644 (file)
@@ -840,7 +840,7 @@ pub fn insert(&mut self, index: usize, element: T) {
 
         // space for the new element
         if len == self.buf.cap() {
-            self.buf.double();
+            self.reserve(1);
         }
 
         unsafe {
@@ -1060,7 +1060,7 @@ pub fn push(&mut self, value: T) {
         // This will panic or abort if we would allocate > isize::MAX bytes
         // or if the length increment would overflow for zero-sized types.
         if self.len == self.buf.cap() {
-            self.buf.double();
+            self.reserve(1);
         }
         unsafe {
             let end = self.as_mut_ptr().offset(self.len as isize);