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) }