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