Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Side by Side Diff: runtime/vm/object.cc

Issue 2949803002: New growth strategy for growable arrays (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/become.h" 10 #include "vm/become.h"
(...skipping 22048 matching lines...) Expand 10 before | Expand all | Expand 10 after
22059 ASSERT(new_length != len); // Unnecessary copying of array. 22059 ASSERT(new_length != len); // Unnecessary copying of array.
22060 PassiveObject& obj = PassiveObject::Handle(zone); 22060 PassiveObject& obj = PassiveObject::Handle(zone);
22061 for (int i = 0; i < len; i++) { 22061 for (int i = 0; i < len; i++) {
22062 obj = source.At(i); 22062 obj = source.At(i);
22063 result.SetAt(i, obj); 22063 result.SetAt(i, obj);
22064 } 22064 }
22065 return result.raw(); 22065 return result.raw();
22066 } 22066 }
22067 22067
22068 22068
22069 RawArray* Array::MakeArray(const GrowableObjectArray& growable_array) { 22069 RawArray* Array::MakeArray(const GrowableObjectArray& growable_array,
erikcorry 2017/06/21 09:06:01 Perhaps call this MakeNongrowableArray?
Lasse Reichstein Nielsen 2017/06/21 09:14:15 Or MakeArrayNongrowable, which doesn't sound as if
Aske Simon Christensen 2017/06/21 12:19:33 Array::MakeFixedLength corresponds to the Dart-sid
22070 bool unique) {
22070 ASSERT(!growable_array.IsNull()); 22071 ASSERT(!growable_array.IsNull());
22072 Thread* thread = Thread::Current();
22073 Zone* zone = thread->zone();
22071 intptr_t used_len = growable_array.Length(); 22074 intptr_t used_len = growable_array.Length();
22072 // Get the type arguments and prepare to copy them. 22075 // Get the type arguments and prepare to copy them.
22073 const TypeArguments& type_arguments = 22076 const TypeArguments& type_arguments =
22074 TypeArguments::Handle(growable_array.GetTypeArguments()); 22077 TypeArguments::Handle(growable_array.GetTypeArguments());
22075 if ((used_len == 0) && (type_arguments.IsNull())) { 22078 if (used_len == 0) {
22076 // This is a raw List (as in no type arguments), so we can return the 22079 if (type_arguments.IsNull() && !unique) {
22077 // simple empty array. 22080 // This is a raw List (as in no type arguments), so we can return the
22078 return Object::empty_array().raw(); 22081 // simple empty array.
22082 return Object::empty_array().raw();
22083 }
22084
22085 // The backing array may be a shared instance, or may not have correct
Lasse Reichstein Nielsen 2017/06/21 07:36:21 This function isn't talking about "backing arrays"
erikcorry 2017/06/21 07:58:31 The naming of this method is terrible. It's not u
Lasse Reichstein Nielsen 2017/06/21 09:14:15 ACK, my bad. I actually recognized that at the cal
22086 // type parameters. Create a new empty array.
22087 Heap::Space space = thread->IsMutatorThread() ? Heap::kNew : Heap::kOld;
22088 Array& array = Array::Handle(zone, Array::New(0, space));
22089 array.SetTypeArguments(type_arguments);
22090 return array.raw();
22079 } 22091 }
22080 intptr_t capacity_len = growable_array.Capacity(); 22092 intptr_t capacity_len = growable_array.Capacity();
22081 Zone* zone = Thread::Current()->zone();
22082 const Array& array = Array::Handle(zone, growable_array.data()); 22093 const Array& array = Array::Handle(zone, growable_array.data());
22083 array.SetTypeArguments(type_arguments); 22094 array.SetTypeArguments(type_arguments);
22084 intptr_t capacity_size = Array::InstanceSize(capacity_len); 22095 intptr_t capacity_size = Array::InstanceSize(capacity_len);
22085 intptr_t used_size = Array::InstanceSize(used_len); 22096 intptr_t used_size = Array::InstanceSize(used_len);
22086 NoSafepointScope no_safepoint; 22097 NoSafepointScope no_safepoint;
22087 22098
22088 // If there is any left over space fill it with either an Array object or 22099 // If there is any left over space fill it with either an Array object or
22089 // just a plain object (depending on the amount of left over space) so 22100 // just a plain object (depending on the amount of left over space) so
22090 // that it can be traversed over successfully during garbage collection. 22101 // that it can be traversed over successfully during garbage collection.
22091 Object::MakeUnusedSpaceTraversable(array, capacity_size, used_size); 22102 Object::MakeUnusedSpaceTraversable(array, capacity_size, used_size);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
22150 RawImmutableArray* ImmutableArray::New(intptr_t len, Heap::Space space) { 22161 RawImmutableArray* ImmutableArray::New(intptr_t len, Heap::Space space) {
22151 ASSERT(Isolate::Current()->object_store()->immutable_array_class() != 22162 ASSERT(Isolate::Current()->object_store()->immutable_array_class() !=
22152 Class::null()); 22163 Class::null());
22153 return reinterpret_cast<RawImmutableArray*>(Array::New(kClassId, len, space)); 22164 return reinterpret_cast<RawImmutableArray*>(Array::New(kClassId, len, space));
22154 } 22165 }
22155 22166
22156 22167
22157 void GrowableObjectArray::Add(const Object& value, Heap::Space space) const { 22168 void GrowableObjectArray::Add(const Object& value, Heap::Space space) const {
22158 ASSERT(!IsNull()); 22169 ASSERT(!IsNull());
22159 if (Length() == Capacity()) { 22170 if (Length() == Capacity()) {
22160 // TODO(Issue 2500): Need a better growth strategy. 22171 intptr_t new_capacity = (Capacity() == 0) ? 3 : Capacity() * 2 + 1;
erikcorry 2017/06/21 09:06:01 Lasse's trick for branchless calculations works he
22161 intptr_t new_capacity = (Capacity() == 0) ? 4 : Capacity() * 2;
22162 if (new_capacity <= Capacity()) { 22172 if (new_capacity <= Capacity()) {
22163 Exceptions::ThrowOOM(); 22173 Exceptions::ThrowOOM();
22164 UNREACHABLE(); 22174 UNREACHABLE();
22165 } 22175 }
22166 Grow(new_capacity, space); 22176 Grow(new_capacity, space);
22167 } 22177 }
22168 ASSERT(Length() < Capacity()); 22178 ASSERT(Length() < Capacity());
22169 intptr_t index = Length(); 22179 intptr_t index = Length();
22170 SetLength(index + 1); 22180 SetLength(index + 1);
22171 SetAt(index, value); 22181 SetAt(index, value);
(...skipping 16 matching lines...) Expand all
22188 const Array& contents = Array::Handle(data()); 22198 const Array& contents = Array::Handle(data());
22189 const PassiveObject& obj = PassiveObject::Handle(contents.At(index)); 22199 const PassiveObject& obj = PassiveObject::Handle(contents.At(index));
22190 contents.SetAt(index, Object::null_object()); 22200 contents.SetAt(index, Object::null_object());
22191 SetLength(index); 22201 SetLength(index);
22192 return obj.raw(); 22202 return obj.raw();
22193 } 22203 }
22194 22204
22195 22205
22196 RawGrowableObjectArray* GrowableObjectArray::New(intptr_t capacity, 22206 RawGrowableObjectArray* GrowableObjectArray::New(intptr_t capacity,
22197 Heap::Space space) { 22207 Heap::Space space) {
22198 const Array& data = Array::Handle(Array::New(capacity, space)); 22208 RawArray* raw_data = (capacity == 0) ? Object::empty_array().raw()
22209 : Array::New(capacity, space);
22210 const Array& data = Array::Handle(raw_data);
22199 return New(data, space); 22211 return New(data, space);
22200 } 22212 }
22201 22213
22202 22214
22203 RawGrowableObjectArray* GrowableObjectArray::New(const Array& array, 22215 RawGrowableObjectArray* GrowableObjectArray::New(const Array& array,
22204 Heap::Space space) { 22216 Heap::Space space) {
22205 ASSERT(Isolate::Current()->object_store()->growable_object_array_class() != 22217 ASSERT(Isolate::Current()->object_store()->growable_object_array_class() !=
22206 Class::null()); 22218 Class::null());
22207 GrowableObjectArray& result = GrowableObjectArray::Handle(); 22219 GrowableObjectArray& result = GrowableObjectArray::Handle();
22208 { 22220 {
(...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after
23419 return UserTag::null(); 23431 return UserTag::null();
23420 } 23432 }
23421 23433
23422 23434
23423 const char* UserTag::ToCString() const { 23435 const char* UserTag::ToCString() const {
23424 const String& tag_label = String::Handle(label()); 23436 const String& tag_label = String::Handle(label());
23425 return tag_label.ToCString(); 23437 return tag_label.ToCString();
23426 } 23438 }
23427 23439
23428 } // namespace dart 23440 } // namespace dart
OLDNEW
« runtime/vm/object.h ('K') | « runtime/vm/object.h ('k') | runtime/vm/profiler_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698