Skip to content

Commit 68ff732

Browse files
committed
Keep rehashing if dist_from_ideal_bucket is > DIST_FROM_IDEAL_BUCKET_LIMIT during insertion (fix issue #52)
During insertion a check was done on dist_from_ideal_bucket to be sure it doesn't becomes bigger than DIST_FROM_IDEAL_BUCKET_LIMIT but this was only done during the robin swap. A check should also be done beforehand if we find an empty bucket otherwise the variable could overflow and lead to bugs. This commit adds this check.
1 parent 6775231 commit 68ff732

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

include/tsl/robin_hash.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
12311231
dist_from_ideal_bucket++;
12321232
}
12331233

1234-
if (rehash_on_extreme_load()) {
1234+
while (rehash_on_extreme_load(dist_from_ideal_bucket)) {
12351235
ibucket = bucket_for_hash(hash);
12361236
dist_from_ideal_bucket = 0;
12371237

@@ -1293,7 +1293,7 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
12931293
while (!m_buckets[ibucket].empty()) {
12941294
if (dist_from_ideal_bucket >
12951295
m_buckets[ibucket].dist_from_ideal_bucket()) {
1296-
if (dist_from_ideal_bucket >=
1296+
if (dist_from_ideal_bucket >
12971297
bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT) {
12981298
/**
12991299
* The number of probes is really high, rehash the map on the next
@@ -1379,8 +1379,11 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
13791379
*
13801380
* Return true if the table has been rehashed.
13811381
*/
1382-
bool rehash_on_extreme_load() {
1383-
if (m_grow_on_next_insert || size() >= m_load_threshold) {
1382+
bool rehash_on_extreme_load(distance_type curr_dist_from_ideal_bucket) {
1383+
if (m_grow_on_next_insert ||
1384+
curr_dist_from_ideal_bucket >
1385+
bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT ||
1386+
size() >= m_load_threshold) {
13841387
rehash_impl(GrowthPolicy::next_bucket_count());
13851388
m_grow_on_next_insert = false;
13861389

0 commit comments

Comments
 (0)