diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index c99460a55c952..4110faa41b324 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,14 @@ impl Drop for Vec { // 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); } + unsafe { + // The branch on needs_drop() is an -O1 performance optimization. + // Without the branch, dropping Vec takes linear time. + if needs_drop::() { + for x in self.iter_mut() { + drop_in_place(x); + } + } } } // RawVec handles deallocation