From 93c2d559a12bf72c7eb96b6a2a5a5bb77ecdc4b4 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 23 Aug 2022 19:09:34 +0000 Subject: [PATCH] tint/utils: Default to using Hasher instead of std::hash STL std::hash 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 Reviewed-by: Dan Sinclair Commit-Queue: Ben Clayton --- src/tint/utils/hashmap.h | 3 ++- src/tint/utils/hashset.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tint/utils/hashmap.h b/src/tint/utils/hashmap.h index 81bebf2410..e17938987f 100644 --- a/src/tint/utils/hashmap.h +++ b/src/tint/utils/hashmap.h @@ -19,6 +19,7 @@ #include #include +#include "src/tint/utils/hash.h" #include "src/tint/utils/hashset.h" namespace tint::utils { @@ -31,7 +32,7 @@ namespace tint::utils { template , + typename HASH = Hasher, typename EQUAL = std::equal_to> class Hashmap { /// LazyCreator is a transient structure used to late-build the Entry::value, when inserted into diff --git a/src/tint/utils/hashset.h b/src/tint/utils/hashset.h index f88a304bf9..3009c72eba 100644 --- a/src/tint/utils/hashset.h +++ b/src/tint/utils/hashset.h @@ -23,6 +23,7 @@ #include #include "src/tint/debug.h" +#include "src/tint/utils/hash.h" #include "src/tint/utils/vector.h" namespace tint::utils { @@ -39,7 +40,7 @@ enum class AddAction { /// An unordered set that uses a robin-hood hashing algorithm. /// @see the fantastic tutorial: https://programming.guide/robin-hood-hashing.html -template , typename EQUAL = std::equal_to> +template , typename EQUAL = std::equal_to> class Hashset { /// 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