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>
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_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 @@
 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