From 0b96b7bf10d00b13d5eed83f679430a4e48f0a01 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Tue, 6 Oct 2020 16:30:47 -0400 Subject: [PATCH] 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: --- src/util/hash_table.c | 15 +++++++++++++++ src/util/hash_table.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/src/util/hash_table.c b/src/util/hash_table.c index 7b2b7eb0d6a..596bab4c322 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 eabc88a5906..d59e33ff9f6 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