diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index af2daabc2d02c..e71077c96c774 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -646,6 +646,20 @@ impl Vec { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn push(&mut self, value: T) { + #[cold] + #[inline(never)] + fn resize(vec: &mut Vec) { + let old_size = vec.cap * mem::size_of::(); + let size = max(old_size, 2 * mem::size_of::()) * 2; + if old_size > size { panic!("capacity overflow") } + unsafe { + let ptr = alloc_or_realloc(*vec.ptr, old_size, size); + if ptr.is_null() { ::alloc::oom() } + vec.ptr = Unique::new(ptr); + } + vec.cap = max(vec.cap, 2) * 2; + } + if mem::size_of::() == 0 { // zero-size types consume no memory, so we can't rely on the // address space running out @@ -653,16 +667,9 @@ impl Vec { unsafe { mem::forget(value); } return } + if self.len == self.cap { - let old_size = self.cap * mem::size_of::(); - let size = max(old_size, 2 * mem::size_of::()) * 2; - if old_size > size { panic!("capacity overflow") } - unsafe { - let ptr = alloc_or_realloc(*self.ptr, old_size, size); - if ptr.is_null() { ::alloc::oom() } - self.ptr = Unique::new(ptr); - } - self.cap = max(self.cap, 2) * 2; + resize(self); } unsafe {