Skip to content

Commit afc20d7

Browse files
committed
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
1 parent 119f429 commit afc20d7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/raw/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,20 @@ impl<T> RawTable<T> {
652652
.checked_add(additional)
653653
.ok_or_else(|| fallability.capacity_overflow())?;
654654

655-
// Rehash in-place without re-allocating if we have plenty of spare
656-
// capacity that is locked up due to DELETED entries.
657-
if new_items < bucket_mask_to_capacity(self.bucket_mask) / 2 {
655+
let full_capacity = bucket_mask_to_capacity(self.bucket_mask);
656+
if new_items <= full_capacity / 2 {
657+
// Rehash in-place without re-allocating if we have plenty of spare
658+
// capacity that is locked up due to DELETED entries.
658659
self.rehash_in_place(hasher);
659660
Ok(())
660661
} else {
661-
self.resize(new_items, hasher, fallability)
662+
// Otherwise, conservatively resize to at least the next size up
663+
// to avoid churning deletes into frequent rehashes.
664+
self.resize(
665+
usize::max(new_items, full_capacity + 1),
666+
hasher,
667+
fallability,
668+
)
662669
}
663670
}
664671

0 commit comments

Comments
 (0)