[PATCH] D19045: Add a CachedHash structure

Rafael Ávila de Espíndola via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 12 19:22:49 PDT 2016


rafael created this revision.
rafael added reviewers: ruiu, sanjoy, dblaikie.
rafael added a subscriber: llvm-commits.

A DenseMap doesn't store the hashes, so it needs to recompute them when the table is resized.

In some applications the hashing cost is noticeable. That is the case for example in lld for symbol names (StringRef).

This patch adds a templated structure that can wraps any value that can go in a DenseMap and caches the hash.


http://reviews.llvm.org/D19045

Files:
  include/llvm/ADT/DenseMapInfo.h

Index: include/llvm/ADT/DenseMapInfo.h
===================================================================
--- include/llvm/ADT/DenseMapInfo.h
+++ include/llvm/ADT/DenseMapInfo.h
@@ -30,6 +30,29 @@
   //static bool isEqual(const T &LHS, const T &RHS);
 };
 
+template <typename T> struct CachedHash {
+  CachedHash(T Val) : Val(Val) { Hash = DenseMapInfo<T>::getHashValue(Val); }
+  CachedHash(T Val, unsigned Hash) : Val(Val), Hash(Hash) {}
+  T Val;
+  unsigned Hash;
+};
+
+// Provide DenseMapInfo for all CachedHash<T>.
+template <typename T> struct DenseMapInfo<CachedHash<T>> {
+  static CachedHash<T> getEmptyKey() {
+    T N = DenseMapInfo<T>::getEmptyKey();
+    return {N, 0};
+  }
+  static CachedHash<T> getTombstoneKey() {
+    T N = DenseMapInfo<T>::getTombstoneKey();
+    return {N, 0};
+  }
+  static unsigned getHashValue(CachedHash<T> Val) { return Val.Hash; }
+  static bool isEqual(CachedHash<T> A, CachedHash<T> B) {
+    return DenseMapInfo<T>::isEqual(A.Val, B.Val);
+  }
+};
+
 // Provide DenseMapInfo for all pointers.
 template<typename T>
 struct DenseMapInfo<T*> {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19045.53511.patch
Type: text/x-patch
Size: 1094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160413/33dfa04b/attachment.bin>


More information about the llvm-commits mailing list