Hashtable: use dynamic growth and use primes as size

Dynamically increase the hashmap size to not exceed the load factor and
avoid too long chains.

Switch from Separate Chaining to Robin Hood linear probing to improve
cache locality.

Use primes as size to further avoid collisions.

E.g. on a standard kde system the number of entries in the ProcessTable
might be around 650.
This commit is contained in:
Christian Göttsche
2020-10-22 15:43:26 +02:00
committed by BenBE
parent 7914ec201e
commit 307c34b028
3 changed files with 210 additions and 74 deletions

View File

@ -16,13 +16,13 @@ typedef void(*Hashtable_PairFunction)(hkey_t key, void* value, void* userdata);
typedef struct HashtableItem_ {
hkey_t key;
unsigned int probe;
void* value;
struct HashtableItem_* next;
} HashtableItem;
typedef struct Hashtable_ {
unsigned int size;
HashtableItem** buckets;
HashtableItem* buckets;
unsigned int items;
bool owner;
} Hashtable;
@ -37,6 +37,8 @@ Hashtable* Hashtable_new(unsigned int size, bool owner);
void Hashtable_delete(Hashtable* this);
void Hashtable_setSize(Hashtable* this, unsigned int size);
void Hashtable_put(Hashtable* this, hkey_t key, void* value);
void* Hashtable_remove(Hashtable* this, hkey_t key);