Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: src/base/hashmap.h

Issue 2345233003: [base] Move hashmap allocator to a field (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast/scopes.cc ('k') | src/compiler/state-values-utils.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // The reason we write our own hash map instead of using unordered_map in STL, 5 // The reason we write our own hash map instead of using unordered_map in STL,
6 // is that STL containers use a mutex pool on debug build, which will lead to 6 // is that STL containers use a mutex pool on debug build, which will lead to
7 // deadlock when we are using async signal handler. 7 // deadlock when we are using async signal handler.
8 8
9 #ifndef V8_BASE_HASHMAP_H_ 9 #ifndef V8_BASE_HASHMAP_H_
10 #define V8_BASE_HASHMAP_H_ 10 #define V8_BASE_HASHMAP_H_
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 48
49 ~TemplateHashMapImpl(); 49 ~TemplateHashMapImpl();
50 50
51 // If an entry with matching key is found, returns that entry. 51 // If an entry with matching key is found, returns that entry.
52 // Otherwise, nullptr is returned. 52 // Otherwise, nullptr is returned.
53 Entry* Lookup(const Key& key, uint32_t hash) const; 53 Entry* Lookup(const Key& key, uint32_t hash) const;
54 54
55 // If an entry with matching key is found, returns that entry. 55 // If an entry with matching key is found, returns that entry.
56 // If no matching entry is found, a new entry is inserted with 56 // If no matching entry is found, a new entry is inserted with
57 // corresponding key, key hash, and default initialized value. 57 // corresponding key, key hash, and default initialized value.
58 Entry* LookupOrInsert(const Key& key, uint32_t hash, 58 Entry* LookupOrInsert(const Key& key, uint32_t hash);
59 AllocationPolicy allocator = AllocationPolicy());
60 59
61 Entry* InsertNew(const Key& key, uint32_t hash, 60 Entry* InsertNew(const Key& key, uint32_t hash);
62 AllocationPolicy allocator = AllocationPolicy());
63 61
64 // Removes the entry with matching key. 62 // Removes the entry with matching key.
65 // It returns the value of the deleted entry 63 // It returns the value of the deleted entry
66 // or null if there is no value for such key. 64 // or null if there is no value for such key.
67 Value Remove(const Key& key, uint32_t hash); 65 Value Remove(const Key& key, uint32_t hash);
68 66
69 // Empties the hash map (occupancy() == 0). 67 // Empties the hash map (occupancy() == 0).
70 void Clear(); 68 void Clear();
71 69
72 // The number of (non-empty) entries in the table. 70 // The number of (non-empty) entries in the table.
(...skipping 15 matching lines...) Expand all
88 Entry* Start() const; 86 Entry* Start() const;
89 Entry* Next(Entry* p) const; 87 Entry* Next(Entry* p) const;
90 88
91 // Some match functions defined for convenience. 89 // Some match functions defined for convenience.
92 static bool PointersMatch(typename MatchFunHelper<Key>::KeyRef key1, 90 static bool PointersMatch(typename MatchFunHelper<Key>::KeyRef key1,
93 typename MatchFunHelper<Key>::KeyRef key2) { 91 typename MatchFunHelper<Key>::KeyRef key2) {
94 return key1 == key2; 92 return key1 == key2;
95 } 93 }
96 94
97 private: 95 private:
96 // Helper which holds the entry array as well as the allocation policy.
97 // AllocationPolicy is a base class rather than a field to take advantage of
98 // the empty base optimisation for state-free allocators, such as the
99 // DefaultAllocationPolicy above.
rmcilroy 2016/09/19 10:55:11 Let's make this a field instead of using the empty
Leszek Swirski 2016/09/19 12:20:56 Fair enough, done.
100 struct AllocatedEntryHolder : public AllocationPolicy {
101 Entry* values;
102 explicit AllocatedEntryHolder(AllocationPolicy allocator)
103 : AllocationPolicy(allocator) {}
104 };
105
98 MatchFun match_; 106 MatchFun match_;
99 Entry* map_; 107 AllocatedEntryHolder map_;
100 uint32_t capacity_; 108 uint32_t capacity_;
101 uint32_t occupancy_; 109 uint32_t occupancy_;
102 110
103 Entry* map_end() const { return map_ + capacity_; } 111 Entry* map_end() const { return map_.values + capacity_; }
104 Entry* Probe(const Key& key, uint32_t hash) const; 112 Entry* Probe(const Key& key, uint32_t hash) const;
105 void Initialize(uint32_t capacity, AllocationPolicy allocator); 113 void Initialize(uint32_t capacity);
106 void Resize(AllocationPolicy allocator); 114 void Resize();
107 }; 115 };
108 116
109 template <typename Key> 117 template <typename Key>
110 struct MatchFunHelper { 118 struct MatchFunHelper {
111 typedef const Key& KeyRef; 119 typedef const Key& KeyRef;
112 typedef bool (*Fun)(KeyRef key1, KeyRef key2); 120 typedef bool (*Fun)(KeyRef key1, KeyRef key2);
113 }; 121 };
114 122
115 template <typename Key> 123 template <typename Key>
116 struct MatchFunHelper<Key*> { 124 struct MatchFunHelper<Key*> {
117 typedef Key* KeyRef; 125 typedef Key* KeyRef;
118 typedef bool (*Fun)(KeyRef key1, KeyRef key2); 126 typedef bool (*Fun)(KeyRef key1, KeyRef key2);
119 }; 127 };
120 128
121 typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap; 129 typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap;
122 130
123 template <typename Key, typename Value, class AllocationPolicy> 131 template <typename Key, typename Value, class AllocationPolicy>
124 TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl( 132 TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl(
125 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { 133 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator)
134 : map_(allocator) {
126 match_ = match; 135 match_ = match;
127 Initialize(initial_capacity, allocator); 136 Initialize(initial_capacity);
128 } 137 }
129 138
130 template <typename Key, typename Value, class AllocationPolicy> 139 template <typename Key, typename Value, class AllocationPolicy>
131 TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() { 140 TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() {
132 AllocationPolicy::Delete(map_); 141 map_.AllocationPolicy::Delete(map_.values);
133 } 142 }
134 143
135 template <typename Key, typename Value, class AllocationPolicy> 144 template <typename Key, typename Value, class AllocationPolicy>
136 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 145 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
137 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key, 146 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key,
138 uint32_t hash) const { 147 uint32_t hash) const {
139 Entry* p = Probe(key, hash); 148 Entry* p = Probe(key, hash);
140 return p->exists() ? p : nullptr; 149 return p->exists() ? p : nullptr;
141 } 150 }
142 151
143 template <typename Key, typename Value, class AllocationPolicy> 152 template <typename Key, typename Value, class AllocationPolicy>
144 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 153 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
145 TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert( 154 TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert(
146 const Key& key, uint32_t hash, AllocationPolicy allocator) { 155 const Key& key, uint32_t hash) {
147 // Find a matching entry. 156 // Find a matching entry.
148 Entry* p = Probe(key, hash); 157 Entry* p = Probe(key, hash);
149 if (p->exists()) { 158 if (p->exists()) {
150 return p; 159 return p;
151 } 160 }
152 161
153 return InsertNew(key, hash, allocator); 162 return InsertNew(key, hash);
154 } 163 }
155 164
156 template <typename Key, typename Value, class AllocationPolicy> 165 template <typename Key, typename Value, class AllocationPolicy>
157 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 166 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
158 TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew( 167 TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew(const Key& key,
159 const Key& key, uint32_t hash, AllocationPolicy allocator) { 168 uint32_t hash) {
160 // Find a matching entry. 169 // Find a matching entry.
161 Entry* p = Probe(key, hash); 170 Entry* p = Probe(key, hash);
162 DCHECK(!p->exists()); 171 DCHECK(!p->exists());
163 172
164 // No entry found; insert one. 173 // No entry found; insert one.
165 new (p) Entry(key, Value(), hash); 174 new (p) Entry(key, Value(), hash);
166 occupancy_++; 175 occupancy_++;
167 176
168 // Grow the map if we reached >= 80% occupancy. 177 // Grow the map if we reached >= 80% occupancy.
169 if (occupancy_ + occupancy_ / 4 >= capacity_) { 178 if (occupancy_ + occupancy_ / 4 >= capacity_) {
170 Resize(allocator); 179 Resize();
171 p = Probe(key, hash); 180 p = Probe(key, hash);
172 } 181 }
173 182
174 return p; 183 return p;
175 } 184 }
176 185
177 template <typename Key, typename Value, class AllocationPolicy> 186 template <typename Key, typename Value, class AllocationPolicy>
178 Value TemplateHashMapImpl<Key, Value, AllocationPolicy>::Remove(const Key& key, 187 Value TemplateHashMapImpl<Key, Value, AllocationPolicy>::Remove(const Key& key,
179 uint32_t hash) { 188 uint32_t hash) {
180 // Lookup the entry for the key to remove. 189 // Lookup the entry for the key to remove.
(...skipping 19 matching lines...) Expand all
200 // This guarantees loop termination as there is at least one empty entry so 209 // This guarantees loop termination as there is at least one empty entry so
201 // eventually the removed entry will have an empty entry after it. 210 // eventually the removed entry will have an empty entry after it.
202 DCHECK(occupancy_ < capacity_); 211 DCHECK(occupancy_ < capacity_);
203 212
204 // p is the candidate entry to clear. q is used to scan forwards. 213 // p is the candidate entry to clear. q is used to scan forwards.
205 Entry* q = p; // Start at the entry to remove. 214 Entry* q = p; // Start at the entry to remove.
206 while (true) { 215 while (true) {
207 // Move q to the next entry. 216 // Move q to the next entry.
208 q = q + 1; 217 q = q + 1;
209 if (q == map_end()) { 218 if (q == map_end()) {
210 q = map_; 219 q = map_.values;
211 } 220 }
212 221
213 // All entries between p and q have their initial position between p and q 222 // All entries between p and q have their initial position between p and q
214 // and the entry p can be cleared without breaking the search for these 223 // and the entry p can be cleared without breaking the search for these
215 // entries. 224 // entries.
216 if (!q->exists()) { 225 if (!q->exists()) {
217 break; 226 break;
218 } 227 }
219 228
220 // Find the initial position for the entry at position q. 229 // Find the initial position for the entry at position q.
221 Entry* r = map_ + (q->hash & (capacity_ - 1)); 230 Entry* r = map_.values + (q->hash & (capacity_ - 1));
222 231
223 // If the entry at position q has its initial position outside the range 232 // If the entry at position q has its initial position outside the range
224 // between p and q it can be moved forward to position p and will still be 233 // between p and q it can be moved forward to position p and will still be
225 // found. There is now a new candidate entry for clearing. 234 // found. There is now a new candidate entry for clearing.
226 if ((q > p && (r <= p || r > q)) || (q < p && (r <= p && r > q))) { 235 if ((q > p && (r <= p || r > q)) || (q < p && (r <= p && r > q))) {
227 *p = *q; 236 *p = *q;
228 p = q; 237 p = q;
229 } 238 }
230 } 239 }
231 240
232 // Clear the entry which is allowed to en emptied. 241 // Clear the entry which is allowed to en emptied.
233 p->clear(); 242 p->clear();
234 occupancy_--; 243 occupancy_--;
235 return value; 244 return value;
236 } 245 }
237 246
238 template <typename Key, typename Value, class AllocationPolicy> 247 template <typename Key, typename Value, class AllocationPolicy>
239 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Clear() { 248 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Clear() {
240 // Mark all entries as empty. 249 // Mark all entries as empty.
241 const Entry* end = map_end(); 250 const Entry* end = map_end();
242 for (Entry* p = map_; p < end; p++) { 251 for (Entry* p = map_.values; p < end; p++) {
243 p->clear(); 252 p->clear();
244 } 253 }
245 occupancy_ = 0; 254 occupancy_ = 0;
246 } 255 }
247 256
248 template <typename Key, typename Value, class AllocationPolicy> 257 template <typename Key, typename Value, class AllocationPolicy>
249 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 258 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
250 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Start() const { 259 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Start() const {
251 return Next(map_ - 1); 260 return Next(map_.values - 1);
252 } 261 }
253 262
254 template <typename Key, typename Value, class AllocationPolicy> 263 template <typename Key, typename Value, class AllocationPolicy>
255 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 264 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
256 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Next(Entry* p) const { 265 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Next(Entry* p) const {
257 const Entry* end = map_end(); 266 const Entry* end = map_end();
258 DCHECK(map_ - 1 <= p && p < end); 267 DCHECK(map_.values - 1 <= p && p < end);
259 for (p++; p < end; p++) { 268 for (p++; p < end; p++) {
260 if (p->exists()) { 269 if (p->exists()) {
261 return p; 270 return p;
262 } 271 }
263 } 272 }
264 return nullptr; 273 return nullptr;
265 } 274 }
266 275
267 template <typename Key, typename Value, class AllocationPolicy> 276 template <typename Key, typename Value, class AllocationPolicy>
268 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 277 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
269 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Probe(const Key& key, 278 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Probe(const Key& key,
270 uint32_t hash) const { 279 uint32_t hash) const {
271 DCHECK(base::bits::IsPowerOfTwo32(capacity_)); 280 DCHECK(base::bits::IsPowerOfTwo32(capacity_));
272 Entry* p = map_ + (hash & (capacity_ - 1)); 281 Entry* p = map_.values + (hash & (capacity_ - 1));
273 const Entry* end = map_end(); 282 const Entry* end = map_end();
274 DCHECK(map_ <= p && p < end); 283 DCHECK(map_.values <= p && p < end);
275 284
276 DCHECK(occupancy_ < capacity_); // Guarantees loop termination. 285 DCHECK(occupancy_ < capacity_); // Guarantees loop termination.
277 while (p->exists() && (hash != p->hash || !match_(key, p->key))) { 286 while (p->exists() && (hash != p->hash || !match_(key, p->key))) {
278 p++; 287 p++;
279 if (p >= end) { 288 if (p >= end) {
280 p = map_; 289 p = map_.values;
281 } 290 }
282 } 291 }
283 292
284 return p; 293 return p;
285 } 294 }
286 295
287 template <typename Key, typename Value, class AllocationPolicy> 296 template <typename Key, typename Value, class AllocationPolicy>
288 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize( 297 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize(
289 uint32_t capacity, AllocationPolicy allocator) { 298 uint32_t capacity) {
290 DCHECK(base::bits::IsPowerOfTwo32(capacity)); 299 DCHECK(base::bits::IsPowerOfTwo32(capacity));
291 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); 300 map_.values = reinterpret_cast<Entry*>(
292 if (map_ == nullptr) { 301 map_.AllocationPolicy::New(capacity * sizeof(Entry)));
302 if (map_.values == nullptr) {
293 FATAL("Out of memory: HashMap::Initialize"); 303 FATAL("Out of memory: HashMap::Initialize");
294 return; 304 return;
295 } 305 }
296 capacity_ = capacity; 306 capacity_ = capacity;
297 Clear(); 307 Clear();
298 } 308 }
299 309
300 template <typename Key, typename Value, class AllocationPolicy> 310 template <typename Key, typename Value, class AllocationPolicy>
301 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize( 311 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize() {
302 AllocationPolicy allocator) { 312 Entry* map = map_.values;
303 Entry* map = map_;
304 uint32_t n = occupancy_; 313 uint32_t n = occupancy_;
305 314
306 // Allocate larger map. 315 // Allocate larger map.
307 Initialize(capacity_ * 2, allocator); 316 Initialize(capacity_ * 2);
308 317
309 // Rehash all current entries. 318 // Rehash all current entries.
310 for (Entry* p = map; n > 0; p++) { 319 for (Entry* p = map; n > 0; p++) {
311 if (p->exists()) { 320 if (p->exists()) {
312 Entry* entry = LookupOrInsert(p->key, p->hash, allocator); 321 Entry* entry = LookupOrInsert(p->key, p->hash);
313 entry->value = p->value; 322 entry->value = p->value;
314 n--; 323 n--;
315 } 324 }
316 } 325 }
317 326
318 // Delete old map. 327 // Delete old map.
319 AllocationPolicy::Delete(map); 328 AllocationPolicy::Delete(map);
320 } 329 }
321 330
322 // A hash map for pointer keys and values with an STL-like interface. 331 // A hash map for pointer keys and values with an STL-like interface.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 void*, void*, AllocationPolicy>::MatchFun match, 366 void*, void*, AllocationPolicy>::MatchFun match,
358 AllocationPolicy allocator = AllocationPolicy()) 367 AllocationPolicy allocator = AllocationPolicy())
359 : TemplateHashMapImpl<void*, void*, AllocationPolicy>( 368 : TemplateHashMapImpl<void*, void*, AllocationPolicy>(
360 match, 369 match,
361 TemplateHashMapImpl<void*, void*, 370 TemplateHashMapImpl<void*, void*,
362 AllocationPolicy>::kDefaultHashMapCapacity, 371 AllocationPolicy>::kDefaultHashMapCapacity,
363 allocator) {} 372 allocator) {}
364 373
365 Iterator begin() const { return Iterator(this, this->Start()); } 374 Iterator begin() const { return Iterator(this, this->Start()); }
366 Iterator end() const { return Iterator(this, nullptr); } 375 Iterator end() const { return Iterator(this, nullptr); }
367 Iterator find(Key* key, bool insert = false, 376 Iterator find(Key* key, bool insert = false) {
368 AllocationPolicy allocator = AllocationPolicy()) {
369 if (insert) { 377 if (insert) {
370 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); 378 return Iterator(this, this->LookupOrInsert(key, key->Hash()));
371 } 379 }
372 return Iterator(this, this->Lookup(key, key->Hash())); 380 return Iterator(this, this->Lookup(key, key->Hash()));
373 } 381 }
374 }; 382 };
375 383
376 } // namespace base 384 } // namespace base
377 } // namespace v8 385 } // namespace v8
378 386
379 #endif // V8_BASE_HASHMAP_H_ 387 #endif // V8_BASE_HASHMAP_H_
OLDNEW
« no previous file with comments | « src/ast/scopes.cc ('k') | src/compiler/state-values-utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698