From afc20d7cdccdc3605ff1f16d7b9097b4d64fc564 Mon Sep 17 00:00:00 2001 From: Eric Roshan-Eisner Date: Mon, 3 Jun 2019 21:47:13 -0700 Subject: [PATCH] Resize with a more conservative amount of space when inserting after deletions. Otherwise we can force a rehash for each insert and removal if we're right under the size limit. Benchmark with SIZE=895, just under a rehash limit. name oops ns/iter okthen ns/iter diff ns/iter diff % speedup insert_erase_std_serial 9,875,585 60,696 -9,814,889 -99.39% x 162.71 Fixes #85 --- src/raw/mod.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/raw/mod.rs b/src/raw/mod.rs index 822b5c9c31..2250e36c79 100644 --- a/src/raw/mod.rs +++ b/src/raw/mod.rs @@ -652,13 +652,20 @@ impl RawTable { .checked_add(additional) .ok_or_else(|| fallability.capacity_overflow())?; - // Rehash in-place without re-allocating if we have plenty of spare - // capacity that is locked up due to DELETED entries. - if new_items < bucket_mask_to_capacity(self.bucket_mask) / 2 { + let full_capacity = bucket_mask_to_capacity(self.bucket_mask); + if new_items <= full_capacity / 2 { + // Rehash in-place without re-allocating if we have plenty of spare + // capacity that is locked up due to DELETED entries. self.rehash_in_place(hasher); Ok(()) } else { - self.resize(new_items, hasher, fallability) + // Otherwise, conservatively resize to at least the next size up + // to avoid churning deletes into frequent rehashes. + self.resize( + usize::max(new_items, full_capacity + 1), + hasher, + fallability, + ) } }