From f890772383c900f9a409a4105df13b6630ebb1f5 Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Tue, 16 Feb 2016 18:20:35 -0500 Subject: [PATCH] Avoid iteration when dropping `HashMap`s whose items don't need dropping This changes the performance of `drop` from linear to constant time for such `HashMap`s. Closes #31711. --- src/libstd/collections/hash/table.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 316c7595266..97cab94b67b 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -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 Drop for RawTable { // 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::();