util/hash_table: add function for reserving size in a hash table

rehashing a populated hash table is very expensive, so for the case where
the maximum/likely table size is already known, this function allows for
pre-sizing the table to avoid ever needing a rehash

Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7037>
This commit is contained in:
Mike Blumenkrantz 2020-10-06 16:30:47 -04:00 committed by Marge Bot
parent 06a5edf247
commit 0b96b7bf10
2 changed files with 17 additions and 0 deletions

View file

@ -660,6 +660,21 @@ _mesa_pointer_hash_table_create(void *mem_ctx)
_mesa_key_pointer_equal);
}
bool
_mesa_hash_table_reserve(struct hash_table *ht, unsigned size)
{
if (size < ht->max_entries)
return true;
for (unsigned i = ht->size_index + 1; i < ARRAY_SIZE(hash_sizes); i++) {
if (hash_sizes[i].max_entries >= size) {
_mesa_hash_table_rehash(ht, i);
break;
}
}
return ht->max_entries >= size;
}
/**
* Hash table wrapper which supports 64-bit keys.
*

View file

@ -124,6 +124,8 @@ bool _mesa_key_pointer_equal(const void *a, const void *b);
struct hash_table *
_mesa_pointer_hash_table_create(void *mem_ctx);
bool
_mesa_hash_table_reserve(struct hash_table *ht, unsigned size);
/**
* This foreach function is safe against deletion (which just replaces
* an entry's data with the deleted marker), but not against insertion