hashmap.h 17.3 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

lpy's avatar
lpy committed
5 6 7 8 9 10
// The reason we write our own hash map instead of using unordered_map in STL,
// is that STL containers use a mutex pool on debug build, which will lead to
// deadlock when we are using async signal handler.

#ifndef V8_BASE_HASHMAP_H_
#define V8_BASE_HASHMAP_H_
11

12 13
#include <stdlib.h>

14
#include "src/base/bits.h"
15
#include "src/base/hashmap-entry.h"
16
#include "src/base/logging.h"
17

18
namespace v8 {
lpy's avatar
lpy committed
19 20 21 22 23 24 25
namespace base {

class DefaultAllocationPolicy {
 public:
  V8_INLINE void* New(size_t size) { return malloc(size); }
  V8_INLINE static void Delete(void* p) { free(p); }
};
26

27
template <typename Key, typename Value, class MatchFun, class AllocationPolicy>
28
class TemplateHashMapImpl {
29
 public:
30
  typedef TemplateHashMapEntry<Key, Value> Entry;
31

32 33 34 35 36
  // The default capacity.  This is used by the call sites which want
  // to pass in a non-default AllocationPolicy but want to use the
  // default value of capacity specified by the implementation.
  static const uint32_t kDefaultHashMapCapacity = 8;

37 38
  // initial_capacity is the size of the initial hash map;
  // it must be a power of 2 (and thus must not be 0).
39 40
  TemplateHashMapImpl(uint32_t capacity = kDefaultHashMapCapacity,
                      MatchFun match = MatchFun(),
41
                      AllocationPolicy allocator = AllocationPolicy());
42

43 44 45 46 47
  // Clones the given hashmap and creates a copy with the same entries.
  TemplateHashMapImpl(const TemplateHashMapImpl<Key, Value, MatchFun,
                                                AllocationPolicy>* original,
                      AllocationPolicy allocator = AllocationPolicy());

48
  ~TemplateHashMapImpl();
49

50
  // If an entry with matching key is found, returns that entry.
51 52
  // Otherwise, nullptr is returned.
  Entry* Lookup(const Key& key, uint32_t hash) const;
53 54 55

  // If an entry with matching key is found, returns that entry.
  // If no matching entry is found, a new entry is inserted with
56
  // corresponding key, key hash, and default initialized value.
57 58
  Entry* LookupOrInsert(const Key& key, uint32_t hash,
                        AllocationPolicy allocator = AllocationPolicy());
59

60 61 62 63 64 65 66
  // If an entry with matching key is found, returns that entry.
  // If no matching entry is found, a new entry is inserted with
  // corresponding key, key hash, and value created by func.
  template <typename Func>
  Entry* LookupOrInsert(const Key& key, uint32_t hash, const Func& value_func,
                        AllocationPolicy allocator = AllocationPolicy());

67 68
  Entry* InsertNew(const Key& key, uint32_t hash,
                   AllocationPolicy allocator = AllocationPolicy());
69

70
  // Removes the entry with matching key.
71 72
  // It returns the value of the deleted entry
  // or null if there is no value for such key.
73
  Value Remove(const Key& key, uint32_t hash);
74

75 76 77
  // Empties the hash map (occupancy() == 0).
  void Clear();

78 79 80 81 82 83 84 85
  // Empties the map and makes it unusable for allocation.
  void Invalidate() {
    AllocationPolicy::Delete(map_);
    map_ = nullptr;
    occupancy_ = 0;
    capacity_ = 0;
  }

86
  // The number of (non-empty) entries in the table.
87
  uint32_t occupancy() const { return occupancy_; }
88 89 90 91

  // The capacity of the table. The implementation
  // makes sure that occupancy is at most 80% of
  // the table capacity.
92
  uint32_t capacity() const { return capacity_; }
93 94 95

  // Iteration
  //
96
  // for (Entry* p = map.Start(); p != nullptr; p = map.Next(p)) {
97 98 99 100 101 102
  //   ...
  // }
  //
  // If entries are inserted during iteration, the effect of
  // calling Next() is undefined.
  Entry* Start() const;
103
  Entry* Next(Entry* entry) const;
104

105 106 107 108 109 110 111 112
  void Reset(AllocationPolicy allocator) {
    Initialize(capacity_, allocator);
    occupancy_ = 0;
  }

