Avoid iteration when dropping HashMaps whose items don't need dropping

This changes the performance of `drop` from linear to constant time for
such `HashMap`s.

Closes #31711.
This commit is contained in:
Andrew Paseltiner 2016-02-16 18:20:35 -05:00
parent 9658645407
commit f890772383

View File

@ -12,6 +12,7 @@ use alloc::heap::{allocate, deallocate, EMPTY};
use cmp;
use hash::{Hash, Hasher, BuildHasher};
use intrinsics::needs_drop;
use marker;
use mem::{align_of, size_of};
use mem;
@ -1009,7 +1010,9 @@ impl<K, V> Drop for RawTable<K, V> {
// dropping empty tables such as on resize.
// Also avoid double drop of elements that have been already moved out.
unsafe {
for _ in self.rev_move_buckets() {}
if needs_drop::<(K, V)>() { // avoid linear runtime for types that don't need drop
for _ in self.rev_move_buckets() {}
}
}
let hashes_size = self.capacity * size_of::<u64>();