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

5
#include "src/identity-map.h"
6

7
#include "src/base/functional.h"
8 9
#include "src/heap/heap.h"
#include "src/roots-inl.h"
10 11 12 13 14

namespace v8 {
namespace internal {

static const int kInitialIdentityMapSize = 4;
15
static const int kResizeFactor = 2;
16

17 18 19 20 21
IdentityMapBase::~IdentityMapBase() {
  // Clear must be called by the subclass to avoid calling the virtual
  // DeleteArray function from the destructor.
  DCHECK_NULL(keys_);
}
22 23 24

void IdentityMapBase::Clear() {
  if (keys_) {
25
    DCHECK(!is_iterable());
26
    heap_->UnregisterStrongRoots(FullObjectSlot(keys_));
27 28
    DeleteArray(keys_);
    DeleteArray(values_);
29 30 31
    keys_ = nullptr;
    values_ = nullptr;
    size_ = 0;
32
    capacity_ = 0;
33 34
    mask_ = 0;
  }
35 36
}

37 38 39
void IdentityMapBase::EnableIteration() {
  CHECK(!is_iterable());
  is_iterable_ = true;
40 41
}

42 43 44
void IdentityMapBase::DisableIteration() {
  CHECK(is_iterable());
  is_iterable_ = false;
45 46
}

47
int IdentityMapBase::ScanKeysFor(Address address) const {
48
  int start = Hash(address) & mask_;
49
  Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
50
  for (int index = start; index < capacity_; index++) {
51
    if (keys_[index] == address) return index;  // Found.
52
    if (keys_[index] == not_mapped) return -1;  // Not found.
53 54 55
  }
  for (int index = 0; index < start; index++) {
    if (keys_[index] == address) return index;  // Found.
56
    if (keys_[index] == not_mapped) return -1;  // Not found.
57 58 59 60
  }
  return -1;
}

61
int IdentityMapBase::InsertKey(Address address) {
62
  Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
63 64
  while (true) {
    int start = Hash(address) & mask_;
65
    int limit = capacity_ / 2;
66 67 68
    // Search up to {limit} entries.
    for (int index = start; --limit > 0; index = (index + 1) & mask_) {
      if (keys_[index] == address) return index;  // Found.
69
      if (keys_[index] == not_mapped) {           // Free entry.
70 71
        size_++;
        DCHECK_LE(size_, capacity_);
72 73 74 75
        keys_[index] = address;
        return index;
      }
    }
76 77
    // Should only have to resize once, since we grow 4x.
    Resize(capacity_ * kResizeFactor);
78 79 80 81
  }
  UNREACHABLE();
}

82 83
bool IdentityMapBase::DeleteIndex(int index, void** deleted_value) {
  if (deleted_value != nullptr) *deleted_value = values_[index];
84
  Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
85 86 87 88 89 90
  DCHECK_NE(keys_[index], not_mapped);
  keys_[index] = not_mapped;
  values_[index] = nullptr;
  size_--;
  DCHECK_GE(size_, 0);

91 92
  if (capacity_ > kInitialIdentityMapSize &&
      size_ * kResizeFactor < capacity_ / kResizeFactor) {
93
    Resize(capacity_ / kResizeFactor);
94
    return true;  // No need to fix collisions as resize reinserts keys.
95 96 97 98 99 100
  }

  // Move any collisions to their new correct location.
  int next_index = index;
  for (;;) {
    next_index = (next_index + 1) & mask_;
101
    Address key = keys_[next_index];
102 103 104 105 106 107 108 109 110
    if (key == not_mapped) break;

    int expected_index = Hash(key) & mask_;
    if (index < next_index) {
      if (index < expected_index && expected_index <= next_index) continue;
    } else {
      DCHECK_GT(index, next_index);
      if (index < expected_index || expected_index <= next_index) continue;
    }
111

112 113 114 115 116 117 118
    DCHECK_EQ(not_mapped, keys_[index]);
    DCHECK_NULL(values_[index]);
    std::swap(keys_[index], keys_[next_index]);
    std::swap(values_[index], values_[next_index]);
    index = next_index;
  }

119
  return true;
120 121
}

122
int IdentityMapBase::Lookup(Address key) const {
123 124 125 126 127 128 129 130 131
  int index = ScanKeysFor(key);
  if (index < 0 && gc_counter_ != heap_->gc_count()) {
    // Miss; rehash if there was a GC, then lookup again.
    const_cast<IdentityMapBase*>(this)->Rehash();
    index = ScanKeysFor(key);
  }
  return index;
}

132
int IdentityMapBase::LookupOrInsert(Address key) {
133 134 135 136 137 138 139 140 141 142 143
  // Perform an optimistic lookup.
  int index = ScanKeysFor(key);
  if (index < 0) {
    // Miss; rehash if there was a GC, then insert.
    if (gc_counter_ != heap_->gc_count()) Rehash();
    index = InsertKey(key);
  }
  DCHECK_GE(index, 0);
  return index;
}

144
int IdentityMapBase::Hash(Address address) const {
145
  CHECK_NE(address, ReadOnlyRoots(heap_).not_mapped_symbol().ptr());
146
  return static_cast<int>(hasher_(address));
147
}
148 149 150 151 152

// Searches this map for the given key using the object's address
// as the identity, returning:
//    found => a pointer to the storage location for the value
//    not found => a pointer to a new storage location for the value
153
IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Address key) {
154 155
  CHECK(!is_iterable());  // Don't allow insertion while iterable.
  if (capacity_ == 0) {
156
    // Allocate the initial storage for keys and values.
157
    capacity_ = kInitialIdentityMapSize;
158 159 160
    mask_ = kInitialIdentityMapSize - 1;
    gc_counter_ = heap_->gc_count();

161
    keys_ = reinterpret_cast<Address*>(NewPointerArray(capacity_));
162
    Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
163 164 165 166
    for (int i = 0; i < capacity_; i++) keys_[i] = not_mapped;
    values_ = NewPointerArray(capacity_);
    memset(values_, 0, sizeof(void*) * capacity_);

167 168
    heap_->RegisterStrongRoots(FullObjectSlot(keys_),
                               FullObjectSlot(keys_ + capacity_));
169
  }
170 171
  int index = LookupOrInsert(key);
  return &values_[index];
172 173 174 175 176 177
}

// Searches this map for the given key using the object's address
// as the identity, returning:
//    found => a pointer to the storage location for the value
//    not found => {nullptr}
178
IdentityMapBase::RawEntry IdentityMapBase::FindEntry(Address key) const {
179 180
  // Don't allow find by key while iterable (might rehash).
  CHECK(!is_iterable());
181
  if (size_ == 0) return nullptr;
182 183 184 185
  // Remove constness since lookup might have to rehash.
  int index = Lookup(key);
  return index >= 0 ? &values_[index] : nullptr;
}
186

187
// Deletes the given key from the map using the object's address as the
188 189
// identity, returning true iff the key was found (in which case, the value
// argument will be set to the deleted entry's value).
190
bool IdentityMapBase::DeleteEntry(Address key, void** deleted_value) {
191
  CHECK(!is_iterable());  // Don't allow deletion by key while iterable.
192
  if (size_ == 0) return false;
193
  int index = Lookup(key);
194 195
  if (index < 0) return false;  // No entry found.
  return DeleteIndex(index, deleted_value);
196 197
}

198
Address IdentityMapBase::KeyAtIndex(int index) const {
199 200
  DCHECK_LE(0, index);
  DCHECK_LT(index, capacity_);
201
  DCHECK_NE(keys_[index], ReadOnlyRoots(heap_).not_mapped_symbol().ptr());
202 203 204 205
  CHECK(is_iterable());  // Must be iterable to access by index;
  return keys_[index];
}

206 207 208
IdentityMapBase::RawEntry IdentityMapBase::EntryAtIndex(int index) const {
  DCHECK_LE(0, index);
  DCHECK_LT(index, capacity_);
209
  DCHECK_NE(keys_[index], ReadOnlyRoots(heap_).not_mapped_symbol().ptr());
210 211 212 213 214 215 216 217
  CHECK(is_iterable());  // Must be iterable to access by index;
  return &values_[index];
}

int IdentityMapBase::NextIndex(int index) const {
  DCHECK_LE(-1, index);
  DCHECK_LE(index, capacity_);
  CHECK(is_iterable());  // Must be iterable to access by index;
218
  Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
219
  for (++index; index < capacity_; ++index) {
220 221 222 223 224 225
    if (keys_[index] != not_mapped) {
      return index;
    }
  }
  return capacity_;
}
226 227

void IdentityMapBase::Rehash() {
228
  CHECK(!is_iterable());  // Can't rehash while iterating.
229 230 231
  // Record the current GC counter.
  gc_counter_ = heap_->gc_count();
  // Assume that most objects won't be moved.
232
  std::vector<std::pair<Address, void*>> reinsert;
233 234 235
  // Search the table looking for keys that wouldn't be found with their
  // current hashcode and evacuate them.
  int last_empty = -1;
236
  Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
237
  for (int i = 0; i < capacity_; i++) {
238
    if (keys_[i] == not_mapped) {
239 240 241 242 243
      last_empty = i;
    } else {
      int pos = Hash(keys_[i]) & mask_;
      if (pos <= last_empty || pos > i) {
        // Evacuate an entry that is in the wrong place.
244
        reinsert.push_back(std::pair<Address, void*>(keys_[i], values_[i]));
245
        keys_[i] = not_mapped;
246 247
        values_[i] = nullptr;
        last_empty = i;
248
        size_--;
249 250 251 252 253
      }
    }
  }
  // Reinsert all the key/value pairs that were in the wrong place.
  for (auto pair : reinsert) {
254
    int index = InsertKey(pair.first);
255 256 257 258 259
    DCHECK_GE(index, 0);
    values_[index] = pair.second;
  }
}

260 261 262 263 264
void IdentityMapBase::Resize(int new_capacity) {
  CHECK(!is_iterable());  // Can't resize while iterating.
  // Resize the internal storage and reinsert all the key/value pairs.
  DCHECK_GT(new_capacity, size_);
  int old_capacity = capacity_;
265
  Address* old_keys = keys_;
266 267
  void** old_values = values_;

268 269
  capacity_ = new_capacity;
  mask_ = capacity_ - 1;
270
  gc_counter_ = heap_->gc_count();
271
  size_ = 0;
272

273
  keys_ = reinterpret_cast<Address*>(NewPointerArray(capacity_));
274
  Address not_mapped = ReadOnlyRoots(heap_).not_mapped_symbol().ptr();
275 276 277
  for (int i = 0; i < capacity_; i++) keys_[i] = not_mapped;
  values_ = NewPointerArray(capacity_);
  memset(values_, 0, sizeof(void*) * capacity_);
278

279
  for (int i = 0; i < old_capacity; i++) {
280
    if (old_keys[i] == not_mapped) continue;
281
    int index = InsertKey(old_keys[i]);
282 283 284 285 286
    DCHECK_GE(index, 0);
    values_[index] = old_values[i];
  }

  // Unregister old keys and register new keys.
287 288 289
  heap_->UnregisterStrongRoots(FullObjectSlot(old_keys));
  heap_->RegisterStrongRoots(FullObjectSlot(keys_),
                             FullObjectSlot(keys_ + capacity_));
290 291 292 293

  // Delete old storage;
  DeleteArray(old_keys);
  DeleteArray(old_values);
294
}
295

296 297
}  // namespace internal
}  // namespace v8