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

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

Issue 2968003004: Revert "The current growth strategy for growable arrays allocates a backing array of size 2 at (emp… (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 3048 matching lines...) Expand 10 before | Expand all | Expand 10 after
3059 // Do not preserve the original implicit constructor, if any. 3059 // Do not preserve the original implicit constructor, if any.
3060 orig_implicit_ctor = Function::null(); 3060 orig_implicit_ctor = Function::null();
3061 } 3061 }
3062 func.set_owner(patch_class); 3062 func.set_owner(patch_class);
3063 new_functions.Add(func); 3063 new_functions.Add(func);
3064 } 3064 }
3065 if (!orig_implicit_ctor.IsNull()) { 3065 if (!orig_implicit_ctor.IsNull()) {
3066 // Preserve the original implicit constructor. 3066 // Preserve the original implicit constructor.
3067 new_functions.Add(orig_implicit_ctor); 3067 new_functions.Add(orig_implicit_ctor);
3068 } 3068 }
3069 Array& new_list = Array::Handle(Array::MakeFixedLength(new_functions)); 3069 Array& new_list = Array::Handle(Array::MakeArray(new_functions));
3070 SetFunctions(new_list); 3070 SetFunctions(new_list);
3071 3071
3072 // Merge the two list of fields. Raise an error when duplicates are found or 3072 // Merge the two list of fields. Raise an error when duplicates are found or
3073 // when a public field is being added. 3073 // when a public field is being added.
3074 orig_list = fields(); 3074 orig_list = fields();
3075 orig_len = orig_list.Length(); 3075 orig_len = orig_list.Length();
3076 patch_list = patch.fields(); 3076 patch_list = patch.fields();
3077 patch_len = patch_list.Length(); 3077 patch_len = patch_list.Length();
3078 3078
3079 Field& field = Field::Handle(); 3079 Field& field = Field::Handle();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
3123 for (intptr_t i = 0; i < num_formals; i++) { 3123 for (intptr_t i = 0; i < num_formals; i++) {
3124 if (i > 0) { 3124 if (i > 0) {
3125 src_pieces.Add(Symbols::CommaSpace()); 3125 src_pieces.Add(Symbols::CommaSpace());
3126 } 3126 }
3127 piece ^= formal_params.At(i); 3127 piece ^= formal_params.At(i);
3128 src_pieces.Add(piece); 3128 src_pieces.Add(piece);
3129 } 3129 }
3130 src_pieces.Add(Symbols::RParenArrow()); 3130 src_pieces.Add(Symbols::RParenArrow());
3131 src_pieces.Add(expr); 3131 src_pieces.Add(expr);
3132 src_pieces.Add(Symbols::Semicolon()); 3132 src_pieces.Add(Symbols::Semicolon());
3133 return String::ConcatAll(Array::Handle(Array::MakeFixedLength(src_pieces))); 3133 return String::ConcatAll(Array::Handle(Array::MakeArray(src_pieces)));
3134 } 3134 }
3135 3135
3136 3136
3137 RawFunction* Function::EvaluateHelper(const Class& cls, 3137 RawFunction* Function::EvaluateHelper(const Class& cls,
3138 const String& expr, 3138 const String& expr,
3139 const Array& param_names, 3139 const Array& param_names,
3140 bool is_static) { 3140 bool is_static) {
3141 const String& func_src = 3141 const String& func_src =
3142 String::Handle(BuildClosureSource(param_names, expr)); 3142 String::Handle(BuildClosureSource(param_names, expr));
3143 Script& script = Script::Handle(); 3143 Script& script = Script::Handle();
(...skipping 5629 matching lines...) Expand 10 before | Expand all | Expand 10 after
8773 if (curr == Token::kNEWLINE) { 8773 if (curr == Token::kNEWLINE) {
8774 for (int i = 0; i < indent; i++) { 8774 for (int i = 0; i < indent; i++) {
8775 literals.Add(Symbols::TwoSpaces()); 8775 literals.Add(Symbols::TwoSpaces());
8776 } 8776 }
8777 } 8777 }
8778 8778
8779 // Setup for next iteration. 8779 // Setup for next iteration.
8780 prev = curr; 8780 prev = curr;
8781 curr = next; 8781 curr = next;
8782 } 8782 }
8783 const Array& source = Array::Handle(Array::MakeFixedLength(literals)); 8783 const Array& source = Array::Handle(Array::MakeArray(literals));
8784 return String::ConcatAll(source); 8784 return String::ConcatAll(source);
8785 } 8785 }
8786 8786
8787 8787
8788 intptr_t TokenStream::ComputeSourcePosition(TokenPosition tok_pos) const { 8788 intptr_t TokenStream::ComputeSourcePosition(TokenPosition tok_pos) const {
8789 Zone* zone = Thread::Current()->zone(); 8789 Zone* zone = Thread::Current()->zone();
8790 Iterator iterator(zone, *this, TokenPosition::kMinSource, 8790 Iterator iterator(zone, *this, TokenPosition::kMinSource,
8791 Iterator::kAllTokens); 8791 Iterator::kAllTokens);
8792 intptr_t src_pos = 0; 8792 intptr_t src_pos = 0;
8793 Token::Kind kind = iterator.CurrentTokenKind(); 8793 Token::Kind kind = iterator.CurrentTokenKind();
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after
9545 token_pos = Smi::New(0); 9545 token_pos = Smi::New(0);
9546 line_starts_list.Add(token_pos); 9546 line_starts_list.Add(token_pos);
9547 while (tkit.CurrentTokenKind() != Token::kEOS) { 9547 while (tkit.CurrentTokenKind() != Token::kEOS) {
9548 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { 9548 if (tkit.CurrentTokenKind() == Token::kNEWLINE) {
9549 cur_line++; 9549 cur_line++;
9550 token_pos = Smi::New(tkit.CurrentPosition().value() + 1); 9550 token_pos = Smi::New(tkit.CurrentPosition().value() + 1);
9551 line_starts_list.Add(token_pos); 9551 line_starts_list.Add(token_pos);
9552 } 9552 }
9553 tkit.Advance(); 9553 tkit.Advance();
9554 } 9554 }
9555 line_starts_array = Array::MakeFixedLength(line_starts_list); 9555 line_starts_array = Array::MakeArray(line_starts_list);
9556 set_line_starts(line_starts_array); 9556 set_line_starts(line_starts_array);
9557 } 9557 }
9558 9558
9559 ASSERT(line_starts_array.Length() > 0); 9559 ASSERT(line_starts_array.Length() > 0);
9560 intptr_t offset = target_token_pos.Pos(); 9560 intptr_t offset = target_token_pos.Pos();
9561 intptr_t min = 0; 9561 intptr_t min = 0;
9562 intptr_t max = line_starts_array.Length() - 1; 9562 intptr_t max = line_starts_array.Length() - 1;
9563 9563
9564 // Binary search to find the line containing this offset. 9564 // Binary search to find the line containing this offset.
9565 while (min < max) { 9565 while (min < max) {
(...skipping 1227 matching lines...) Expand 10 before | Expand all | Expand 10 after
10793 for (intptr_t j = 0; j < functions.Length(); j++) { 10793 for (intptr_t j = 0; j < functions.Length(); j++) {
10794 func ^= functions.At(j); 10794 func ^= functions.At(j);
10795 if (func.is_external()) { 10795 if (func.is_external()) {
10796 owner_script = func.script(); 10796 owner_script = func.script();
10797 AddScriptIfUnique(scripts, owner_script); 10797 AddScriptIfUnique(scripts, owner_script);
10798 } 10798 }
10799 } 10799 }
10800 } 10800 }
10801 10801
10802 // Create the array of scripts and cache it in loaded_scripts_. 10802 // Create the array of scripts and cache it in loaded_scripts_.
10803 const Array& scripts_array = Array::Handle(Array::MakeFixedLength(scripts)); 10803 const Array& scripts_array = Array::Handle(Array::MakeArray(scripts));
10804 StorePointer(&raw_ptr()->loaded_scripts_, scripts_array.raw()); 10804 StorePointer(&raw_ptr()->loaded_scripts_, scripts_array.raw());
10805 } 10805 }
10806 return loaded_scripts(); 10806 return loaded_scripts();
10807 } 10807 }
10808 10808
10809 10809
10810 // TODO(hausner): we might want to add a script dictionary to the 10810 // TODO(hausner): we might want to add a script dictionary to the
10811 // library class to make this lookup faster. 10811 // library class to make this lookup faster.
10812 RawScript* Library::LookupScript(const String& url) const { 10812 RawScript* Library::LookupScript(const String& url) const {
10813 const intptr_t url_length = url.Length(); 10813 const intptr_t url_length = url.Length();
(...skipping 11444 matching lines...) Expand 10 before | Expand all | Expand 10 after
22258 ASSERT(new_length != len); // Unnecessary copying of array. 22258 ASSERT(new_length != len); // Unnecessary copying of array.
22259 PassiveObject& obj = PassiveObject::Handle(zone); 22259 PassiveObject& obj = PassiveObject::Handle(zone);
22260 for (int i = 0; i < len; i++) { 22260 for (int i = 0; i < len; i++) {
22261 obj = source.At(i); 22261 obj = source.At(i);
22262 result.SetAt(i, obj); 22262 result.SetAt(i, obj);
22263 } 22263 }
22264 return result.raw(); 22264 return result.raw();
22265 } 22265 }
22266 22266
22267 22267
22268 RawArray* Array::MakeFixedLength(const GrowableObjectArray& growable_array, 22268 RawArray* Array::MakeArray(const GrowableObjectArray& growable_array) {
22269 bool unique) {
22270 ASSERT(!growable_array.IsNull()); 22269 ASSERT(!growable_array.IsNull());
22271 Thread* thread = Thread::Current();
22272 Zone* zone = thread->zone();
22273 intptr_t used_len = growable_array.Length(); 22270 intptr_t used_len = growable_array.Length();
22274 // Get the type arguments and prepare to copy them. 22271 // Get the type arguments and prepare to copy them.
22275 const TypeArguments& type_arguments = 22272 const TypeArguments& type_arguments =
22276 TypeArguments::Handle(growable_array.GetTypeArguments()); 22273 TypeArguments::Handle(growable_array.GetTypeArguments());
22277 if (used_len == 0) { 22274 if ((used_len == 0) && (type_arguments.IsNull())) {
22278 if (type_arguments.IsNull() && !unique) { 22275 // This is a raw List (as in no type arguments), so we can return the
22279 // This is a raw List (as in no type arguments), so we can return the 22276 // simple empty array.
22280 // simple empty array. 22277 return Object::empty_array().raw();
22281 return Object::empty_array().raw();
22282 }
22283
22284 // The backing array may be a shared instance, or may not have correct
22285 // type parameters. Create a new empty array.
22286 Heap::Space space = thread->IsMutatorThread() ? Heap::kNew : Heap::kOld;
22287 Array& array = Array::Handle(zone, Array::New(0, space));
22288 array.SetTypeArguments(type_arguments);
22289 return array.raw();
22290 } 22278 }
22291 intptr_t capacity_len = growable_array.Capacity(); 22279 intptr_t capacity_len = growable_array.Capacity();
22280 Zone* zone = Thread::Current()->zone();
22292 const Array& array = Array::Handle(zone, growable_array.data()); 22281 const Array& array = Array::Handle(zone, growable_array.data());
22293 ASSERT(array.IsArray()); 22282 ASSERT(array.IsArray());
22294 array.SetTypeArguments(type_arguments); 22283 array.SetTypeArguments(type_arguments);
22295 intptr_t capacity_size = Array::InstanceSize(capacity_len); 22284 intptr_t capacity_size = Array::InstanceSize(capacity_len);
22296 intptr_t used_size = Array::InstanceSize(used_len); 22285 intptr_t used_size = Array::InstanceSize(used_len);
22297 NoSafepointScope no_safepoint; 22286 NoSafepointScope no_safepoint;
22298 22287
22299 // If there is any left over space fill it with either an Array object or 22288 // If there is any left over space fill it with either an Array object or
22300 // just a plain object (depending on the amount of left over space) so 22289 // just a plain object (depending on the amount of left over space) so
22301 // that it can be traversed over successfully during garbage collection. 22290 // that it can be traversed over successfully during garbage collection.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
22361 RawImmutableArray* ImmutableArray::New(intptr_t len, Heap::Space space) { 22350 RawImmutableArray* ImmutableArray::New(intptr_t len, Heap::Space space) {
22362 ASSERT(Isolate::Current()->object_store()->immutable_array_class() != 22351 ASSERT(Isolate::Current()->object_store()->immutable_array_class() !=
22363 Class::null()); 22352 Class::null());
22364 return reinterpret_cast<RawImmutableArray*>(Array::New(kClassId, len, space)); 22353 return reinterpret_cast<RawImmutableArray*>(Array::New(kClassId, len, space));
22365 } 22354 }
22366 22355
22367 22356
22368 void GrowableObjectArray::Add(const Object& value, Heap::Space space) const { 22357 void GrowableObjectArray::Add(const Object& value, Heap::Space space) const {
22369 ASSERT(!IsNull()); 22358 ASSERT(!IsNull());
22370 if (Length() == Capacity()) { 22359 if (Length() == Capacity()) {
22371 // Grow from 0 to 3, and then double + 1. 22360 // TODO(Issue 2500): Need a better growth strategy.
22372 intptr_t new_capacity = (Capacity() * 2) | 3; 22361 intptr_t new_capacity = (Capacity() == 0) ? 4 : Capacity() * 2;
22373 if (new_capacity <= Capacity()) { 22362 if (new_capacity <= Capacity()) {
22374 Exceptions::ThrowOOM(); 22363 Exceptions::ThrowOOM();
22375 UNREACHABLE(); 22364 UNREACHABLE();
22376 } 22365 }
22377 Grow(new_capacity, space); 22366 Grow(new_capacity, space);
22378 } 22367 }
22379 ASSERT(Length() < Capacity()); 22368 ASSERT(Length() < Capacity());
22380 intptr_t index = Length(); 22369 intptr_t index = Length();
22381 SetLength(index + 1); 22370 SetLength(index + 1);
22382 SetAt(index, value); 22371 SetAt(index, value);
(...skipping 16 matching lines...) Expand all
22399 const Array& contents = Array::Handle(data()); 22388 const Array& contents = Array::Handle(data());
22400 const PassiveObject& obj = PassiveObject::Handle(contents.At(index)); 22389 const PassiveObject& obj = PassiveObject::Handle(contents.At(index));
22401 contents.SetAt(index, Object::null_object()); 22390 contents.SetAt(index, Object::null_object());
22402 SetLength(index); 22391 SetLength(index);
22403 return obj.raw(); 22392 return obj.raw();
22404 } 22393 }
22405 22394
22406 22395
22407 RawGrowableObjectArray* GrowableObjectArray::New(intptr_t capacity, 22396 RawGrowableObjectArray* GrowableObjectArray::New(intptr_t capacity,
22408 Heap::Space space) { 22397 Heap::Space space) {
22409 RawArray* raw_data = (capacity == 0) ? Object::empty_array().raw() 22398 const Array& data = Array::Handle(Array::New(capacity, space));
22410 : Array::New(capacity, space);
22411 const Array& data = Array::Handle(raw_data);
22412 return New(data, space); 22399 return New(data, space);
22413 } 22400 }
22414 22401
22415 22402
22416 RawGrowableObjectArray* GrowableObjectArray::New(const Array& array, 22403 RawGrowableObjectArray* GrowableObjectArray::New(const Array& array,
22417 Heap::Space space) { 22404 Heap::Space space) {
22418 ASSERT(Isolate::Current()->object_store()->growable_object_array_class() != 22405 ASSERT(Isolate::Current()->object_store()->growable_object_array_class() !=
22419 Class::null()); 22406 Class::null());
22420 GrowableObjectArray& result = GrowableObjectArray::Handle(); 22407 GrowableObjectArray& result = GrowableObjectArray::Handle();
22421 { 22408 {
(...skipping 1214 matching lines...) Expand 10 before | Expand all | Expand 10 after
23636 return UserTag::null(); 23623 return UserTag::null();
23637 } 23624 }
23638 23625
23639 23626
23640 const char* UserTag::ToCString() const { 23627 const char* UserTag::ToCString() const {
23641 const String& tag_label = String::Handle(label()); 23628 const String& tag_label = String::Handle(label());
23642 return tag_label.ToCString(); 23629 return tag_label.ToCString();
23643 } 23630 }
23644 23631
23645 } // namespace dart 23632 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698