Hashtable: skip rehashing if the size is the same

Hashtable_setSize should not rehash if the actual size stays the same as
the number of buckets and natural positions stay the same too.
This commit is contained in:
Denis Lisov 2021-12-12 15:19:07 +03:00 committed by cgzones
parent bc08c7dc2a
commit d084a80023
1 changed files with 5 additions and 1 deletions

View File

@ -191,10 +191,14 @@ void Hashtable_setSize(Hashtable* this, size_t size) {
if (size <= this->items) if (size <= this->items)
return; return;
size_t newSize = nextPrime(size);
if (newSize == this->size)
return;
HashtableItem* oldBuckets = this->buckets; HashtableItem* oldBuckets = this->buckets;
size_t oldSize = this->size; size_t oldSize = this->size;
this->size = nextPrime(size); this->size = newSize;
this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem)); this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
this->items = 0; this->items = 0;