提交 928e2e23 编写于 作者: B bors

Auto merge of #23670 - cmr:vec-push-slowpath, r=pcwalton

Makes Vec::push considerably smaller: 25 instructions, rather than 42, on
x86_64.
......@@ -646,6 +646,20 @@ pub fn retain<F>(&mut self, mut f: F) where F: FnMut(&T) -> bool {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push(&mut self, value: T) {
#[cold]
#[inline(never)]
fn resize<T>(vec: &mut Vec<T>) {
let old_size = vec.cap * mem::size_of::<T>();
let size = max(old_size, 2 * mem::size_of::<T>()) * 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::<T>() == 0 {
// zero-size types consume no memory, so we can't rely on the
// address space running out
......@@ -653,16 +667,9 @@ pub fn push(&mut self, value: T) {
unsafe { mem::forget(value); }
return
}
if self.len == self.cap {
let old_size = self.cap * mem::size_of::<T>();
let size = max(old_size, 2 * mem::size_of::<T>()) * 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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册