| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 #include "src/identity-map.h" | 5 #include "src/identity-map.h" |
| 6 | 6 |
| 7 #include "src/heap/heap.h" | 7 #include "src/heap/heap.h" |
| 8 #include "src/heap/heap-inl.h" |
| 8 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
| 9 | 10 |
| 10 namespace v8 { | 11 namespace v8 { |
| 11 namespace internal { | 12 namespace internal { |
| 12 | 13 |
| 13 static const int kInitialIdentityMapSize = 4; | 14 static const int kInitialIdentityMapSize = 4; |
| 14 static const int kResizeFactor = 4; | 15 static const int kResizeFactor = 4; |
| 15 | 16 |
| 16 IdentityMapBase::~IdentityMapBase() { | 17 IdentityMapBase::~IdentityMapBase() { |
| 17 if (keys_) { | 18 if (keys_) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 30 | 31 |
| 31 IdentityMapBase::RawEntry IdentityMapBase::Insert(Handle<Object> key) { | 32 IdentityMapBase::RawEntry IdentityMapBase::Insert(Handle<Object> key) { |
| 32 AllowHandleDereference for_lookup; | 33 AllowHandleDereference for_lookup; |
| 33 int index = InsertIndex(*key); | 34 int index = InsertIndex(*key); |
| 34 DCHECK_GE(index, 0); | 35 DCHECK_GE(index, 0); |
| 35 return &values_[index]; | 36 return &values_[index]; |
| 36 } | 37 } |
| 37 | 38 |
| 38 | 39 |
| 39 int IdentityMapBase::Hash(Object* address) { | 40 int IdentityMapBase::Hash(Object* address) { |
| 41 CHECK_NE(address, heap_->not_mapped_symbol()); |
| 40 uintptr_t raw_address = reinterpret_cast<uintptr_t>(address); | 42 uintptr_t raw_address = reinterpret_cast<uintptr_t>(address); |
| 41 CHECK_NE(0U, raw_address); // Cannot store Smi 0 as a key here, sorry. | |
| 42 // Xor some of the upper bits, since the lower 2 or 3 are usually aligned. | 43 // Xor some of the upper bits, since the lower 2 or 3 are usually aligned. |
| 43 return static_cast<int>((raw_address >> 11) ^ raw_address); | 44 return static_cast<int>((raw_address >> 11) ^ raw_address); |
| 44 } | 45 } |
| 45 | 46 |
| 46 | 47 |
| 47 int IdentityMapBase::LookupIndex(Object* address) { | 48 int IdentityMapBase::LookupIndex(Object* address) { |
| 48 int start = Hash(address) & mask_; | 49 int start = Hash(address) & mask_; |
| 50 Object* not_mapped = heap_->not_mapped_symbol(); |
| 49 for (int index = start; index < size_; index++) { | 51 for (int index = start; index < size_; index++) { |
| 50 if (keys_[index] == address) return index; // Found. | 52 if (keys_[index] == address) return index; // Found. |
| 51 if (keys_[index] == nullptr) return -1; // Not found. | 53 if (keys_[index] == not_mapped) return -1; // Not found. |
| 52 } | 54 } |
| 53 for (int index = 0; index < start; index++) { | 55 for (int index = 0; index < start; index++) { |
| 54 if (keys_[index] == address) return index; // Found. | 56 if (keys_[index] == address) return index; // Found. |
| 55 if (keys_[index] == nullptr) return -1; // Not found. | 57 if (keys_[index] == not_mapped) return -1; // Not found. |
| 56 } | 58 } |
| 57 return -1; | 59 return -1; |
| 58 } | 60 } |
| 59 | 61 |
| 60 | 62 |
| 61 int IdentityMapBase::InsertIndex(Object* address) { | 63 int IdentityMapBase::InsertIndex(Object* address) { |
| 64 Object* not_mapped = heap_->not_mapped_symbol(); |
| 62 while (true) { | 65 while (true) { |
| 63 int start = Hash(address) & mask_; | 66 int start = Hash(address) & mask_; |
| 64 int limit = size_ / 2; | 67 int limit = size_ / 2; |
| 65 // Search up to {limit} entries. | 68 // Search up to {limit} entries. |
| 66 for (int index = start; --limit > 0; index = (index + 1) & mask_) { | 69 for (int index = start; --limit > 0; index = (index + 1) & mask_) { |
| 67 if (keys_[index] == address) return index; // Found. | 70 if (keys_[index] == address) return index; // Found. |
| 68 if (keys_[index] == nullptr) { // Free entry. | 71 if (keys_[index] == not_mapped) { // Free entry. |
| 69 keys_[index] = address; | 72 keys_[index] = address; |
| 70 return index; | 73 return index; |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 Resize(); // Should only have to resize once, since we grow 4x. | 76 Resize(); // Should only have to resize once, since we grow 4x. |
| 74 } | 77 } |
| 75 UNREACHABLE(); | 78 UNREACHABLE(); |
| 76 return -1; | 79 return -1; |
| 77 } | 80 } |
| 78 | 81 |
| 79 | 82 |
| 80 // Searches this map for the given key using the object's address | 83 // Searches this map for the given key using the object's address |
| 81 // as the identity, returning: | 84 // as the identity, returning: |
| 82 // found => a pointer to the storage location for the value | 85 // found => a pointer to the storage location for the value |
| 83 // not found => a pointer to a new storage location for the value | 86 // not found => a pointer to a new storage location for the value |
| 84 IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Handle<Object> key) { | 87 IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Handle<Object> key) { |
| 85 Heap::OptionalRelocationLock lock(heap_, concurrent_); | 88 Heap::OptionalRelocationLock lock(heap_, concurrent_); |
| 86 RawEntry result; | 89 RawEntry result; |
| 87 if (size_ == 0) { | 90 if (size_ == 0) { |
| 88 // Allocate the initial storage for keys and values. | 91 // Allocate the initial storage for keys and values. |
| 89 size_ = kInitialIdentityMapSize; | 92 size_ = kInitialIdentityMapSize; |
| 90 mask_ = kInitialIdentityMapSize - 1; | 93 mask_ = kInitialIdentityMapSize - 1; |
| 91 gc_counter_ = heap_->gc_count(); | 94 gc_counter_ = heap_->gc_count(); |
| 92 | 95 |
| 93 keys_ = zone_->NewArray<Object*>(size_); | 96 keys_ = zone_->NewArray<Object*>(size_); |
| 94 memset(keys_, 0, sizeof(Object*) * size_); | 97 Object* not_mapped = heap_->not_mapped_symbol(); |
| 98 for (int i = 0; i < size_; i++) keys_[i] = not_mapped; |
| 95 values_ = zone_->NewArray<void*>(size_); | 99 values_ = zone_->NewArray<void*>(size_); |
| 96 memset(values_, 0, sizeof(void*) * size_); | 100 memset(values_, 0, sizeof(void*) * size_); |
| 97 | 101 |
| 98 heap_->RegisterStrongRoots(keys_, keys_ + size_); | 102 heap_->RegisterStrongRoots(keys_, keys_ + size_); |
| 99 result = Insert(key); | 103 result = Insert(key); |
| 100 } else { | 104 } else { |
| 101 // Perform an optimistic lookup. | 105 // Perform an optimistic lookup. |
| 102 result = Lookup(key); | 106 result = Lookup(key); |
| 103 if (result == nullptr) { | 107 if (result == nullptr) { |
| 104 // Miss; rehash if there was a GC, then insert. | 108 // Miss; rehash if there was a GC, then insert. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 128 | 132 |
| 129 | 133 |
| 130 void IdentityMapBase::Rehash() { | 134 void IdentityMapBase::Rehash() { |
| 131 // Record the current GC counter. | 135 // Record the current GC counter. |
| 132 gc_counter_ = heap_->gc_count(); | 136 gc_counter_ = heap_->gc_count(); |
| 133 // Assume that most objects won't be moved. | 137 // Assume that most objects won't be moved. |
| 134 ZoneVector<std::pair<Object*, void*>> reinsert(zone_); | 138 ZoneVector<std::pair<Object*, void*>> reinsert(zone_); |
| 135 // Search the table looking for keys that wouldn't be found with their | 139 // Search the table looking for keys that wouldn't be found with their |
| 136 // current hashcode and evacuate them. | 140 // current hashcode and evacuate them. |
| 137 int last_empty = -1; | 141 int last_empty = -1; |
| 142 Object* not_mapped = heap_->not_mapped_symbol(); |
| 138 for (int i = 0; i < size_; i++) { | 143 for (int i = 0; i < size_; i++) { |
| 139 if (keys_[i] == nullptr) { | 144 if (keys_[i] == not_mapped) { |
| 140 last_empty = i; | 145 last_empty = i; |
| 141 } else { | 146 } else { |
| 142 int pos = Hash(keys_[i]) & mask_; | 147 int pos = Hash(keys_[i]) & mask_; |
| 143 if (pos <= last_empty || pos > i) { | 148 if (pos <= last_empty || pos > i) { |
| 144 // Evacuate an entry that is in the wrong place. | 149 // Evacuate an entry that is in the wrong place. |
| 145 reinsert.push_back(std::pair<Object*, void*>(keys_[i], values_[i])); | 150 reinsert.push_back(std::pair<Object*, void*>(keys_[i], values_[i])); |
| 146 keys_[i] = nullptr; | 151 keys_[i] = not_mapped; |
| 147 values_[i] = nullptr; | 152 values_[i] = nullptr; |
| 148 last_empty = i; | 153 last_empty = i; |
| 149 } | 154 } |
| 150 } | 155 } |
| 151 } | 156 } |
| 152 // Reinsert all the key/value pairs that were in the wrong place. | 157 // Reinsert all the key/value pairs that were in the wrong place. |
| 153 for (auto pair : reinsert) { | 158 for (auto pair : reinsert) { |
| 154 int index = InsertIndex(pair.first); | 159 int index = InsertIndex(pair.first); |
| 155 DCHECK_GE(index, 0); | 160 DCHECK_GE(index, 0); |
| 156 DCHECK_NULL(values_[index]); | 161 DCHECK_NULL(values_[index]); |
| 157 values_[index] = pair.second; | 162 values_[index] = pair.second; |
| 158 } | 163 } |
| 159 } | 164 } |
| 160 | 165 |
| 161 | 166 |
| 162 void IdentityMapBase::Resize() { | 167 void IdentityMapBase::Resize() { |
| 163 // Grow the internal storage and reinsert all the key/value pairs. | 168 // Grow the internal storage and reinsert all the key/value pairs. |
| 164 int old_size = size_; | 169 int old_size = size_; |
| 165 Object** old_keys = keys_; | 170 Object** old_keys = keys_; |
| 166 void** old_values = values_; | 171 void** old_values = values_; |
| 167 | 172 |
| 168 size_ = size_ * kResizeFactor; | 173 size_ = size_ * kResizeFactor; |
| 169 mask_ = size_ - 1; | 174 mask_ = size_ - 1; |
| 170 gc_counter_ = heap_->gc_count(); | 175 gc_counter_ = heap_->gc_count(); |
| 171 | 176 |
| 172 CHECK_LE(size_, (1024 * 1024 * 16)); // that would be extreme... | 177 CHECK_LE(size_, (1024 * 1024 * 16)); // that would be extreme... |
| 173 | 178 |
| 174 keys_ = zone_->NewArray<Object*>(size_); | 179 keys_ = zone_->NewArray<Object*>(size_); |
| 175 memset(keys_, 0, sizeof(Object*) * size_); | 180 Object* not_mapped = heap_->not_mapped_symbol(); |
| 181 for (int i = 0; i < size_; i++) keys_[i] = not_mapped; |
| 176 values_ = zone_->NewArray<void*>(size_); | 182 values_ = zone_->NewArray<void*>(size_); |
| 177 memset(values_, 0, sizeof(void*) * size_); | 183 memset(values_, 0, sizeof(void*) * size_); |
| 178 | 184 |
| 179 for (int i = 0; i < old_size; i++) { | 185 for (int i = 0; i < old_size; i++) { |
| 180 if (old_keys[i] == nullptr) continue; | 186 if (old_keys[i] == not_mapped) continue; |
| 181 int index = InsertIndex(old_keys[i]); | 187 int index = InsertIndex(old_keys[i]); |
| 182 DCHECK_GE(index, 0); | 188 DCHECK_GE(index, 0); |
| 183 values_[index] = old_values[i]; | 189 values_[index] = old_values[i]; |
| 184 } | 190 } |
| 185 | 191 |
| 186 // Unregister old keys and register new keys. | 192 // Unregister old keys and register new keys. |
| 187 heap_->UnregisterStrongRoots(old_keys); | 193 heap_->UnregisterStrongRoots(old_keys); |
| 188 heap_->RegisterStrongRoots(keys_, keys_ + size_); | 194 heap_->RegisterStrongRoots(keys_, keys_ + size_); |
| 189 } | 195 } |
| 190 } // namespace internal | 196 } // namespace internal |
| 191 } // namespace v8 | 197 } // namespace v8 |
| OLD | NEW |