| OLD | NEW |
| 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 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/objects.h" | 7 #include "src/objects.h" |
| 8 #include "src/transitions-inl.h" | 8 #include "src/transitions-inl.h" |
| 9 #include "src/utils.h" | 9 #include "src/utils.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 | 14 |
| 15 Handle<TransitionArray> TransitionArray::Allocate(Isolate* isolate, | 15 Handle<TransitionArray> TransitionArray::Allocate(Isolate* isolate, |
| 16 int number_of_transitions) { | 16 int number_of_transitions, |
| 17 Handle<FixedArray> array = | 17 int slack) { |
| 18 isolate->factory()->NewFixedArray(ToKeyIndex(number_of_transitions)); | 18 Handle<FixedArray> array = isolate->factory()->NewFixedArray( |
| 19 LengthFor(number_of_transitions + slack)); |
| 19 array->set(kPrototypeTransitionsIndex, Smi::FromInt(0)); | 20 array->set(kPrototypeTransitionsIndex, Smi::FromInt(0)); |
| 21 array->set(kTransitionLengthIndex, Smi::FromInt(number_of_transitions)); |
| 20 return Handle<TransitionArray>::cast(array); | 22 return Handle<TransitionArray>::cast(array); |
| 21 } | 23 } |
| 22 | 24 |
| 23 | 25 |
| 24 Handle<TransitionArray> TransitionArray::AllocateSimple(Isolate* isolate, | 26 Handle<TransitionArray> TransitionArray::AllocateSimple(Isolate* isolate, |
| 25 Handle<Map> target) { | 27 Handle<Map> target) { |
| 26 Handle<FixedArray> array = | 28 Handle<FixedArray> array = |
| 27 isolate->factory()->NewFixedArray(kSimpleTransitionSize); | 29 isolate->factory()->NewFixedArray(kSimpleTransitionSize); |
| 28 array->set(kSimpleTransitionTarget, *target); | 30 array->set(kSimpleTransitionTarget, *target); |
| 29 return Handle<TransitionArray>::cast(array); | 31 return Handle<TransitionArray>::cast(array); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 DCHECK(!containing_map->transitions()->IsFullTransitionArray()); | 69 DCHECK(!containing_map->transitions()->IsFullTransitionArray()); |
| 68 int nof = containing_map->transitions()->number_of_transitions(); | 70 int nof = containing_map->transitions()->number_of_transitions(); |
| 69 | 71 |
| 70 // A transition array may shrink during GC. | 72 // A transition array may shrink during GC. |
| 71 Handle<TransitionArray> result = Allocate(containing_map->GetIsolate(), nof); | 73 Handle<TransitionArray> result = Allocate(containing_map->GetIsolate(), nof); |
| 72 DisallowHeapAllocation no_gc; | 74 DisallowHeapAllocation no_gc; |
| 73 int new_nof = containing_map->transitions()->number_of_transitions(); | 75 int new_nof = containing_map->transitions()->number_of_transitions(); |
| 74 if (new_nof != nof) { | 76 if (new_nof != nof) { |
| 75 DCHECK(new_nof == 0); | 77 DCHECK(new_nof == 0); |
| 76 result->Shrink(ToKeyIndex(0)); | 78 result->Shrink(ToKeyIndex(0)); |
| 79 result->SetNumberOfTransitions(0); |
| 77 } else if (nof == 1) { | 80 } else if (nof == 1) { |
| 78 result->NoIncrementalWriteBarrierCopyFrom( | 81 result->NoIncrementalWriteBarrierCopyFrom( |
| 79 containing_map->transitions(), kSimpleTransitionIndex, 0); | 82 containing_map->transitions(), kSimpleTransitionIndex, 0); |
| 80 } | 83 } |
| 81 | 84 |
| 82 result->set_back_pointer_storage( | 85 result->set_back_pointer_storage( |
| 83 containing_map->transitions()->back_pointer_storage()); | 86 containing_map->transitions()->back_pointer_storage()); |
| 84 return result; | 87 return result; |
| 85 } | 88 } |
| 86 | 89 |
| 87 | 90 |
| 88 Handle<TransitionArray> TransitionArray::CopyInsert(Handle<Map> map, | 91 Handle<TransitionArray> TransitionArray::Insert(Handle<Map> map, |
| 89 Handle<Name> name, | 92 Handle<Name> name, |
| 90 Handle<Map> target, | 93 Handle<Map> target, |
| 91 SimpleTransitionFlag flag) { | 94 SimpleTransitionFlag flag) { |
| 92 if (!map->HasTransitionArray()) { | 95 if (!map->HasTransitionArray()) { |
| 93 return TransitionArray::NewWith(map, name, target, flag); | 96 return TransitionArray::NewWith(map, name, target, flag); |
| 94 } | 97 } |
| 95 | 98 |
| 96 int number_of_transitions = map->transitions()->number_of_transitions(); | 99 int number_of_transitions = map->transitions()->number_of_transitions(); |
| 97 int new_size = number_of_transitions; | 100 int new_nof = number_of_transitions; |
| 98 | 101 |
| 99 int insertion_index = map->transitions()->Search(*name); | 102 int insertion_index = map->transitions()->Search(*name); |
| 100 if (insertion_index == kNotFound) ++new_size; | 103 if (insertion_index == kNotFound) ++new_nof; |
| 104 DCHECK(new_nof <= kMaxNumberOfTransitions); |
| 101 | 105 |
| 102 Handle<TransitionArray> result = Allocate(map->GetIsolate(), new_size); | 106 if (new_nof <= map->transitions()->number_of_transitions_storage()) { |
| 107 DisallowHeapAllocation no_gc; |
| 108 TransitionArray* array = map->transitions(); |
| 109 |
| 110 if (insertion_index != kNotFound) { |
| 111 array->SetTarget(insertion_index, *target); |
| 112 return handle(array); |
| 113 } |
| 114 |
| 115 array->SetNumberOfTransitions(new_nof); |
| 116 uint32_t hash = name->Hash(); |
| 117 for (insertion_index = number_of_transitions; insertion_index > 0; |
| 118 --insertion_index) { |
| 119 Name* key = array->GetKey(insertion_index - 1); |
| 120 if (key->Hash() <= hash) break; |
| 121 array->SetKey(insertion_index, key); |
| 122 array->SetTarget(insertion_index, array->GetTarget(insertion_index - 1)); |
| 123 } |
| 124 array->SetKey(insertion_index, *name); |
| 125 array->SetTarget(insertion_index, *target); |
| 126 return handle(array); |
| 127 } |
| 128 |
| 129 Handle<TransitionArray> result = Allocate( |
| 130 map->GetIsolate(), new_nof, |
| 131 Map::SlackForArraySize(number_of_transitions, kMaxNumberOfTransitions)); |
| 103 | 132 |
| 104 // The map's transition array may grown smaller during the allocation above as | 133 // The map's transition array may grown smaller during the allocation above as |
| 105 // it was weakly traversed, though it is guaranteed not to disappear. Trim the | 134 // it was weakly traversed, though it is guaranteed not to disappear. Trim the |
| 106 // result copy if needed, and recompute variables. | 135 // result copy if needed, and recompute variables. |
| 107 DCHECK(map->HasTransitionArray()); | 136 DCHECK(map->HasTransitionArray()); |
| 108 DisallowHeapAllocation no_gc; | 137 DisallowHeapAllocation no_gc; |
| 109 TransitionArray* array = map->transitions(); | 138 TransitionArray* array = map->transitions(); |
| 110 if (array->number_of_transitions() != number_of_transitions) { | 139 if (array->number_of_transitions() != number_of_transitions) { |
| 111 DCHECK(array->number_of_transitions() < number_of_transitions); | 140 DCHECK(array->number_of_transitions() < number_of_transitions); |
| 112 | 141 |
| 113 number_of_transitions = array->number_of_transitions(); | 142 number_of_transitions = array->number_of_transitions(); |
| 114 new_size = number_of_transitions; | 143 new_nof = number_of_transitions; |
| 115 | 144 |
| 116 insertion_index = array->Search(*name); | 145 insertion_index = array->Search(*name); |
| 117 if (insertion_index == kNotFound) ++new_size; | 146 if (insertion_index == kNotFound) ++new_nof; |
| 118 | 147 |
| 119 result->Shrink(ToKeyIndex(new_size)); | 148 result->Shrink(ToKeyIndex(new_nof)); |
| 149 result->SetNumberOfTransitions(new_nof); |
| 120 } | 150 } |
| 121 | 151 |
| 122 if (array->HasPrototypeTransitions()) { | 152 if (array->HasPrototypeTransitions()) { |
| 123 result->SetPrototypeTransitions(array->GetPrototypeTransitions()); | 153 result->SetPrototypeTransitions(array->GetPrototypeTransitions()); |
| 124 } | 154 } |
| 125 | 155 |
| 126 if (insertion_index != kNotFound) { | |
| 127 for (int i = 0; i < number_of_transitions; ++i) { | |
| 128 if (i != insertion_index) { | |
| 129 result->NoIncrementalWriteBarrierCopyFrom(array, i, i); | |
| 130 } | |
| 131 } | |
| 132 result->NoIncrementalWriteBarrierSet(insertion_index, *name, *target); | |
| 133 result->set_back_pointer_storage(array->back_pointer_storage()); | |
| 134 return result; | |
| 135 } | |
| 136 | |
| 137 insertion_index = 0; | 156 insertion_index = 0; |
| 138 for (; insertion_index < number_of_transitions; ++insertion_index) { | 157 for (; insertion_index < number_of_transitions; ++insertion_index) { |
| 139 if (InsertionPointFound(array->GetKey(insertion_index), *name)) break; | 158 if (InsertionPointFound(array->GetKey(insertion_index), *name)) break; |
| 140 result->NoIncrementalWriteBarrierCopyFrom( | 159 result->NoIncrementalWriteBarrierCopyFrom( |
| 141 array, insertion_index, insertion_index); | 160 array, insertion_index, insertion_index); |
| 142 } | 161 } |
| 143 | 162 |
| 144 result->NoIncrementalWriteBarrierSet(insertion_index, *name, *target); | 163 result->NoIncrementalWriteBarrierSet(insertion_index, *name, *target); |
| 145 | 164 |
| 146 for (; insertion_index < number_of_transitions; ++insertion_index) { | 165 for (; insertion_index < number_of_transitions; ++insertion_index) { |
| 147 result->NoIncrementalWriteBarrierCopyFrom( | 166 result->NoIncrementalWriteBarrierCopyFrom( |
| 148 array, insertion_index, insertion_index + 1); | 167 array, insertion_index, insertion_index + 1); |
| 149 } | 168 } |
| 150 | 169 |
| 151 result->set_back_pointer_storage(array->back_pointer_storage()); | 170 result->set_back_pointer_storage(array->back_pointer_storage()); |
| 152 return result; | 171 return result; |
| 153 } | 172 } |
| 154 | 173 |
| 155 | 174 |
| 156 } } // namespace v8::internal | 175 } } // namespace v8::internal |
| OLD | NEW |