Skip to content

Commit

Permalink
Fix issue #23. The 'distance_type' used to store the distance of a va…
Browse files Browse the repository at this point in the history
…lue in a bucket from its ideal bucket could overflow when a lot of collisions happened and the load_factor() stayed below REHASH_ON_HIGH_NB_PROBES__MIN_LOAD_FACTOR. We now rehash no matter the load factor if the distance becomes too high.
  • Loading branch information
Tessil committed Sep 25, 2019
1 parent 8d650f9 commit 73959d6
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions include/tsl/robin_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ class bucket_entry: public bucket_entry_hash<StoreHash> {
tsl_rh_assert(!empty());
value().~value_type();
}

public:
static const distance_type DIST_FROM_IDEAL_BUCKET_LIMIT = 512;
static_assert(DIST_FROM_IDEAL_BUCKET_LIMIT <= std::numeric_limits<distance_type>::max() - 1,
"DIST_FROM_IDEAL_BUCKET_LIMIT must be <= std::numeric_limits<distance_type>::max() - 1.");

private:
using storage = typename std::aligned_storage<sizeof(value_type), alignof(value_type)>::type;
Expand Down Expand Up @@ -1266,9 +1271,7 @@ class robin_hash: private Hash, private KeyEqual, private GrowthPolicy {

while(!m_buckets[ibucket].empty()) {
if(dist_from_ideal_bucket > m_buckets[ibucket].dist_from_ideal_bucket()) {
if(dist_from_ideal_bucket >= REHASH_ON_HIGH_NB_PROBES__NPROBES &&
load_factor() >= REHASH_ON_HIGH_NB_PROBES__MIN_LOAD_FACTOR)
{
if(dist_from_ideal_bucket >= bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT) {
/**
* The number of probes is really high, rehash the map on the next insert.
* Difficult to do now as rehash may throw an exception.
Expand Down Expand Up @@ -1376,10 +1379,6 @@ class robin_hash: private Hash, private KeyEqual, private GrowthPolicy {
"MAXIMUM_MIN_LOAD_FACTOR should be < MINIMUM_MAX_LOAD_FACTOR");

private:
static const distance_type REHASH_ON_HIGH_NB_PROBES__NPROBES = 128;
static constexpr float REHASH_ON_HIGH_NB_PROBES__MIN_LOAD_FACTOR = 0.15f;


/**
* Return an always valid pointer to an static empty bucket_entry with last_bucket() == true.
*/
Expand Down

0 comments on commit 73959d6

Please sign in to comment.