lldb: Cache string hash during ConstString pool queries/insertions · llvm/llvm-project@2e19760 (original) (raw)
`@@ -77,10 +77,10 @@ class Pool {
`
77
77
`return 0;
`
78
78
` }
`
79
79
``
80
``
`-
StringPoolValueType GetMangledCounterpart(const char *ccstr) const {
`
``
80
`+
StringPoolValueType GetMangledCounterpart(const char *ccstr) {
`
81
81
`if (ccstr != nullptr) {
`
82
``
`-
const uint8_t h = hash(llvm::StringRef(ccstr));
`
83
``
`-
llvm::sys::SmartScopedReader rlock(m_string_pools[h].m_mutex);
`
``
82
`+
const PoolEntry &pool = selectPool(llvm::StringRef(ccstr));
`
``
83
`+
llvm::sys::SmartScopedReader rlock(pool.m_mutex);
`
84
84
`return GetStringMapEntryFromKeyData(ccstr).getValue();
`
85
85
` }
`
86
86
`return nullptr;
`
`@@ -100,19 +100,20 @@ class Pool {
`
100
100
``
101
101
`const char *GetConstCStringWithStringRef(llvm::StringRef string_ref) {
`
102
102
`if (string_ref.data()) {
`
103
``
`-
const uint8_t h = hash(string_ref);
`
``
103
`+
const uint32_t string_hash = StringPool::hash(string_ref);
`
``
104
`+
PoolEntry &pool = selectPool(string_hash);
`
104
105
``
105
106
` {
`
106
``
`-
llvm::sys::SmartScopedReader 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())
`
``
107
`+
llvm::sys::SmartScopedReader rlock(pool.m_mutex);
`
``
108
`+
auto it = pool.m_string_map.find(string_ref, string_hash);
`
``
109
`+
if (it != pool.m_string_map.end())
`
109
110
`return it->getKeyData();
`
110
111
` }
`
111
112
``
112
``
`-
llvm::sys::SmartScopedWriter wlock(m_string_pools[h].m_mutex);
`
``
113
`+
llvm::sys::SmartScopedWriter wlock(pool.m_mutex);
`
113
114
` StringPoolEntryType &entry =
`
114
``
`-
*m_string_pools[h]
`
115
``
`-
.m_string_map.insert(std::make_pair(string_ref, nullptr))
`
``
115
`+
*pool.m_string_map
`
``
116
`+
.insert(std::make_pair(string_ref, nullptr), string_hash)
`
116
117
` .first;
`
117
118
`return entry.getKeyData();
`
118
119
` }
`
`@@ -125,12 +126,14 @@ class Pool {
`
125
126
`const char *demangled_ccstr = nullptr;
`
126
127
``
127
128
` {
`
128
``
`-
const uint8_t h = hash(demangled);
`
129
``
`-
llvm::sys::SmartScopedWriter wlock(m_string_pools[h].m_mutex);
`
``
129
`+
const uint32_t demangled_hash = StringPool::hash(demangled);
`
``
130
`+
PoolEntry &pool = selectPool(demangled_hash);
`
``
131
`+
llvm::sys::SmartScopedWriter wlock(pool.m_mutex);
`
130
132
``
131
133
`// Make or update string pool entry with the mangled counterpart
`
132
``
`-
StringPool &map = m_string_pools[h].m_string_map;
`
133
``
`-
StringPoolEntryType &entry = *map.try_emplace(demangled).first;
`
``
134
`+
StringPool &map = pool.m_string_map;
`
``
135
`+
StringPoolEntryType &entry =
`
``
136
`+
*map.try_emplace_with_hash(demangled, demangled_hash).first;
`
134
137
``
135
138
` entry.second = mangled_ccstr;
`
136
139
``
`@@ -141,8 +144,8 @@ class Pool {
`
141
144
` {
`
142
145
`// Now assign the demangled const string as the counterpart of the
`
143
146
`// mangled const string...
`
144
``
`-
const uint8_t h = hash(llvm::StringRef(mangled_ccstr));
`
145
``
`-
llvm::sys::SmartScopedWriter wlock(m_string_pools[h].m_mutex);
`
``
147
`+
PoolEntry &pool = selectPool(llvm::StringRef(mangled_ccstr));
`
``
148
`+
llvm::sys::SmartScopedWriter wlock(pool.m_mutex);
`
146
149
`GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
`
147
150
` }
`
148
151
``
`@@ -171,17 +174,20 @@ class Pool {
`
171
174
` }
`
172
175
``
173
176
`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
``
-
179
177
`struct PoolEntry {
`
180
178
`mutable llvm::sys::SmartRWMutex m_mutex;
`
181
179
` StringPool m_string_map;
`
182
180
` };
`
183
181
``
184
182
` 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
`+
}
`
185
191
`};
`
186
192
``
187
193
`// Frameworks and dylibs aren't supposed to have global C++ initializers so we
`
`@@ -197,7 +203,7 @@ static Pool &StringPool() {
`
197
203
`static Pool *g_string_pool = nullptr;
`
198
204
``
199
205
`llvm::call_once(g_pool_initialization_flag,
`
200
``
`-
{ g_string_pool = new Pool(); });
`
``
206
`+
{ g_string_pool = new Pool(); });
`
201
207
``
202
208
`return *g_string_pool;
`
203
209
`}
`