From: Mike Blumenkrantz Date: Tue, 6 Oct 2020 20:30:47 +0000 (-0400) Subject: util/hash_table: add function for reserving size in a hash table X-Git-Tag: upstream/21.0.0~4493 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0b96b7bf10d00b13d5eed83f679430a4e48f0a01;p=platform%2Fupstream%2Fmesa.git 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 Reviewed-by: Eric Anholt Part-of: --- diff --git a/src/util/hash_table.c b/src/util/hash_table.c index 7b2b7eb..596bab4 100644 --- a/src/util/hash_table.c +++ b/src/util/hash_table.c @@ -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. * diff --git a/src/util/hash_table.h b/src/util/hash_table.h index eabc88a..d59e33f 100644 --- a/src/util/hash_table.h +++ b/src/util/hash_table.h @@ -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