From 04f1186ea8f0160da1a1863a344ff9a9f55b6733 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Sat, 7 May 2022 22:24:34 -0400 Subject: [PATCH 06/31] Remove DJB hash dependency --- llvm/lib/Support/StringMap.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp index d9eeba619428..f0d5ae6de646 100644 --- a/llvm/lib/Support/StringMap.cpp +++ b/llvm/lib/Support/StringMap.cpp @@ -12,11 +12,26 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringExtras.h" -#include "llvm/Support/DJB.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/MathExtras.h" using namespace llvm; +/// HashString - Hash function for strings. +/// +/// This is the Bernstein hash function. +// +// FIXME: Investigate whether a modified bernstein hash function performs +// better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx +// X*33+c -> X*33^c +static inline unsigned HashString(std::string_view str, + unsigned result = 0) noexcept { + for (std::string_view::size_type i = 0, e = str.size(); i != e; ++i) { + result = result * 33 + static_cast(str[i]); + } + return result; +} + /// Returns the number of buckets to allocate to ensure that the DenseMap can /// accommodate \p NumEntries without need to grow(). static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) { @@ -77,7 +92,7 @@ unsigned StringMapImpl::LookupBucketFor(std::string_view Name) { init(16); HTSize = NumBuckets; } - unsigned FullHashValue = djbHash(Name, 0); + unsigned FullHashValue = HashString(Name); unsigned BucketNo = FullHashValue & (HTSize - 1); unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); @@ -133,7 +148,7 @@ int StringMapImpl::FindKey(std::string_view Key) const { unsigned HTSize = NumBuckets; if (HTSize == 0) return -1; // Really empty table? - unsigned FullHashValue = djbHash(Key, 0); + unsigned FullHashValue = HashString(Key); unsigned BucketNo = FullHashValue & (HTSize - 1); unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); -- 2.20.1.windows.1