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

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

Issue 2345233003: [base] Move hashmap allocator to a field (Closed)
Patch Set: Remove zone arguments where the parameters were removed so that the bloody thing compiles again 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 22 matching lines...) Expand all
95 static bool PointersMatch(typename MatchFunHelper<Key>::KeyRef key1, 93 static bool PointersMatch(typename MatchFunHelper<Key>::KeyRef key1,
96 typename MatchFunHelper<Key>::KeyRef key2) { 94 typename MatchFunHelper<Key>::KeyRef key2) {
97 return key1 == key2; 95 return key1 == key2;
98 } 96 }
99 97
100 private: 98 private:
101 MatchFun match_; 99 MatchFun match_;
102 Entry* map_; 100 Entry* map_;
103 uint32_t capacity_; 101 uint32_t capacity_;
104 uint32_t occupancy_; 102 uint32_t occupancy_;
103 AllocationPolicy allocator_;
105 104
106 Entry* map_end() const { return map_ + capacity_; } 105 Entry* map_end() const { return map_ + capacity_; }
107 Entry* Probe(const Key& key, uint32_t hash) const; 106 Entry* Probe(const Key& key, uint32_t hash) const;
108 void Initialize(uint32_t capacity, AllocationPolicy allocator); 107 void Initialize(uint32_t capacity);
109 void Resize(AllocationPolicy allocator); 108 void Resize();
110 }; 109 };
111 110
112 template <typename Key> 111 template <typename Key>
113 struct MatchFunHelper { 112 struct MatchFunHelper {
114 typedef const Key& KeyRef; 113 typedef const Key& KeyRef;
115 typedef bool (*Fun)(KeyRef key1, KeyRef key2); 114 typedef bool (*Fun)(KeyRef key1, KeyRef key2);
116 }; 115 };
117 116
118 template <typename Key> 117 template <typename Key>
119 struct MatchFunHelper<Key*> { 118 struct MatchFunHelper<Key*> {
120 typedef Key* KeyRef; 119 typedef Key* KeyRef;
121 typedef bool (*Fun)(KeyRef key1, KeyRef key2); 120 typedef bool (*Fun)(KeyRef key1, KeyRef key2);
122 }; 121 };
123 122
124 typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap; 123 typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap;
125 124
126 template <typename Key, typename Value, class AllocationPolicy> 125 template <typename Key, typename Value, class AllocationPolicy>
127 TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl( 126 TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl(
128 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { 127 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator)
128 : allocator_(allocator) {
129 match_ = match; 129 match_ = match;
130 Initialize(initial_capacity, allocator); 130 Initialize(initial_capacity);
131 } 131 }
132 132
133 template <typename Key, typename Value, class AllocationPolicy> 133 template <typename Key, typename Value, class AllocationPolicy>
134 TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() { 134 TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() {
135 AllocationPolicy::Delete(map_); 135 allocator_.Delete(map_);
136 } 136 }
137 137
138 template <typename Key, typename Value, class AllocationPolicy> 138 template <typename Key, typename Value, class AllocationPolicy>
139 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 139 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
140 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key, 140 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key,
141 uint32_t hash) const { 141 uint32_t hash) const {
142 Entry* p = Probe(key, hash); 142 Entry* p = Probe(key, hash);
143 return p->exists() ? p : nullptr; 143 return p->exists() ? p : nullptr;
144 } 144 }
145 145
146 template <typename Key, typename Value, class AllocationPolicy> 146 template <typename Key, typename Value, class AllocationPolicy>
147 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 147 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
148 TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert( 148 TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert(
149 const Key& key, uint32_t hash, AllocationPolicy allocator) { 149 const Key& key, uint32_t hash) {
150 // Find a matching entry. 150 // Find a matching entry.
151 Entry* p = Probe(key, hash); 151 Entry* p = Probe(key, hash);
152 if (p->exists()) { 152 if (p->exists()) {
153 return p; 153 return p;
154 } 154 }
155 155
156 return InsertNew(key, hash, allocator); 156 return InsertNew(key, hash);
157 } 157 }
158 158
159 template <typename Key, typename Value, class AllocationPolicy> 159 template <typename Key, typename Value, class AllocationPolicy>
160 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* 160 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry*
161 TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew( 161 TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew(const Key& key,
162 const Key& key, uint32_t hash, AllocationPolicy allocator) { 162 uint32_t hash) {
163 // Find a matching entry. 163 // Find a matching entry.
164 Entry* p = Probe(key, hash); 164 Entry* p = Probe(key, hash);
165 DCHECK(!p->exists()); 165 DCHECK(!p->exists());
166 166
167 // No entry found; construct one in-place in the empty slot using placement 167 // No entry found; construct one in-place in the empty slot using placement
168 // new. 168 // new.
169 new (p) Entry(key, Value(), hash); 169 new (p) Entry(key, Value(), hash);
170 occupancy_++; 170 occupancy_++;
171 171
172 // Grow the map if we reached >= 80% occupancy. 172 // Grow the map if we reached >= 80% occupancy.
173 if (occupancy_ + occupancy_ / 4 >= capacity_) { 173 if (occupancy_ + occupancy_ / 4 >= capacity_) {
174 Resize(allocator); 174 Resize();
175 p = Probe(key, hash); 175 p = Probe(key, hash);
176 } 176 }
177 177
178 return p; 178 return p;
179 } 179 }
180 180
181 template <typename Key, typename Value, class AllocationPolicy> 181 template <typename Key, typename Value, class AllocationPolicy>
182 Value TemplateHashMapImpl<Key, Value, AllocationPolicy>::Remove(const Key& key, 182 Value TemplateHashMapImpl<Key, Value, AllocationPolicy>::Remove(const Key& key,
183 uint32_t hash) { 183 uint32_t hash) {
184 // Lookup the entry for the key to remove. 184 // Lookup the entry for the key to remove.
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 if (p >= end) { 283 if (p >= end) {
284 p = map_; 284 p = map_;
285 } 285 }
286 } 286 }
287 287
288 return p; 288 return p;
289 } 289 }
290 290
291 template <typename Key, typename Value, class AllocationPolicy> 291 template <typename Key, typename Value, class AllocationPolicy>
292 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize( 292 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize(
293 uint32_t capacity, AllocationPolicy allocator) { 293 uint32_t capacity) {
294 DCHECK(base::bits::IsPowerOfTwo32(capacity)); 294 DCHECK(base::bits::IsPowerOfTwo32(capacity));
295 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); 295 map_ = reinterpret_cast<Entry*>(allocator_.New(capacity * sizeof(Entry)));
296 if (map_ == nullptr) { 296 if (map_ == nullptr) {
297 FATAL("Out of memory: HashMap::Initialize"); 297 FATAL("Out of memory: HashMap::Initialize");
298 return; 298 return;
299 } 299 }
300 capacity_ = capacity; 300 capacity_ = capacity;
301 Clear(); 301 Clear();
302 } 302 }
303 303
304 template <typename Key, typename Value, class AllocationPolicy> 304 template <typename Key, typename Value, class AllocationPolicy>
305 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize( 305 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize() {
306 AllocationPolicy allocator) {
307 Entry* map = map_; 306 Entry* map = map_;
308 uint32_t n = occupancy_; 307 uint32_t n = occupancy_;
309 308
310 // Allocate larger map. 309 // Allocate larger map.
311 Initialize(capacity_ * 2, allocator); 310 Initialize(capacity_ * 2);
312 311
313 // Rehash all current entries. 312 // Rehash all current entries.
314 for (Entry* p = map; n > 0; p++) { 313 for (Entry* p = map; n > 0; p++) {
315 if (p->exists()) { 314 if (p->exists()) {
316 Entry* entry = LookupOrInsert(p->key, p->hash, allocator); 315 Entry* entry = LookupOrInsert(p->key, p->hash);
317 entry->value = p->value; 316 entry->value = p->value;
318 n--; 317 n--;
319 } 318 }
320 } 319 }
321 320
322 // Delete old map. 321 // Delete old map.
323 AllocationPolicy::Delete(map); 322 allocator_.Delete(map);
324 } 323 }
325 324
326 // A hash map for pointer keys and values with an STL-like interface. 325 // A hash map for pointer keys and values with an STL-like interface.
327 template <class Key, class Value, class AllocationPolicy> 326 template <class Key, class Value, class AllocationPolicy>
328 class TemplateHashMap 327 class TemplateHashMap
329 : private TemplateHashMapImpl<void*, void*, AllocationPolicy> { 328 : private TemplateHashMapImpl<void*, void*, AllocationPolicy> {
330 public: 329 public:
331 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT 330 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
332 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT 331 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
333 struct value_type { 332 struct value_type {
(...skipping 27 matching lines...) Expand all
361 void*, void*, AllocationPolicy>::MatchFun match, 360 void*, void*, AllocationPolicy>::MatchFun match,
362 AllocationPolicy allocator = AllocationPolicy()) 361 AllocationPolicy allocator = AllocationPolicy())
363 : TemplateHashMapImpl<void*, void*, AllocationPolicy>( 362 : TemplateHashMapImpl<void*, void*, AllocationPolicy>(
364 match, 363 match,
365 TemplateHashMapImpl<void*, void*, 364 TemplateHashMapImpl<void*, void*,
366 AllocationPolicy>::kDefaultHashMapCapacity, 365 AllocationPolicy>::kDefaultHashMapCapacity,
367 allocator) {} 366 allocator) {}
368 367
369 Iterator begin() const { return Iterator(this, this->Start()); } 368 Iterator begin() const { return Iterator(this, this->Start()); }
370 Iterator end() const { return Iterator(this, nullptr); } 369 Iterator end() const { return Iterator(this, nullptr); }
371 Iterator find(Key* key, bool insert = false, 370 Iterator find(Key* key, bool insert = false) {
372 AllocationPolicy allocator = AllocationPolicy()) {
373 if (insert) { 371 if (insert) {
374 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); 372 return Iterator(this, this->LookupOrInsert(key, key->Hash()));
375 } 373 }
376 return Iterator(this, this->Lookup(key, key->Hash())); 374 return Iterator(this, this->Lookup(key, key->Hash()));
377 } 375 }
378 }; 376 };
379 377
380 } // namespace base 378 } // namespace base
381 } // namespace v8 379 } // namespace v8
382 380
383 #endif // V8_BASE_HASHMAP_H_ 381 #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