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

Side by Side Diff: src/builtins.cc

Issue 8820014: Support Smi->Double->HeapObject transitions in constructed Arrays. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Disable smi only array optimizations Created 9 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 226 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
227 } 227 }
228 return array->SetElementsLength(args[1]); 228 return array->SetElementsLength(args[1]);
229 } 229 }
230 230
231 // Optimize the case where there are no parameters passed. 231 // Optimize the case where there are no parameters passed.
232 if (args.length() == 1) { 232 if (args.length() == 1) {
233 return array->Initialize(JSArray::kPreallocatedArrayElements); 233 return array->Initialize(JSArray::kPreallocatedArrayElements);
234 } 234 }
235 235
236 // Take the arguments as elements. 236 // Set length and elements on the array.
237 int number_of_elements = args.length() - 1; 237 int number_of_elements = args.length() - 1;
238 Smi* len = Smi::FromInt(number_of_elements); 238 array->set_length(Smi::FromInt(0));
Jakob Kummerow 2011/12/07 17:02:47 A comment explaining why the length must be set to
danno 2011/12/08 15:09:09 It's actually unneeded. Removed. On 2011/12/07 17:
239 MaybeObject* maybe_object =
240 array->EnsureCanContainElements(&args, 1, number_of_elements,
241 ALLOW_CONVERTED_DOUBLE_ELEMENTS);
242 if (maybe_object->IsFailure()) return maybe_object;
243
244 // Allocate an appropriately typed elements array.
239 Object* obj; 245 Object* obj;
240 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len->value()); 246 MaybeObject* maybe_obj;
241 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 247 ElementsKind elements_kind = array->GetElementsKind();
248 if (elements_kind == FAST_DOUBLE_ELEMENTS) {
249 maybe_obj = heap->AllocateUninitializedFixedDoubleArray(
250 number_of_elements);
251 } else {
252 maybe_obj = heap->AllocateFixedArrayWithHoles(number_of_elements);
253 }
254 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
255 FixedArrayBase* elms = FixedArrayBase::cast(obj);
Jakob Kummerow 2011/12/07 17:02:47 Thanks to templates, you can clean this up a littl
danno 2011/12/08 15:09:09 Done.
256
257 // Fill in the content
258 switch (array->GetElementsKind()) {
259 case FAST_SMI_ONLY_ELEMENTS: {
260 FixedArray* smi_elms = FixedArray::cast(elms);
261 for (int index = 0; index < number_of_elements; index++) {
262 smi_elms->set(index, args[index+1], SKIP_WRITE_BARRIER);
263 }
264 break;
265 }
266 case FAST_ELEMENTS: {
267 AssertNoAllocation no_gc;
268 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
269 FixedArray* object_elms = FixedArray::cast(elms);
270 for (int index = 0; index < number_of_elements; index++) {
271 object_elms->set(index, args[index+1], mode);
272 }
273 break;
274 }
275 case FAST_DOUBLE_ELEMENTS: {
276 FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms);
277 for (int index = 0; index < number_of_elements; index++) {
278 double_elms->set(index, args[index+1]->Number());
279 }
280 break;
281 }
282 default:
283 UNREACHABLE();
284 break;
242 } 285 }
243 286
244 // Set length and elements on the array. 287 array->set_elements(elms);
245 MaybeObject* maybe_object = 288 array->set_length(Smi::FromInt(number_of_elements));
246 array->EnsureCanContainElements(FixedArray::cast(obj));
247 if (maybe_object->IsFailure()) return maybe_object;
248
249 AssertNoAllocation no_gc;
250 FixedArray* elms = FixedArray::cast(obj);
251 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
252 // Fill in the content
253 for (int index = 0; index < number_of_elements; index++) {
254 elms->set(index, args[index+1], mode);
255 }
256
257 array->set_elements(FixedArray::cast(obj));
258 array->set_length(len);
259
260 return array; 289 return array;
261 } 290 }
262 291
263 292
264 MUST_USE_RESULT static MaybeObject* AllocateJSArray(Heap* heap) { 293 MUST_USE_RESULT static MaybeObject* AllocateJSArray(Heap* heap) {
265 JSFunction* array_function = 294 JSFunction* array_function =
266 heap->isolate()->context()->global_context()->array_function(); 295 heap->isolate()->context()->global_context()->array_function();
267 Object* result; 296 Object* result;
268 { MaybeObject* maybe_result = heap->AllocateJSObject(array_function); 297 { MaybeObject* maybe_result = heap->AllocateJSObject(array_function);
269 if (!maybe_result->ToObject(&result)) return maybe_result; 298 if (!maybe_result->ToObject(&result)) return maybe_result;
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 } 446 }
418 447
419 // Need to ensure that the arguments passed in args can be contained in 448 // Need to ensure that the arguments passed in args can be contained in
420 // the array. 449 // the array.
421 int args_length = args->length(); 450 int args_length = args->length();
422 if (first_added_arg >= args_length) return array->elements(); 451 if (first_added_arg >= args_length) return array->elements();
423 452
424 MaybeObject* maybe_array = array->EnsureCanContainElements( 453 MaybeObject* maybe_array = array->EnsureCanContainElements(
425 args, 454 args,
426 first_added_arg, 455 first_added_arg,
427 args_length - first_added_arg); 456 args_length - first_added_arg,
457 DONT_ALLOW_DOUBLE_ELEMENTS);
428 if (maybe_array->IsFailure()) return maybe_array; 458 if (maybe_array->IsFailure()) return maybe_array;
429 return array->elements(); 459 return array->elements();
430 } 460 }
431 461
432 462
433 static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap, 463 static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap,
434 JSArray* receiver) { 464 JSArray* receiver) {
435 if (!FLAG_clever_optimizations) return false; 465 if (!FLAG_clever_optimizations) return false;
436 Context* global_context = heap->isolate()->context()->global_context(); 466 Context* global_context = heap->isolate()->context()->global_context();
437 JSObject* array_proto = 467 JSObject* array_proto =
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 ASSERT(array->HasFastTypeElements()); 650 ASSERT(array->HasFastTypeElements());
621 651
622 int len = Smi::cast(array->length())->value(); 652 int len = Smi::cast(array->length())->value();
623 int to_add = args.length() - 1; 653 int to_add = args.length() - 1;
624 int new_length = len + to_add; 654 int new_length = len + to_add;
625 // Currently fixed arrays cannot grow too big, so 655 // Currently fixed arrays cannot grow too big, so
626 // we should never hit this case. 656 // we should never hit this case.
627 ASSERT(to_add <= (Smi::kMaxValue - len)); 657 ASSERT(to_add <= (Smi::kMaxValue - len));
628 658
629 MaybeObject* maybe_object = 659 MaybeObject* maybe_object =
630 array->EnsureCanContainElements(&args, 1, to_add); 660 array->EnsureCanContainElements(&args, 1, to_add,
661 DONT_ALLOW_DOUBLE_ELEMENTS);
631 if (maybe_object->IsFailure()) return maybe_object; 662 if (maybe_object->IsFailure()) return maybe_object;
632 663
633 if (new_length > elms->length()) { 664 if (new_length > elms->length()) {
634 // New backing storage is needed. 665 // New backing storage is needed.
635 int capacity = new_length + (new_length >> 1) + 16; 666 int capacity = new_length + (new_length >> 1) + 16;
636 Object* obj; 667 Object* obj;
637 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity); 668 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
638 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 669 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
639 } 670 }
640 FixedArray* new_elms = FixedArray::cast(obj); 671 FixedArray* new_elms = FixedArray::cast(obj);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 } 782 }
752 JSArray* result_array = JSArray::cast(result); 783 JSArray* result_array = JSArray::cast(result);
753 784
754 { MaybeObject* maybe_result = 785 { MaybeObject* maybe_result =
755 heap->AllocateUninitializedFixedArray(result_len); 786 heap->AllocateUninitializedFixedArray(result_len);
756 if (!maybe_result->ToObject(&result)) return maybe_result; 787 if (!maybe_result->ToObject(&result)) return maybe_result;
757 } 788 }
758 FixedArray* result_elms = FixedArray::cast(result); 789 FixedArray* result_elms = FixedArray::cast(result);
759 790
760 MaybeObject* maybe_object = 791 MaybeObject* maybe_object =
761 result_array->EnsureCanContainElements(result_elms); 792 result_array->EnsureCanContainElements(result_elms,
793 DONT_ALLOW_DOUBLE_ELEMENTS);
762 if (maybe_object->IsFailure()) return maybe_object; 794 if (maybe_object->IsFailure()) return maybe_object;
763 795
764 AssertNoAllocation no_gc; 796 AssertNoAllocation no_gc;
765 CopyElements(heap, &no_gc, result_elms, 0, elms, k, result_len); 797 CopyElements(heap, &no_gc, result_elms, 0, elms, k, result_len);
766 798
767 // Set elements. 799 // Set elements.
768 result_array->set_elements(result_elms); 800 result_array->set_elements(result_elms);
769 801
770 // Set the length. 802 // Set the length.
771 result_array->set_length(Smi::FromInt(result_len)); 803 result_array->set_length(Smi::FromInt(result_len));
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 heap->AllocateUninitializedFixedArray(result_len); 1047 heap->AllocateUninitializedFixedArray(result_len);
1016 if (!maybe_result->ToObject(&result)) return maybe_result; 1048 if (!maybe_result->ToObject(&result)) return maybe_result;
1017 } 1049 }
1018 FixedArray* result_elms = FixedArray::cast(result); 1050 FixedArray* result_elms = FixedArray::cast(result);
1019 1051
1020 // Ensure element type transitions happen before copying elements in. 1052 // Ensure element type transitions happen before copying elements in.
1021 if (result_array->HasFastSmiOnlyElements()) { 1053 if (result_array->HasFastSmiOnlyElements()) {
1022 for (int i = 0; i < n_arguments; i++) { 1054 for (int i = 0; i < n_arguments; i++) {
1023 JSArray* array = JSArray::cast(args[i]); 1055 JSArray* array = JSArray::cast(args[i]);
1024 if (!array->HasFastSmiOnlyElements()) { 1056 if (!array->HasFastSmiOnlyElements()) {
1025 result_array->EnsureCanContainNonSmiElements(); 1057 result_array->EnsureCanContainHeapObjectElements();
1026 break; 1058 break;
1027 } 1059 }
1028 } 1060 }
1029 } 1061 }
1030 1062
1031 // Copy data. 1063 // Copy data.
1032 AssertNoAllocation no_gc; 1064 AssertNoAllocation no_gc;
1033 int start_pos = 0; 1065 int start_pos = 0;
1034 for (int i = 0; i < n_arguments; i++) { 1066 for (int i = 0; i < n_arguments; i++) {
1035 JSArray* array = JSArray::cast(args[i]); 1067 JSArray* array = JSArray::cast(args[i]);
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
1785 return Handle<Code>(code_address); \ 1817 return Handle<Code>(code_address); \
1786 } 1818 }
1787 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) 1819 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
1788 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) 1820 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
1789 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) 1821 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
1790 #undef DEFINE_BUILTIN_ACCESSOR_C 1822 #undef DEFINE_BUILTIN_ACCESSOR_C
1791 #undef DEFINE_BUILTIN_ACCESSOR_A 1823 #undef DEFINE_BUILTIN_ACCESSOR_A
1792 1824
1793 1825
1794 } } // namespace v8::internal 1826 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698