<div dir="ltr"><p dir="ltr"><br>
On 14 Apr 2015 00:05, "Jason Ekstrand" <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>> wrote:<br>
><br>
> On Sat, Apr 11, 2015 at 4:25 PM, Thomas Helland<br>
> <<a href="mailto:thomashelland90@gmail.com" target="_blank">thomashelland90@gmail.com</a>> wrote:<br>
> > The same rationale applies here as for the hash table.<br>
> > Power of two size should give better performance,<br>
> > and using the algorithm hash = sh + i/2 + i*i/2<br>
> > should result in only distinct hash values when hitting collisions.<br>
> ><br>
> > Difference at 95.0% confidence<br>
> > -7.9505 +/- 2.44011<br>
> > -5.04357% +/- 1.54794%<br>
> ><br>
> > V3: Feedback from Eric Anholt<br>
> > - Don't change load factor and starting size.<br>
> ><br>
> > V2: Feedback from Connor Abbott<br>
> > - Don't set initial hash address before potential rehash<br>
> > - Remove hash_sizes table<br>
> > - Correct the quadratic hashing algorithm<br>
> > - Use correct comment style<br>
> ><br>
> > Feedback from Jason Ekstrand<br>
> > - Use unreachable() to detect if we fail to insert<br>
> ><br>
> > Signed-off-by: Thomas Helland <<a href="mailto:thomashelland90@gmail.com" target="_blank">thomashelland90@gmail.com</a>><br>
> > ---<br>
> > src/util/set.c | 118 ++++++++++++++++++++-------------------------------------<br>
> > src/util/set.h | 3 +-<br>
> > 2 files changed, 43 insertions(+), 78 deletions(-)<br>
> ><br>
> > diff --git a/src/util/set.c b/src/util/set.c<br>
> > index f01f869..7ff9520 100644<br>
> > --- a/src/util/set.c<br>
> > +++ b/src/util/set.c<br>
> > @@ -32,6 +32,17 @@<br>
> > * Keith Packard <<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>><br>
> > */<br>
> ><br>
> > +/**<br>
> > + * Implements an open-addressing, quadratic probing hash-set.<br>
> > + *<br>
> > + * We choose set sizes that's a power of two.<br>
> > + * This is computationally less expensive than primes.<br>
> > + * As a bonus the size and free space can be calculated instead of looked up.<br>
> > + * FNV-1a has good avalanche properties, so collision is not an issue.<br>
> > + * These sets are sized to have an extra 10% free to avoid<br>
> > + * exponential performance degradation as the set fills.<br>
> > + */<br>
> > +<br>
> > #include <stdlib.h><br>
> > #include <assert.h><br>
> ><br>
> > @@ -39,51 +50,9 @@<br>
> > #include "ralloc.h"<br>
> > #include "set.h"<br>
> ><br>
> > -/*<br>
> > - * From Knuth -- a good choice for hash/rehash values is p, p-2 where<br>
> > - * p and p-2 are both prime. These tables are sized to have an extra 10%<br>
> > - * free to avoid exponential performance degradation as the hash table fills<br>
> > - */<br>
> > -<br>
> > uint32_t deleted_key_value;<br>
> > const void *deleted_key = &deleted_key_value;<br>
> ><br>
> > -static const struct {<br>
> > - uint32_t max_entries, size, rehash;<br>
> > -} hash_sizes[] = {<br>
> > - { 2, 5, 3 },<br>
> > - { 4, 7, 5 },<br>
> > - { 8, 13, 11 },<br>
> > - { 16, 19, 17 },<br>
> > - { 32, 43, 41 },<br>
> > - { 64, 73, 71 },<br>
> > - { 128, 151, 149 },<br>
> > - { 256, 283, 281 },<br>
> > - { 512, 571, 569 },<br>
> > - { 1024, 1153, 1151 },<br>
> > - { 2048, 2269, 2267 },<br>
> > - { 4096, 4519, 4517 },<br>
> > - { 8192, 9013, 9011 },<br>
> > - { 16384, 18043, 18041 },<br>
> > - { 32768, 36109, 36107 },<br>
> > - { 65536, 72091, 72089 },<br>
> > - { 131072, 144409, 144407 },<br>
> > - { 262144, 288361, 288359 },<br>
> > - { 524288, 576883, 576881 },<br>
> > - { 1048576, 1153459, 1153457 },<br>
> > - { 2097152, 2307163, 2307161 },<br>
> > - { 4194304, 4613893, 4613891 },<br>
> > - { 8388608, 9227641, 9227639 },<br>
> > - { 16777216, 18455029, 18455027 },<br>
> > - { 33554432, 36911011, 36911009 },<br>
> > - { 67108864, 73819861, 73819859 },<br>
> > - { 134217728, 147639589, 147639587 },<br>
> > - { 268435456, 295279081, 295279079 },<br>
> > - { 536870912, 590559793, 590559791 },<br>
> > - { 1073741824, 1181116273, 1181116271 },<br>
> > - { 2147483648ul, 2362232233ul, 2362232231ul }<br>
> > -};<br>
> > -<br>
> > static int<br>
> > entry_is_free(struct set_entry *entry)<br>
> > {<br>
> > @@ -114,10 +83,9 @@ _mesa_set_create(void *mem_ctx,<br>
> > if (ht == NULL)<br>
> > return NULL;<br>
> ><br>
> > - ht->size_index = 0;<br>
> > - ht->size = hash_sizes[ht->size_index].size;<br>
> > - ht->rehash = hash_sizes[ht->size_index].rehash;<br>
> > - ht->max_entries = hash_sizes[ht->size_index].max_entries;<br>
> > + ht->size_iteration = 2;<br>
> > + ht->size = 1 << ht->size_iteration;<br>
> > + ht->max_entries = ht->size * 0.9;<br>
> > ht->key_hash_function = key_hash_function;<br>
> > ht->key_equals_function = key_equals_function;<br>
> > ht->table = rzalloc_array(ht, struct set_entry, ht->size);<br>
> > @@ -163,12 +131,11 @@ _mesa_set_destroy(struct set *ht, void (*delete_function)(struct set_entry *entr<br>
> > static struct set_entry *<br>
> > set_search(const struct set *ht, uint32_t hash, const void *key)<br>
> > {<br>
> > - uint32_t hash_address;<br>
> > + uint32_t start_hash_address = hash & (ht->size - 1);<br>
> > + uint32_t hash_address = start_hash_address;<br>
> > + uint32_t quad_hash = 1;<br>
> ><br>
> > - hash_address = hash % ht->size;<br>
> > do {<br>
> > - uint32_t double_hash;<br>
> > -<br>
> > struct set_entry *entry = ht->table + hash_address;<br>
> ><br>
> > if (entry_is_free(entry)) {<br>
> > @@ -179,10 +146,10 @@ set_search(const struct set *ht, uint32_t hash, const void *key)<br>
> > }<br>
> > }<br>
> ><br>
> > - double_hash = 1 + hash % ht->rehash;<br>
> > -<br>
> > - hash_address = (hash_address + double_hash) % ht->size;<br>
> > - } while (hash_address != hash % ht->size);<br>
> > + hash_address = (start_hash_address +<br>
> > + (quad_hash + (quad_hash * quad_hash)) / 2) & (ht->size - 1);<br>
> > + quad_hash++;<br>
> > + } while (hash_address != start_hash_address);<br>
> ><br>
> > return NULL;<br>
> > }<br>
> > @@ -207,35 +174,30 @@ static struct set_entry *<br>
> > set_add(struct set *ht, uint32_t hash, const void *key);<br>
> ><br>
> > static void<br>
> > -set_rehash(struct set *ht, unsigned new_size_index)<br>
> > +set_rehash(struct set *ht, uint32_t new_size_iteration)<br>
> > {<br>
> > struct set old_ht;<br>
> > struct set_entry *table, *entry;<br>
> ><br>
> > - if (new_size_index >= ARRAY_SIZE(hash_sizes))<br>
> > + if (new_size_iteration >= 31)<br>
> > return;<br>
> ><br>
> > table = rzalloc_array(ht, struct set_entry,<br>
> > - hash_sizes[new_size_index].size);<br>
> > + 1 << new_size_iteration);<br>
> > if (table == NULL)<br>
> > return;<br>
> ><br>
> > old_ht = *ht;<br>
> ><br>
> > ht->table = table;<br>
> > - ht->size_index = new_size_index;<br>
> > - ht->size = hash_sizes[ht->size_index].size;<br>
> > - ht->rehash = hash_sizes[ht->size_index].rehash;<br>
> > - ht->max_entries = hash_sizes[ht->size_index].max_entries;<br>
> > + ht->size_iteration = new_size_iteration;<br>
> > + ht->size = 1 << new_size_iteration;<br>
> > + ht->max_entries = ht->size * 0.7;<br>
> > ht->entries = 0;<br>
> > ht->deleted_entries = 0;<br>
> ><br>
> > - for (entry = old_ht.table;<br>
> > - entry != old_ht.table + old_ht.size;<br>
> > - entry++) {<br>
> > - if (entry_is_present(entry)) {<br>
> > - set_add(ht, entry->hash, entry->key);<br>
> > - }<br>
> > + set_foreach(&old_ht, entry) {<br>
> > + set_add(ht, entry->hash, entry->key);<br>
> > }<br>
><br>
> This hunk, while I think it's correct, is unrelated. Please drop it.<br>
> I think I'm ok with the cleanup, but it should be a separate patch if<br>
> you want to do that.<br>
> --Jason<br>
><br><br>Yup, unrelated change it is.<br>I'll split it in two and post both in one of the coming days.<br><br>(This is how it is done in the hash table in current master.<br>Following the same pattern in the set seemed desirable.)<br><br>- Thomas</p><p dir="ltr">
> > ralloc_free(old_ht.table);<br>
> > @@ -250,19 +212,21 @@ set_rehash(struct set *ht, unsigned new_size_index)<br>
> > static struct set_entry *<br>
> > set_add(struct set *ht, uint32_t hash, const void *key)<br>
> > {<br>
> > - uint32_t hash_address;<br>
> > + uint32_t start_hash_address, hash_address;<br>
> > + uint32_t quad_hash = 1;<br>
> > struct set_entry *available_entry = NULL;<br>
> ><br>
> > if (ht->entries >= ht->max_entries) {<br>
> > - set_rehash(ht, ht->size_index + 1);<br>
> > + set_rehash(ht, ht->size_iteration + 1);<br>
> > } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {<br>
> > - set_rehash(ht, ht->size_index);<br>
> > + set_rehash(ht, ht->size_iteration);<br>
> > }<br>
> ><br>
> > - hash_address = hash % ht->size;<br>
> > + start_hash_address = hash & (ht->size - 1);<br>
> > + hash_address = start_hash_address;<br>
> > +<br>
> > do {<br>
> > struct set_entry *entry = ht->table + hash_address;<br>
> > - uint32_t double_hash;<br>
> ><br>
> > if (!entry_is_present(entry)) {<br>
> > /* Stash the first available entry we find */<br>
> > @@ -288,10 +252,11 @@ set_add(struct set *ht, uint32_t hash, const void *key)<br>
> > return entry;<br>
> > }<br>
> ><br>
> > - double_hash = 1 + hash % ht->rehash;<br>
> > + hash_address = (start_hash_address +<br>
> > + (quad_hash + (quad_hash * quad_hash)) / 2) & (ht->size - 1);<br>
> > + quad_hash++;<br>
> > + } while (hash_address != start_hash_address);<br>
> ><br>
> > - hash_address = (hash_address + double_hash) % ht->size;<br>
> > - } while (hash_address != hash % ht->size);<br>
> ><br>
> > if (available_entry) {<br>
> > if (entry_is_deleted(available_entry))<br>
> > @@ -305,6 +270,7 @@ set_add(struct set *ht, uint32_t hash, const void *key)<br>
> > /* We could hit here if a required resize failed. An unchecked-malloc<br>
> > * application could ignore this result.<br>
> > */<br>
> > + unreachable("Failed to insert entry in hash set");<br>
> > return NULL;<br>
> > }<br>
> ><br>
> > @@ -368,7 +334,7 @@ _mesa_set_random_entry(struct set *ht,<br>
> > int (*predicate)(struct set_entry *entry))<br>
> > {<br>
> > struct set_entry *entry;<br>
> > - uint32_t i = rand() % ht->size;<br>
> > + uint32_t i = rand() & (ht->size - 1);<br>
> ><br>
> > if (ht->entries == 0)<br>
> > return NULL;<br>
> > diff --git a/src/util/set.h b/src/util/set.h<br>
> > index 9acd2c2..fa17d3a 100644<br>
> > --- a/src/util/set.h<br>
> > +++ b/src/util/set.h<br>
> > @@ -46,9 +46,8 @@ struct set {<br>
> > uint32_t (*key_hash_function)(const void *key);<br>
> > bool (*key_equals_function)(const void *a, const void *b);<br>
> > uint32_t size;<br>
> > - uint32_t rehash;<br>
> > uint32_t max_entries;<br>
> > - uint32_t size_index;<br>
> > + uint32_t size_iteration;<br>
> > uint32_t entries;<br>
> > uint32_t deleted_entries;<br>
> > };<br>
> > --<br>
> > 2.3.4<br>
> ><br>
> > _______________________________________________<br>
> > mesa-dev mailing list<br>
> > <a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
> > <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</p>
</div>