[Mesa-dev] [PATCH] util/hash_table: Rework the API to know about hashing

Jason Ekstrand jason at jlekstrand.net
Tue Nov 25 15:49:07 PST 2014

On Tue, Nov 25, 2014 at 3:40 PM, Eric Anholt <eric at anholt.net> wrote:

> Jason Ekstrand <jason at jlekstrand.net> writes:
> > Previously, the hash_table API required the user to do all of the hashing
> > of keys as it passed them in.  Since the hashing function is intrinsicly
> > tied to the comparison function, it makes sense for the hash table to
> know
> > about it.  Also, it makes for a somewhat clumsy API as the user is
> > constantly calling hashing functions many of which have long names.  This
> > is especially bad when the standard call looks something like
> >
> > _mesa_hash_table_insert(ht, _mesa_pointer_hash(key), key, data);
> >
> > In the above case, there is no reason why the hash table shouldn't do the
> > hashing for you.  We leave the option for you to do your own hashing if
> > it's more efficient, but it's no longer needed.  Also, if you do do your
> > own hashing, the hash table will assert that your hash matches what it
> > expects out of the hashing function.  This should make it harder to mess
> up
> > your hashing.
> I went to go port this change to the upstream repo, and found a commit
> laying around from cworth that did the same thing.  He called the
> renamed functions "_pre_hashed", and included sets as well.  I think I
> prefer that name slightly?  What do you think?  I've pushed his change
> to the upstream (git+ssh://people.freedesktop.org/~anholt/hash_table).

That looks fine to me.  It looks mostly like what I did.  The one thing I
think my patch has over Carl's is that I split the insert_pre_hashed
function into an internal and an external version.  If you call the
external version, it asserts that the hash value from the internal hashing
function matches the hash value you gave it.  That way you'll notice if you
ever mix up hashing functions.  If you'd like, I can make that a patch on
top of what you have in your branch there.  Or you can do it and just push

> It's a really good idea, though, and I'm sorry I hadn't pushed the
> change and propagated it to Mesa already. :(

That's ok.  Mesa doesn't use it that many places.  We don't really feel the
pain until NIR which uses it about twice as much as the rest of mesa.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20141125/ce00fb5b/attachment.html>

More information about the mesa-dev mailing list