Skip to content

Commit 5bc1adf

Browse files
committed
Revert "lldb: Cache string hash during ConstString pool queries/insertions"
Underlying StringMap API for providing a hash has caused some problems (observed a crash in lld) - so reverting this until I can figure out/fix what's going on there. This reverts commit 52ba075. This reverts commit 2e19760.
1 parent 7537c3c commit 5bc1adf

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

lldb/source/Utility/ConstString.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class Pool {
7777
return 0;
7878
}
7979

80-
StringPoolValueType GetMangledCounterpart(const char *ccstr) {
80+
StringPoolValueType GetMangledCounterpart(const char *ccstr) const {
8181
if (ccstr != nullptr) {
82-
const PoolEntry &pool = selectPool(llvm::StringRef(ccstr));
83-
llvm::sys::SmartScopedReader<false> rlock(pool.m_mutex);
82+
const uint8_t h = hash(llvm::StringRef(ccstr));
83+
llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
8484
return GetStringMapEntryFromKeyData(ccstr).getValue();
8585
}
8686
return nullptr;
@@ -100,20 +100,19 @@ class Pool {
100100

101101
const char *GetConstCStringWithStringRef(llvm::StringRef string_ref) {
102102
if (string_ref.data()) {
103-
const uint32_t string_hash = StringPool::hash(string_ref);
104-
PoolEntry &pool = selectPool(string_hash);
103+
const uint8_t h = hash(string_ref);
105104

106105
{
107-
llvm::sys::SmartScopedReader<false> rlock(pool.m_mutex);
108-
auto it = pool.m_string_map.find(string_ref, string_hash);
109-
if (it != pool.m_string_map.end())
106+
llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
107+
auto it = m_string_pools[h].m_string_map.find(string_ref);
108+
if (it != m_string_pools[h].m_string_map.end())
110109
return it->getKeyData();
111110
}
112111

113-
llvm::sys::SmartScopedWriter<false> wlock(pool.m_mutex);
112+
llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
114113
StringPoolEntryType &entry =
115-
*pool.m_string_map
116-
.insert(std::make_pair(string_ref, nullptr), string_hash)
114+
*m_string_pools[h]
115+
.m_string_map.insert(std::make_pair(string_ref, nullptr))
117116
.first;
118117
return entry.getKeyData();
119118
}
@@ -126,14 +125,12 @@ class Pool {
126125
const char *demangled_ccstr = nullptr;
127126

128127
{
129-
const uint32_t demangled_hash = StringPool::hash(demangled);
130-
PoolEntry &pool = selectPool(demangled_hash);
131-
llvm::sys::SmartScopedWriter<false> wlock(pool.m_mutex);
128+
const uint8_t h = hash(demangled);
129+
llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
132130

133131
// Make or update string pool entry with the mangled counterpart
134-
StringPool &map = pool.m_string_map;
135-
StringPoolEntryType &entry =
136-
*map.try_emplace_with_hash(demangled, demangled_hash).first;
132+
StringPool &map = m_string_pools[h].m_string_map;
133+
StringPoolEntryType &entry = *map.try_emplace(demangled).first;
137134

138135
entry.second = mangled_ccstr;
139136

@@ -144,8 +141,8 @@ class Pool {
144141
{
145142
// Now assign the demangled const string as the counterpart of the
146143
// mangled const string...
147-
PoolEntry &pool = selectPool(llvm::StringRef(mangled_ccstr));
148-
llvm::sys::SmartScopedWriter<false> wlock(pool.m_mutex);
144+
const uint8_t h = hash(llvm::StringRef(mangled_ccstr));
145+
llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
149146
GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
150147
}
151148

@@ -174,20 +171,17 @@ class Pool {
174171
}
175172

176173
protected:
174+
uint8_t hash(llvm::StringRef s) const {
175+
uint32_t h = llvm::djbHash(s);
176+
return ((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff;
177+
}
178+
177179
struct PoolEntry {
178180
mutable llvm::sys::SmartRWMutex<false> m_mutex;
179181
StringPool m_string_map;
180182
};
181183

182184
std::array<PoolEntry, 256> m_string_pools;
183-
184-
PoolEntry &selectPool(const llvm::StringRef &s) {
185-
return selectPool(StringPool::hash(s));
186-
}
187-
188-
PoolEntry &selectPool(uint32_t h) {
189-
return m_string_pools[((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff];
190-
}
191185
};
192186

193187
// Frameworks and dylibs aren't supposed to have global C++ initializers so we
@@ -203,7 +197,7 @@ static Pool &StringPool() {
203197
static Pool *g_string_pool = nullptr;
204198

205199
llvm::call_once(g_pool_initialization_flag,
206-
[]() { g_string_pool = new Pool(); });
200+
[]() { g_string_pool = new Pool(); });
207201

208202
return *g_string_pool;
209203
}

llvm/lib/Support/StringMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const {
148148
if (NumBuckets == 0)
149149
return -1; // Really empty table?
150150
#ifdef EXPENSIVE_CHECKS
151-
assert(FullHashValue == hash(Key));
151+
assert(FullHashValue == hash(Key);
152152
#endif
153153
if (shouldReverseIterate())
154154
FullHashValue = ~FullHashValue;

0 commit comments

Comments
 (0)