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 4, 2019
1 parent 119f429 commit 20f2cdf
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/raw/mod.rs
Expand Up @@ -652,11 +652,16 @@ 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 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)
}
Expand Down

0 comments on commit 20f2cdf

Please sign in to comment.