Skip to content

Commit

Permalink
Resize with a more conservative amount of space when inserting after …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
edre committed Jun 5, 2019
1 parent 119f429 commit afc20d7
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/raw/mod.rs
Expand Up @@ -652,13 +652,20 @@ impl<T> RawTable<T> {
.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,
)
}
}

Expand Down

0 comments on commit afc20d7

Please sign in to comment.