Skip to content

Commit c5fa777

Browse files
committed
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.
1 parent fd38a75 commit c5fa777

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/libcollections/vec.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use alloc::heap::EMPTY;
6565
use core::cmp::Ordering;
6666
use core::fmt;
6767
use core::hash::{self, Hash};
68-
use core::intrinsics::{arith_offset, assume, drop_in_place};
68+
use core::intrinsics::{arith_offset, assume, drop_in_place, needs_drop};
6969
use core::iter::FromIterator;
7070
use core::mem;
7171
use core::ops::{Index, IndexMut, Deref};
@@ -1322,8 +1322,10 @@ impl<T> Drop for Vec<T> {
13221322
// OK because exactly when this stops being a valid assumption, we
13231323
// don't need unsafe_no_drop_flag shenanigans anymore.
13241324
if self.buf.unsafe_no_drop_flag_needs_drop() {
1325-
for x in self.iter_mut() {
1326-
unsafe { drop_in_place(x); }
1325+
if unsafe { needs_drop::<T>() } {
1326+
for x in self.iter_mut() {
1327+
unsafe { drop_in_place(x); }
1328+
}
13271329
}
13281330
}
13291331
// RawVec handles deallocation

0 commit comments

Comments
 (0)