[Mesa-dev] [PATCH 2/4] util: Change hash_table to use quadratic probing
Eric Anholt
eric at anholt.net
Mon Mar 16 15:46:02 PDT 2015
Thomas Helland <thomashelland90 at gmail.com> writes:
> This should give better cache locality, less memory consumption,
> and should also be faster since we avoid a modulo operation.
> Also change table size to be power of two.
> This gives better performance as we can do bitmasking instead of
> modulo operations for fitting the hash in the address space.
> By using the algorithm hash = sh + i/2 + i*i/2
> ee are guaranteed that all retries from the quad probing
> are distinct, and so should be able to completely fill the table.
> This passes the test added to exercise a worst case collision scenario.
> ---
> src/util/hash_table.c | 101 +++++++++++++++++++++++++-------------------------
> src/util/hash_table.h | 1 -
> 2 files changed, 50 insertions(+), 52 deletions(-)
>
> diff --git a/src/util/hash_table.c b/src/util/hash_table.c
> index 3247593..92ffc10 100644
> --- a/src/util/hash_table.c
> +++ b/src/util/hash_table.c
> @@ -33,7 +33,7 @@
> */
>
> /**
> - * Implements an open-addressing, linear-reprobing hash table.
> + * Implements an open-addressing, quadratic probing hash table.
> *
> * For more information, see:
> *
> @@ -51,44 +51,45 @@
> static const uint32_t deleted_key_value;
>
> /**
> - * From Knuth -- a good choice for hash/rehash values is p, p-2 where
> - * p and p-2 are both prime. These tables are sized to have an extra 10%
> - * free to avoid exponential performance degradation as the hash table fills
> + * We chose table sizes that's a power of two.
> + * This is computationally less expensive than primes.
> + * FNV-1a has good avalanche properties, so collision is not an issue.
> + * These tables are sized to have an extra 10% free to avoid
> + * exponential performance degradation as the hash table fills
> */
> static const struct {
> - uint32_t max_entries, size, rehash;
> + uint32_t max_entries, size;
> } hash_sizes[] = {
> - { 2, 5, 3 },
> - { 4, 7, 5 },
> - { 8, 13, 11 },
> - { 16, 19, 17 },
> - { 32, 43, 41 },
> - { 64, 73, 71 },
> - { 128, 151, 149 },
> - { 256, 283, 281 },
> - { 512, 571, 569 },
> - { 1024, 1153, 1151 },
> - { 2048, 2269, 2267 },
> - { 4096, 4519, 4517 },
> - { 8192, 9013, 9011 },
> - { 16384, 18043, 18041 },
> - { 32768, 36109, 36107 },
> - { 65536, 72091, 72089 },
> - { 131072, 144409, 144407 },
> - { 262144, 288361, 288359 },
> - { 524288, 576883, 576881 },
> - { 1048576, 1153459, 1153457 },
> - { 2097152, 2307163, 2307161 },
> - { 4194304, 4613893, 4613891 },
> - { 8388608, 9227641, 9227639 },
> - { 16777216, 18455029, 18455027 },
> - { 33554432, 36911011, 36911009 },
> - { 67108864, 73819861, 73819859 },
> - { 134217728, 147639589, 147639587 },
> - { 268435456, 295279081, 295279079 },
> - { 536870912, 590559793, 590559791 },
> - { 1073741824, 1181116273, 1181116271},
> - { 2147483648ul, 2362232233ul, 2362232231ul}
> + { 3, 4 },
> + { 7, 8 },
> + { 14, 16 },
> + { 28, 32 },
> + { 57, 64 },
> + { 115, 128 },
> + { 230, 256 },
> + { 460, 512 },
> + { 921, 1024 },
> + { 1843, 2048 },
> + { 3686, 4096 },
> + { 7372, 8192 },
> + { 14745, 16384 },
> + { 29491, 32768 },
> + { 58982, 65536 },
> + { 117964, 131072 },
> + { 235929, 262144 },
> + { 471859, 524288 },
> + { 943718, 1048576 },
> + { 1887436, 2097152 },
> + { 3774873, 4194304 },
> + { 7549747, 8388608 },
> + { 15099494, 16777216 },
> + { 30198988, 33554432 },
> + { 60397977, 67108864 },
> + { 120795955, 134217728 },
> + { 241591910, 268435456 },
> + { 483183820, 536870912 },
> + { 966367641, 1073741824 },
> + { 1932735283ul, 2147483648ul }
> };
>
> static int
> @@ -123,7 +124,6 @@ _mesa_hash_table_create(void *mem_ctx,
>
> ht->size_index = 0;
> ht->size = hash_sizes[ht->size_index].size;
> - ht->rehash = hash_sizes[ht->size_index].rehash;
> ht->max_entries = hash_sizes[ht->size_index].max_entries;
> ht->key_hash_function = key_hash_function;
> ht->key_equals_function = key_equals_function;
> @@ -182,12 +182,12 @@ _mesa_hash_table_set_deleted_key(struct hash_table *ht, const void *deleted_key)
> static struct hash_entry *
> hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)
> {
> - uint32_t start_hash_address = hash % ht->size;
> + uint32_t start_hash_address = hash & (ht->size - 1);
> uint32_t hash_address = start_hash_address;
> + // Start at 2, or we will match start_hash_address initially and bail
> + uint32_t quad_hash = 2;
>
> do {
> - uint32_t double_hash;
> -
> struct hash_entry *entry = ht->table + hash_address;
>
> if (entry_is_free(entry)) {
> @@ -198,9 +198,9 @@ hash_table_search(struct hash_table *ht, uint32_t hash, const void *key)
> }
> }
>
> - double_hash = 1 + hash % ht->rehash;
> -
> - hash_address = (hash_address + double_hash) % ht->size;
> + hash_address = (start_hash_address + (quad_hash / 2) +
> + ((quad_hash * quad_hash) / 2)) & (ht->size - 1);
> + quad_hash++;
I'm assuming you wanted the sequence quoted at:
http://en.wikipedia.org/wiki/Quadratic_probing
But looking at the iterations of your quad_hash, since you're using
integer math:
(2 / 2) + (2 * 2) / 2 = 1 + 2 = 3
(3 / 2) + (3 * 3) / 2 = 1 + 4 = 5
(4 / 2) + (4 * 4) / 2 = 2 + 8 = 10
(5 / 2) + (5 * 5) / 2 = 2 + 12 = 14
I think what you wanted as your probe is:
uint32_t double_hash = 1;
...
hash_address = (start_hash_address + ((quad_hash + quad_hash *
quad_hash) >> 1)) & (ht->size - 1)
quad_hash++;
Then you get:
(1 + 1 * 1) / 2 = 3 / 2 = 1
(2 + 2 * 2) / 2 = 6 / 2 = 3
(3 + 3 * 3) / 2 = 12 / 2 = 6
(4 + 4 * 4) / 2 = 20 / 2 = 10
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20150316/4d11eb92/attachment-0001.sig>
More information about the mesa-dev
mailing list