@@ -77,10 +77,10 @@ class Pool {
77
77
return 0 ;
78
78
}
79
79
80
- StringPoolValueType GetMangledCounterpart (const char *ccstr) {
80
+ StringPoolValueType GetMangledCounterpart (const char *ccstr) const {
81
81
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 );
84
84
return GetStringMapEntryFromKeyData (ccstr).getValue ();
85
85
}
86
86
return nullptr ;
@@ -100,20 +100,19 @@ class Pool {
100
100
101
101
const char *GetConstCStringWithStringRef (llvm::StringRef string_ref) {
102
102
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);
105
104
106
105
{
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 ())
110
109
return it->getKeyData ();
111
110
}
112
111
113
- llvm::sys::SmartScopedWriter<false > wlock (pool .m_mutex );
112
+ llvm::sys::SmartScopedWriter<false > wlock (m_string_pools[h] .m_mutex );
114
113
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 ))
117
116
.first ;
118
117
return entry.getKeyData ();
119
118
}
@@ -126,14 +125,12 @@ class Pool {
126
125
const char *demangled_ccstr = nullptr ;
127
126
128
127
{
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 );
132
130
133
131
// 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 ;
137
134
138
135
entry.second = mangled_ccstr;
139
136
@@ -144,8 +141,8 @@ class Pool {
144
141
{
145
142
// Now assign the demangled const string as the counterpart of the
146
143
// 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 );
149
146
GetStringMapEntryFromKeyData (mangled_ccstr).setValue (demangled_ccstr);
150
147
}
151
148
@@ -174,20 +171,17 @@ class Pool {
174
171
}
175
172
176
173
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
+
177
179
struct PoolEntry {
178
180
mutable llvm::sys::SmartRWMutex<false > m_mutex;
179
181
StringPool m_string_map;
180
182
};
181
183
182
184
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
- }
191
185
};
192
186
193
187
// Frameworks and dylibs aren't supposed to have global C++ initializers so we
@@ -203,7 +197,7 @@ static Pool &StringPool() {
203
197
static Pool *g_string_pool = nullptr ;
204
198
205
199
llvm::call_once (g_pool_initialization_flag,
206
- []() { g_string_pool = new Pool (); });
200
+ []() { g_string_pool = new Pool (); });
207
201
208
202
return *g_string_pool;
209
203
}
0 commit comments