diff --git a/lldb/source/Utility/ConstString.cpp b/lldb/source/Utility/ConstString.cpp index ea897dc611cc9..4535771adfb73 100644 --- a/lldb/source/Utility/ConstString.cpp +++ b/lldb/source/Utility/ConstString.cpp @@ -77,10 +77,10 @@ class Pool { return 0; } - StringPoolValueType GetMangledCounterpart(const char *ccstr) { + StringPoolValueType GetMangledCounterpart(const char *ccstr) const { if (ccstr != nullptr) { - const PoolEntry &pool = selectPool(llvm::StringRef(ccstr)); - llvm::sys::SmartScopedReader rlock(pool.m_mutex); + const uint8_t h = hash(llvm::StringRef(ccstr)); + llvm::sys::SmartScopedReader rlock(m_string_pools[h].m_mutex); return GetStringMapEntryFromKeyData(ccstr).getValue(); } return nullptr; @@ -100,20 +100,19 @@ class Pool { const char *GetConstCStringWithStringRef(llvm::StringRef string_ref) { if (string_ref.data()) { - const uint32_t string_hash = StringPool::hash(string_ref); - PoolEntry &pool = selectPool(string_hash); + const uint8_t h = hash(string_ref); { - llvm::sys::SmartScopedReader rlock(pool.m_mutex); - auto it = pool.m_string_map.find(string_ref, string_hash); - if (it != pool.m_string_map.end()) + llvm::sys::SmartScopedReader rlock(m_string_pools[h].m_mutex); + auto it = m_string_pools[h].m_string_map.find(string_ref); + if (it != m_string_pools[h].m_string_map.end()) return it->getKeyData(); } - llvm::sys::SmartScopedWriter wlock(pool.m_mutex); + llvm::sys::SmartScopedWriter wlock(m_string_pools[h].m_mutex); StringPoolEntryType &entry = - *pool.m_string_map - .insert(std::make_pair(string_ref, nullptr), string_hash) + *m_string_pools[h] + .m_string_map.insert(std::make_pair(string_ref, nullptr)) .first; return entry.getKeyData(); } @@ -126,14 +125,12 @@ class Pool { const char *demangled_ccstr = nullptr; { - const uint32_t demangled_hash = StringPool::hash(demangled); - PoolEntry &pool = selectPool(demangled_hash); - llvm::sys::SmartScopedWriter wlock(pool.m_mutex); + const uint8_t h = hash(demangled); + llvm::sys::SmartScopedWriter wlock(m_string_pools[h].m_mutex); // Make or update string pool entry with the mangled counterpart - StringPool &map = pool.m_string_map; - StringPoolEntryType &entry = - *map.try_emplace_with_hash(demangled, demangled_hash).first; + StringPool &map = m_string_pools[h].m_string_map; + StringPoolEntryType &entry = *map.try_emplace(demangled).first; entry.second = mangled_ccstr; @@ -144,8 +141,8 @@ class Pool { { // Now assign the demangled const string as the counterpart of the // mangled const string... - PoolEntry &pool = selectPool(llvm::StringRef(mangled_ccstr)); - llvm::sys::SmartScopedWriter wlock(pool.m_mutex); + const uint8_t h = hash(llvm::StringRef(mangled_ccstr)); + llvm::sys::SmartScopedWriter wlock(m_string_pools[h].m_mutex); GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr); } @@ -174,20 +171,17 @@ class Pool { } protected: + uint8_t hash(llvm::StringRef s) const { + uint32_t h = llvm::djbHash(s); + return ((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff; + } + struct PoolEntry { mutable llvm::sys::SmartRWMutex m_mutex; StringPool m_string_map; }; std::array m_string_pools; - - PoolEntry &selectPool(const llvm::StringRef &s) { - return selectPool(StringPool::hash(s)); - } - - PoolEntry &selectPool(uint32_t h) { - return m_string_pools[((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff]; - } }; // Frameworks and dylibs aren't supposed to have global C++ initializers so we @@ -203,7 +197,7 @@ static Pool &StringPool() { static Pool *g_string_pool = nullptr; llvm::call_once(g_pool_initialization_flag, - []() { g_string_pool = new Pool(); }); + []() { g_string_pool = new Pool(); }); return *g_string_pool; } diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp index 432e1fc343f1f..451108d01d38a 100644 --- a/llvm/lib/Support/StringMap.cpp +++ b/llvm/lib/Support/StringMap.cpp @@ -148,7 +148,7 @@ int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const { if (NumBuckets == 0) return -1; // Really empty table? #ifdef EXPENSIVE_CHECKS - assert(FullHashValue == hash(Key)); + assert(FullHashValue == hash(Key); #endif if (shouldReverseIterate()) FullHashValue = ~FullHashValue;