about summary refs log tree commit diff
diff options
context:
space:
mode:
authorwhitequark <whitequark@whitequark.org>2015-09-20 11:04:01 +0300
committerwhitequark <whitequark@whitequark.org>2015-09-20 11:17:19 +0300
commitc5fa7776dff913dd75fed6f4e7ed483f0e75e367 (patch)
tree71cb0654c7d01478de497e5671728e08355bb6ef
parentfd38a75077a4c5efc87413b7f9f7f1b6bc9db9af (diff)
downloadrust-c5fa7776dff913dd75fed6f4e7ed483f0e75e367.tar.gz
rust-c5fa7776dff913dd75fed6f4e7ed483f0e75e367.zip
Do not drop_in_place elements of Vec<T> if T doesn't need dropping
With -O2, LLVM's inliner can remove this code, but this does not happen
with -O1 and lower. As a result, dropping Vec<u8> was linear with length,
resulting in abysmal performance for large buffers.
-rw-r--r--src/libcollections/vec.rs8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index c99460a55c9..fac84617943 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -65,7 +65,7 @@ use alloc::heap::EMPTY;
 use core::cmp::Ordering;
 use core::fmt;
 use core::hash::{self, Hash};
-use core::intrinsics::{arith_offset, assume, drop_in_place};
+use core::intrinsics::{arith_offset, assume, drop_in_place, needs_drop};
 use core::iter::FromIterator;
 use core::mem;
 use core::ops::{Index, IndexMut, Deref};
@@ -1322,8 +1322,10 @@ impl<T> Drop for Vec<T> {
         // OK because exactly when this stops being a valid assumption, we
         // don't need unsafe_no_drop_flag shenanigans anymore.
         if self.buf.unsafe_no_drop_flag_needs_drop() {
-            for x in self.iter_mut() {
-                unsafe { drop_in_place(x); }
+            if unsafe { needs_drop::<T>() } {
+                for x in self.iter_mut() {
+                    unsafe { drop_in_place(x); }
+                }
             }
         }
         // RawVec handles deallocation