tint/utils: Default to using Hasher instead of std::hash

STL std::hash<T*> implementations have a tendency of just reinterpreting the
pointer as a size_t. This is fast, but produces a bad hash, as most pointers
tend to be 4 or 16 byte aligned. The lack of entropy in the LSBs causes
clustering of hashmap slots. Use tint::utils::Hasher to get better hashes.

Change-Id: Ife768d573cd1875e746ca9d77a4ac19e43b06aca
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99281
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-08-23 19:09:34 +00:00 committed by Dawn LUCI CQ
parent 18dc315ccb
commit 93c2d559a1
2 changed files with 4 additions and 2 deletions

View File

@ -19,6 +19,7 @@
#include <optional> #include <optional>
#include <utility> #include <utility>
#include "src/tint/utils/hash.h"
#include "src/tint/utils/hashset.h" #include "src/tint/utils/hashset.h"
namespace tint::utils { namespace tint::utils {
@ -31,7 +32,7 @@ namespace tint::utils {
template <typename K, template <typename K,
typename V, typename V,
size_t N, size_t N,
typename HASH = std::hash<K>, typename HASH = Hasher<K>,
typename EQUAL = std::equal_to<K>> typename EQUAL = std::equal_to<K>>
class Hashmap { class Hashmap {
/// LazyCreator is a transient structure used to late-build the Entry::value, when inserted into /// LazyCreator is a transient structure used to late-build the Entry::value, when inserted into

View File

@ -23,6 +23,7 @@
#include <utility> #include <utility>
#include "src/tint/debug.h" #include "src/tint/debug.h"
#include "src/tint/utils/hash.h"
#include "src/tint/utils/vector.h" #include "src/tint/utils/vector.h"
namespace tint::utils { namespace tint::utils {
@ -39,7 +40,7 @@ enum class AddAction {
/// An unordered set that uses a robin-hood hashing algorithm. /// An unordered set that uses a robin-hood hashing algorithm.
/// @see the fantastic tutorial: https://programming.guide/robin-hood-hashing.html /// @see the fantastic tutorial: https://programming.guide/robin-hood-hashing.html
template <typename T, size_t N, typename HASH = std::hash<T>, typename EQUAL = std::equal_to<T>> template <typename T, size_t N, typename HASH = Hasher<T>, typename EQUAL = std::equal_to<T>>
class Hashset { class Hashset {
/// A slot is a single entry in the underlying vector. /// A slot is a single entry in the underlying vector.
/// A slot can either be empty or filled with a value. If the slot is empty, #hash and #distance /// A slot can either be empty or filled with a value. If the slot is empty, #hash and #distance