| 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/heap/heap-inl.h" |
| 9 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 static const int kInitialIdentityMapSize = 4; | 14 static const int kInitialIdentityMapSize = 4; |
| 15 static const int kResizeFactor = 4; | 15 static const int kResizeFactor = 4; |
| 16 | 16 |
| 17 IdentityMapBase::~IdentityMapBase() { | 17 IdentityMapBase::~IdentityMapBase() { |
| 18 if (keys_) { | 18 if (keys_) heap_->UnregisterStrongRoots(keys_); |
| 19 Heap::OptionalRelocationLock lock(heap_, concurrent_); | |
| 20 heap_->UnregisterStrongRoots(keys_); | |
| 21 } | |
| 22 } | 19 } |
| 23 | 20 |
| 24 | 21 |
| 25 IdentityMapBase::RawEntry IdentityMapBase::Lookup(Handle<Object> key) { | 22 IdentityMapBase::RawEntry IdentityMapBase::Lookup(Object* key) { |
| 26 AllowHandleDereference for_lookup; | 23 int index = LookupIndex(key); |
| 27 int index = LookupIndex(*key); | |
| 28 return index >= 0 ? &values_[index] : nullptr; | 24 return index >= 0 ? &values_[index] : nullptr; |
| 29 } | 25 } |
| 30 | 26 |
| 31 | 27 |
| 32 IdentityMapBase::RawEntry IdentityMapBase::Insert(Handle<Object> key) { | 28 IdentityMapBase::RawEntry IdentityMapBase::Insert(Object* key) { |
| 33 AllowHandleDereference for_lookup; | 29 int index = InsertIndex(key); |
| 34 int index = InsertIndex(*key); | |
| 35 DCHECK_GE(index, 0); | 30 DCHECK_GE(index, 0); |
| 36 return &values_[index]; | 31 return &values_[index]; |
| 37 } | 32 } |
| 38 | 33 |
| 39 | 34 |
| 40 int IdentityMapBase::Hash(Object* address) { | 35 int IdentityMapBase::Hash(Object* address) { |
| 41 CHECK_NE(address, heap_->not_mapped_symbol()); | 36 CHECK_NE(address, heap_->not_mapped_symbol()); |
| 42 uintptr_t raw_address = reinterpret_cast<uintptr_t>(address); | 37 uintptr_t raw_address = reinterpret_cast<uintptr_t>(address); |
| 43 // Xor some of the upper bits, since the lower 2 or 3 are usually aligned. | 38 // Xor some of the upper bits, since the lower 2 or 3 are usually aligned. |
| 44 return static_cast<int>((raw_address >> 11) ^ raw_address); | 39 return static_cast<int>((raw_address >> 11) ^ raw_address); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 72 } |
| 78 UNREACHABLE(); | 73 UNREACHABLE(); |
| 79 return -1; | 74 return -1; |
| 80 } | 75 } |
| 81 | 76 |
| 82 | 77 |
| 83 // Searches this map for the given key using the object's address | 78 // Searches this map for the given key using the object's address |
| 84 // as the identity, returning: | 79 // as the identity, returning: |
| 85 // found => a pointer to the storage location for the value | 80 // found => a pointer to the storage location for the value |
| 86 // not found => a pointer to a new storage location for the value | 81 // not found => a pointer to a new storage location for the value |
| 87 IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Handle<Object> key) { | 82 IdentityMapBase::RawEntry IdentityMapBase::GetEntry(Object* key) { |
| 88 Heap::OptionalRelocationLock lock(heap_, concurrent_); | |
| 89 RawEntry result; | 83 RawEntry result; |
| 90 if (size_ == 0) { | 84 if (size_ == 0) { |
| 91 // Allocate the initial storage for keys and values. | 85 // Allocate the initial storage for keys and values. |
| 92 size_ = kInitialIdentityMapSize; | 86 size_ = kInitialIdentityMapSize; |
| 93 mask_ = kInitialIdentityMapSize - 1; | 87 mask_ = kInitialIdentityMapSize - 1; |
| 94 gc_counter_ = heap_->gc_count(); | 88 gc_counter_ = heap_->gc_count(); |
| 95 | 89 |
| 96 keys_ = zone_->NewArray<Object*>(size_); | 90 keys_ = zone_->NewArray<Object*>(size_); |
| 97 Object* not_mapped = heap_->not_mapped_symbol(); | 91 Object* not_mapped = heap_->not_mapped_symbol(); |
| 98 for (int i = 0; i < size_; i++) keys_[i] = not_mapped; | 92 for (int i = 0; i < size_; i++) keys_[i] = not_mapped; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 111 } | 105 } |
| 112 } | 106 } |
| 113 return result; | 107 return result; |
| 114 } | 108 } |
| 115 | 109 |
| 116 | 110 |
| 117 // Searches this map for the given key using the object's address | 111 // Searches this map for the given key using the object's address |
| 118 // as the identity, returning: | 112 // as the identity, returning: |
| 119 // found => a pointer to the storage location for the value | 113 // found => a pointer to the storage location for the value |
| 120 // not found => {nullptr} | 114 // not found => {nullptr} |
| 121 IdentityMapBase::RawEntry IdentityMapBase::FindEntry(Handle<Object> key) { | 115 IdentityMapBase::RawEntry IdentityMapBase::FindEntry(Object* key) { |
| 122 if (size_ == 0) return nullptr; | 116 if (size_ == 0) return nullptr; |
| 123 | 117 |
| 124 Heap::OptionalRelocationLock lock(heap_, concurrent_); | |
| 125 RawEntry result = Lookup(key); | 118 RawEntry result = Lookup(key); |
| 126 if (result == nullptr && gc_counter_ != heap_->gc_count()) { | 119 if (result == nullptr && gc_counter_ != heap_->gc_count()) { |
| 127 Rehash(); // Rehash is expensive, so only do it in case of a miss. | 120 Rehash(); // Rehash is expensive, so only do it in case of a miss. |
| 128 result = Lookup(key); | 121 result = Lookup(key); |
| 129 } | 122 } |
| 130 return result; | 123 return result; |
| 131 } | 124 } |
| 132 | 125 |
| 133 | 126 |
| 134 void IdentityMapBase::Rehash() { | 127 void IdentityMapBase::Rehash() { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 DCHECK_GE(index, 0); | 181 DCHECK_GE(index, 0); |
| 189 values_[index] = old_values[i]; | 182 values_[index] = old_values[i]; |
| 190 } | 183 } |
| 191 | 184 |
| 192 // Unregister old keys and register new keys. | 185 // Unregister old keys and register new keys. |
| 193 heap_->UnregisterStrongRoots(old_keys); | 186 heap_->UnregisterStrongRoots(old_keys); |
| 194 heap_->RegisterStrongRoots(keys_, keys_ + size_); | 187 heap_->RegisterStrongRoots(keys_, keys_ + size_); |
| 195 } | 188 } |
| 196 } // namespace internal | 189 } // namespace internal |
| 197 } // namespace v8 | 190 } // namespace v8 |
| OLD | NEW |