 protected:
  void Initialize(uint32_t capacity, AllocationPolicy allocator);

113 114 115 116
 private:
  Entry* map_;
  uint32_t capacity_;
  uint32_t occupancy_;
117 118
  // TODO(leszeks): This takes up space even if it has no state, maybe replace
  // with something that does the empty base optimisation e.g. std::tuple
119
  MatchFun match_;
120

121
  Entry* map_end() const { return map_ + capacity_; }
122
  Entry* Probe(const Key& key, uint32_t hash) const;
123
  Entry* FillEmptyEntry(Entry* entry, const Key& key, const Value& value,
124 125 126
                        uint32_t hash,
                        AllocationPolicy allocator = AllocationPolicy());
  void Resize(AllocationPolicy allocator);
127 128

  DISALLOW_COPY_AND_ASSIGN(TemplateHashMapImpl);
129
};
130 131 132
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::
133
    TemplateHashMapImpl(uint32_t initial_capacity, MatchFun match,
134 135
                        AllocationPolicy allocator)
    : match_(match) {
136
  Initialize(initial_capacity, allocator);
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::
    TemplateHashMapImpl(const TemplateHashMapImpl<Key, Value, MatchFun,
                                                  AllocationPolicy>* original,
                        AllocationPolicy allocator)
    : capacity_(original->capacity_),
      occupancy_(original->occupancy_),
      match_(original->match_) {
  map_ = reinterpret_cast<Entry*>(allocator.New(capacity_ * sizeof(Entry)));
  memcpy(map_, original->map_, capacity_ * sizeof(Entry));
}

152 153 154 155
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
TemplateHashMapImpl<Key, Value, MatchFun,
                    AllocationPolicy>::~TemplateHashMapImpl() {
156
  AllocationPolicy::Delete(map_);
157 158
}

159 160 161 162 163
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Lookup(
    const Key& key, uint32_t hash) const {
164 165
  Entry* entry = Probe(key, hash);
  return entry->exists() ? entry : nullptr;
166 167
}

168 169 170 171
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert(
172
    const Key& key, uint32_t hash, AllocationPolicy allocator) {
173 174 175 176 177 178 179 180 181 182
  return LookupOrInsert(key, hash, []() { return Value(); }, allocator);
}

template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
template <typename Func>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::LookupOrInsert(
    const Key& key, uint32_t hash, const Func& value_func,
    AllocationPolicy allocator) {
183
  // Find a matching entry.
184 185 186
  Entry* entry = Probe(key, hash);
  if (entry->exists()) {
    return entry;
187 188
  }

189
  return FillEmptyEntry(entry, key, value_func(), hash, allocator);
190 191
}

192 193 194 195
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::InsertNew(
196
    const Key& key, uint32_t hash, AllocationPolicy allocator) {
197
  Entry* entry = Probe(key, hash);
198
  return FillEmptyEntry(entry, key, Value(), hash, allocator);
199 200
}

201 202 203 204
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
Value TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Remove(
    const Key& key, uint32_t hash) {
205 206
  // Lookup the entry for the key to remove.
  Entry* p = Probe(key, hash);
207
  if (!p->exists()) {
208
    // Key not found nothing to remove.
209
    return nullptr;
210 211
  }

212
  Value value = p->value;
213 214 215 216 217 218 219 220 221 222 223 224 225 226
  // To remove an entry we need to ensure that it does not create an empty
  // entry that will cause the search for another entry to stop too soon. If all
  // the entries between the entry to remove and the next empty slot have their
  // initial position inside this interval, clearing the entry to remove will
  // not break the search. If, while searching for the next empty entry, an
  // entry is encountered which does not have its initial position between the
  // entry to remove and the position looked at, then this entry can be moved to
  // the place of the entry to remove without breaking the search for it. The
  // entry made vacant by this move is now the entry to remove and the process
  // starts over.
  // Algorithm from http://en.wikipedia.org/wiki/Open_addressing.

  // This guarantees loop termination as there is at least one empty entry so
  // eventually the removed entry will have an empty entry after it.
227
  DCHECK(occupancy_ < capacity_);
228 229 230 231 232 233 234 235 236 237 238 239 240

  // p is the candidate entry to clear. q is used to scan forwards.
  Entry* q = p;  // Start at the entry to remove.
  while (true) {
    // Move q to the next entry.
    q = q + 1;
    if (q == map_end()) {
      q = map_;
    }

    // All entries between p and q have their initial position between p and q
    // and the entry p can be cleared without breaking the search for these
    // entries.
241
    if (!q->exists()) {
242 243 244 245 246 247 248 249 250
      break;
    }

    // Find the initial position for the entry at position q.
    Entry* r = map_ + (q->hash & (capacity_ - 1));

    // If the entry at position q has its initial position outside the range
    // between p and q it can be moved forward to position p and will still be
    // found. There is now a new candidate entry for clearing.
lpy's avatar
lpy committed
251
    if ((q > p && (r <= p || r > q)) || (q < p && (r <= p && r > q))) {
252 253 254 255 256 257
      *p = *q;
      p = q;
    }
  }

  // Clear the entry which is allowed to en emptied.
258
  p->clear();
259
  occupancy_--;
260
  return value;
261 262
}

263 264 265
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Clear() {
266
  // Mark all entries as empty.
267 268
  for (size_t i = 0; i < capacity_; ++i) {
    map_[i].clear();
269 270 271 272
  }
  occupancy_ = 0;
}

273 274 275 276
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Start() const {
277 278 279
  return Next(map_ - 1);
}

280 281 282 283 284
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Next(
    Entry* entry) const {
285
  const Entry* end = map_end();
286 287 288 289
  DCHECK(map_ - 1 <= entry && entry < end);
  for (entry++; entry < end; entry++) {
    if (entry->exists()) {
      return entry;
290 291
    }
  }
292
  return nullptr;
293 294
}

295 296 297 298 299
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Probe(
    const Key& key, uint32_t hash) const {
300
  DCHECK(base::bits::IsPowerOfTwo32(capacity_));
301 302
  size_t i = hash & (capacity_ - 1);
  DCHECK(i < capacity_);
303

304
  DCHECK(occupancy_ < capacity_);  // Guarantees loop termination.
305 306
  while (map_[i].exists() && !match_(hash, map_[i].hash, key, map_[i].key)) {
    i = (i + 1) & (capacity_ - 1);
307 308
  }

309
  return &map_[i];
310 311
}

312 313 314 315
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry*
TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::FillEmptyEntry(
316 317
    Entry* entry, const Key& key, const Value& value, uint32_t hash,
    AllocationPolicy allocator) {
318 319 320 321 322 323 324
  DCHECK(!entry->exists());

  new (entry) Entry(key, value, hash);
  occupancy_++;

  // Grow the map if we reached >= 80% occupancy.
  if (occupancy_ + occupancy_ / 4 >= capacity_) {
325
    Resize(allocator);
326 327 328 329
    entry = Probe(key, hash);
  }

  return entry;
330 331
}

332 333 334
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Initialize(
335
    uint32_t capacity, AllocationPolicy allocator) {
336
  DCHECK(base::bits::IsPowerOfTwo32(capacity));
337
  map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
338
  if (map_ == nullptr) {
lpy's avatar
lpy committed
339
    FATAL("Out of memory: HashMap::Initialize");
340 341 342 343 344 345
    return;
  }
  capacity_ = capacity;
  Clear();
}

346 347 348
template <typename Key, typename Value, typename MatchFun,
          class AllocationPolicy>
void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Resize(
349
    AllocationPolicy allocator) {
350 351 352 353
  Entry* map = map_;
  uint32_t n = occupancy_;

  // Allocate larger map.
354
  Initialize(capacity_ * 2, allocator);
355 356

  // Rehash all current entries.
357 358 359
  for (Entry* entry = map; n > 0; entry++) {
    if (entry->exists()) {
      Entry* new_entry = Probe(entry->key, entry->hash);
360 361
      new_entry = FillEmptyEntry(new_entry, entry->key, entry->value,
                                 entry->hash, allocator);
362 363 364 365 366
      n--;
    }
  }

  // Delete old map.
367
  AllocationPolicy::Delete(map);
368
}
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
// Match function which compares hashes before executing a (potentially
// expensive) key comparison.
template <typename Key, typename MatchFun>
struct HashEqualityThenKeyMatcher {
  explicit HashEqualityThenKeyMatcher(MatchFun match) : match_(match) {}

  bool operator()(uint32_t hash1, uint32_t hash2, const Key& key1,
                  const Key& key2) const {
    return hash1 == hash2 && match_(key1, key2);
  }

 private:
  MatchFun match_;
};

// Hashmap<void*, void*> which takes a custom key comparison function pointer.
386
template <typename AllocationPolicy>
387
class CustomMatcherTemplateHashMapImpl
388 389 390 391 392 393 394
    : public TemplateHashMapImpl<
          void*, void*,
          HashEqualityThenKeyMatcher<void*, bool (*)(void*, void*)>,
          AllocationPolicy> {
  typedef TemplateHashMapImpl<
      void*, void*, HashEqualityThenKeyMatcher<void*, bool (*)(void*, void*)>,
      AllocationPolicy>
395 396 397 398 399
      Base;

 public:
  typedef bool (*MatchFun)(void*, void*);

400 401 402
  CustomMatcherTemplateHashMapImpl(
      MatchFun match, uint32_t capacity = Base::kDefaultHashMapCapacity,
      AllocationPolicy allocator = AllocationPolicy())
403 404
      : Base(capacity, HashEqualityThenKeyMatcher<void*, MatchFun>(match),
             allocator) {}
405 406 407 408 409 410 411 412

  CustomMatcherTemplateHashMapImpl(
      const CustomMatcherTemplateHashMapImpl<AllocationPolicy>* original,
      AllocationPolicy allocator = AllocationPolicy())
      : Base(original, allocator) {}

 private:
  DISALLOW_COPY_AND_ASSIGN(CustomMatcherTemplateHashMapImpl);
413 414 415 416 417
};

typedef CustomMatcherTemplateHashMapImpl<DefaultAllocationPolicy>
    CustomMatcherHashMap;

418 419 420 421 422 423 424 425 426 427
// Match function which compares keys directly by equality.
template <typename Key>
struct KeyEqualityMatcher {
  bool operator()(uint32_t hash1, uint32_t hash2, const Key& key1,
                  const Key& key2) const {
    return key1 == key2;
  }
};

// Hashmap<void*, void*> which compares the key pointers directly.
428 429
template <typename AllocationPolicy>
class PointerTemplateHashMapImpl
430
    : public TemplateHashMapImpl<void*, void*, KeyEqualityMatcher<void*>,
431
                                 AllocationPolicy> {
432
  typedef TemplateHashMapImpl<void*, void*, KeyEqualityMatcher<void*>,
433 434
                              AllocationPolicy>
      Base;
435

436 437 438
 public:
  PointerTemplateHashMapImpl(uint32_t capacity = Base::kDefaultHashMapCapacity,
                             AllocationPolicy allocator = AllocationPolicy())
439
      : Base(capacity, KeyEqualityMatcher<void*>(), allocator) {}
440 441 442 443
};

typedef PointerTemplateHashMapImpl<DefaultAllocationPolicy> HashMap;

444
// A hash map for pointer keys and values with an STL-like interface.
445 446
template <class Key, class Value, class MatchFun, class AllocationPolicy>
class TemplateHashMap
447 448 449 450 451 452 453
    : private TemplateHashMapImpl<void*, void*,
                                  HashEqualityThenKeyMatcher<void*, MatchFun>,
                                  AllocationPolicy> {
  typedef TemplateHashMapImpl<void*, void*,
                              HashEqualityThenKeyMatcher<void*, MatchFun>,
                              AllocationPolicy>
      Base;
454

455
 public:
lpy's avatar
lpy committed
456
  STATIC_ASSERT(sizeof(Key*) == sizeof(void*));    // NOLINT
457 458 459 460 461 462 463 464 465 466 467 468 469 470
  STATIC_ASSERT(sizeof(Value*) == sizeof(void*));  // NOLINT
  struct value_type {
    Key* first;
    Value* second;
  };

  class Iterator {
   public:
    Iterator& operator++() {
      entry_ = map_->Next(entry_);
      return *this;
    }

    value_type* operator->() { return reinterpret_cast<value_type*>(entry_); }
lpy's avatar
lpy committed
471
    bool operator!=(const Iterator& other) { return entry_ != other.entry_; }
472 473

   private:
474
    Iterator(const Base* map, typename Base::Entry* entry)
lpy's avatar
lpy committed
475
        : map_(map), entry_(entry) {}
476

477 478
    const Base* map_;
    typename Base::Entry* entry_;
479 480 481 482

    friend class TemplateHashMap;
  };

483
  TemplateHashMap(MatchFun match,
484
                  AllocationPolicy allocator = AllocationPolicy())
485 486
      : Base(Base::kDefaultHashMapCapacity,
             HashEqualityThenKeyMatcher<void*, MatchFun>(match), allocator) {}
487 488

  Iterator begin() const { return Iterator(this, this->Start()); }
489
  Iterator end() const { return Iterator(this, nullptr); }
490 491
  Iterator find(Key* key, bool insert = false,
                AllocationPolicy allocator = AllocationPolicy()) {
492
    if (insert) {
493
      return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator));
494 495
    }
    return Iterator(this, this->Lookup(key, key->Hash()));
496 497 498
  }
};

lpy's avatar
lpy committed
499
}  // namespace base
500
}  // namespace v8
501

lpy's avatar
lpy committed
502
#endif  // V8_BASE_HASHMAP_H_