Skip to content

Commit

Permalink
Avoid iteration when dropping HashMaps whose items don't need dropping
Browse files Browse the repository at this point in the history
This changes the performance of `drop` from linear to constant time for
such `HashMap`s.

Closes #31711.
  • Loading branch information
apasel422 committed Feb 16, 2016
1 parent 9658645 commit f890772
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/libstd/collections/hash/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>();
Expand Down

0 comments on commit f890772

Please sign in to comment.