From 20f2cdf56652ed88b0dfd7398ff191e89a6e6d09 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 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/raw/mod.rs b/src/raw/mod.rs index 822b5c9c31..e9c9c580ac 100644 --- a/src/raw/mod.rs +++ b/src/raw/mod.rs @@ -652,11 +652,16 @@ 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 if additional == 1 { + // For inserts, just allocate more space to avoid churning deletes + // into frequent rehashes. + self.resize(full_capacity + 1, hasher, fallability) } else { self.resize(new_items, hasher, fallability) }