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

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: Review feedback 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
« no previous file with comments | « src/arm/builtins-arm.cc ('k') | src/elements.h » ('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 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 MaybeObject* maybe_object =
239 Object* obj; 239 array->EnsureCanContainElements(&args, 1, number_of_elements,
240 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(len->value()); 240 ALLOW_CONVERTED_DOUBLE_ELEMENTS);
241 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 241 if (maybe_object->IsFailure()) return maybe_object;
242
243 // Allocate an appropriately typed elements array.
244 MaybeObject* maybe_elms;
245 ElementsKind elements_kind = array->GetElementsKind();
246 if (elements_kind == FAST_DOUBLE_ELEMENTS) {
247 maybe_elms = heap->AllocateUninitializedFixedDoubleArray(
248 number_of_elements);
249 } else {
250 maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements);
251 }
252 FixedArrayBase* elms;
253 if (!maybe_elms->To<FixedArrayBase>(&elms)) return maybe_elms;
254
255 // Fill in the content
256 switch (array->GetElementsKind()) {
257 case FAST_SMI_ONLY_ELEMENTS: {
258 FixedArray* smi_elms = FixedArray::cast(elms);
259 for (int index = 0; index < number_of_elements; index++) {
260 smi_elms->set(index, args[index+1], SKIP_WRITE_BARRIER);
261 }
262 break;
263 }
264 case FAST_ELEMENTS: {
265 AssertNoAllocation no_gc;
266 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
267 FixedArray* object_elms = FixedArray::cast(elms);
268 for (int index = 0; index < number_of_elements; index++) {
269 object_elms->set(index, args[index+1], mode);
270 }
271 break;
272 }
273 case FAST_DOUBLE_ELEMENTS: {
274 FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms);
275 for (int index = 0; index < number_of_elements; index++) {
276 double_elms->set(index, args[index+1]->Number());
277 }
278 break;
279 }
280 default:
281 UNREACHABLE();
282 break;
242 } 283 }
243 284
244 // Set length and elements on the array. 285 array->set_elements(elms);
245 MaybeObject* maybe_object = 286 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; 287 return array;
261 } 288 }
262 289
263 290
264 MUST_USE_RESULT static MaybeObject* AllocateJSArray(Heap* heap) { 291 MUST_USE_RESULT static MaybeObject* AllocateJSArray(Heap* heap) {
265 JSFunction* array_function = 292 JSFunction* array_function =
266 heap->isolate()->context()->global_context()->array_function(); 293 heap->isolate()->context()->global_context()->array_function();
267 Object* result; 294 Object* result;
268 { MaybeObject* maybe_result = heap->AllocateJSObject(array_function); 295 { MaybeObject* maybe_result = heap->AllocateJSObject(array_function);
269 if (!maybe_result->ToObject(&result)) return maybe_result; 296 if (!maybe_result->ToObject(&result)) return maybe_result;
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 } 444 }
418 445
419 // Need to ensure that the arguments passed in args can be contained in 446 // Need to ensure that the arguments passed in args can be contained in
420 // the array. 447 // the array.
421 int args_length = args->length(); 448 int args_length = args->length();
422 if (first_added_arg >= args_length) return array->elements(); 449 if (first_added_arg >= args_length) return array->elements();
423 450
424 MaybeObject* maybe_array = array->EnsureCanContainElements( 451 MaybeObject* maybe_array = array->EnsureCanContainElements(
425 args, 452 args,
426 first_added_arg, 453 first_added_arg,
427 args_length - first_added_arg); 454 args_length - first_added_arg,
455 DONT_ALLOW_DOUBLE_ELEMENTS);
428 if (maybe_array->IsFailure()) return maybe_array; 456 if (maybe_array->IsFailure()) return maybe_array;
429 return array->elements(); 457 return array->elements();
430 } 458 }
431 459
432 460
433 static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap, 461 static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap,
434 JSArray* receiver) { 462 JSArray* receiver) {
435 if (!FLAG_clever_optimizations) return false; 463 if (!FLAG_clever_optimizations) return false;
436 Context* global_context = heap->isolate()->context()->global_context(); 464 Context* global_context = heap->isolate()->context()->global_context();
437 JSObject* array_proto = 465 JSObject* array_proto =
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 ASSERT(array->HasFastTypeElements()); 648 ASSERT(array->HasFastTypeElements());
621 649
622 int len = Smi::cast(array->length())->value(); 650 int len = Smi::cast(array->length())->value();
623 int to_add = args.length() - 1; 651 int to_add = args.length() - 1;
624 int new_length = len + to_add; 652 int new_length = len + to_add;
625 // Currently fixed arrays cannot grow too big, so 653 // Currently fixed arrays cannot grow too big, so
626 // we should never hit this case. 654 // we should never hit this case.
627 ASSERT(to_add <= (Smi::kMaxValue - len)); 655 ASSERT(to_add <= (Smi::kMaxValue - len));
628 656
629 MaybeObject* maybe_object = 657 MaybeObject* maybe_object =
630 array->EnsureCanContainElements(&args, 1, to_add); 658 array->EnsureCanContainElements(&args, 1, to_add,
659 DONT_ALLOW_DOUBLE_ELEMENTS);
631 if (maybe_object->IsFailure()) return maybe_object; 660 if (maybe_object->IsFailure()) return maybe_object;
632 661
633 if (new_length > elms->length()) { 662 if (new_length > elms->length()) {
634 // New backing storage is needed. 663 // New backing storage is needed.
635 int capacity = new_length + (new_length >> 1) + 16; 664 int capacity = new_length + (new_length >> 1) + 16;
636 Object* obj; 665 Object* obj;
637 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity); 666 { MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
638 if (!maybe_obj->ToObject(&obj)) return maybe_obj; 667 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
639 } 668 }
640 FixedArray* new_elms = FixedArray::cast(obj); 669 FixedArray* new_elms = FixedArray::cast(obj);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 } 780 }
752 JSArray* result_array = JSArray::cast(result); 781 JSArray* result_array = JSArray::cast(result);
753 782
754 { MaybeObject* maybe_result = 783 { MaybeObject* maybe_result =
755 heap->AllocateUninitializedFixedArray(result_len); 784 heap->AllocateUninitializedFixedArray(result_len);
756 if (!maybe_result->ToObject(&result)) return maybe_result; 785 if (!maybe_result->ToObject(&result)) return maybe_result;
757 } 786 }
758 FixedArray* result_elms = FixedArray::cast(result); 787 FixedArray* result_elms = FixedArray::cast(result);
759 788
760 MaybeObject* maybe_object = 789 MaybeObject* maybe_object =
761 result_array->EnsureCanContainElements(result_elms); 790 result_array->EnsureCanContainElements(result_elms,
791 DONT_ALLOW_DOUBLE_ELEMENTS);
762 if (maybe_object->IsFailure()) return maybe_object; 792 if (maybe_object->IsFailure()) return maybe_object;
763 793
764 AssertNoAllocation no_gc; 794 AssertNoAllocation no_gc;
765 CopyElements(heap, &no_gc, result_elms, 0, elms, k, result_len); 795 CopyElements(heap, &no_gc, result_elms, 0, elms, k, result_len);
766 796
767 // Set elements. 797 // Set elements.
768 result_array->set_elements(result_elms); 798 result_array->set_elements(result_elms);
769 799
770 // Set the length. 800 // Set the length.
771 result_array->set_length(Smi::FromInt(result_len)); 801 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); 1045 heap->AllocateUninitializedFixedArray(result_len);
1016 if (!maybe_result->ToObject(&result)) return maybe_result; 1046 if (!maybe_result->ToObject(&result)) return maybe_result;
1017 } 1047 }
1018 FixedArray* result_elms = FixedArray::cast(result); 1048 FixedArray* result_elms = FixedArray::cast(result);
1019 1049
1020 // Ensure element type transitions happen before copying elements in. 1050 // Ensure element type transitions happen before copying elements in.
1021 if (result_array->HasFastSmiOnlyElements()) { 1051 if (result_array->HasFastSmiOnlyElements()) {
1022 for (int i = 0; i < n_arguments; i++) { 1052 for (int i = 0; i < n_arguments; i++) {
1023 JSArray* array = JSArray::cast(args[i]); 1053 JSArray* array = JSArray::cast(args[i]);
1024 if (!array->HasFastSmiOnlyElements()) { 1054 if (!array->HasFastSmiOnlyElements()) {
1025 result_array->EnsureCanContainNonSmiElements(); 1055 result_array->EnsureCanContainHeapObjectElements();
1026 break; 1056 break;
1027 } 1057 }
1028 } 1058 }
1029 } 1059 }
1030 1060
1031 // Copy data. 1061 // Copy data.
1032 AssertNoAllocation no_gc; 1062 AssertNoAllocation no_gc;
1033 int start_pos = 0; 1063 int start_pos = 0;
1034 for (int i = 0; i < n_arguments; i++) { 1064 for (int i = 0; i < n_arguments; i++) {
1035 JSArray* array = JSArray::cast(args[i]); 1065 JSArray* array = JSArray::cast(args[i]);
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
1785 return Handle<Code>(code_address); \ 1815 return Handle<Code>(code_address); \
1786 } 1816 }
1787 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) 1817 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
1788 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) 1818 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
1789 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) 1819 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
1790 #undef DEFINE_BUILTIN_ACCESSOR_C 1820 #undef DEFINE_BUILTIN_ACCESSOR_C
1791 #undef DEFINE_BUILTIN_ACCESSOR_A 1821 #undef DEFINE_BUILTIN_ACCESSOR_A
1792 1822
1793 1823
1794 } } // namespace v8::internal 1824 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/arm/builtins-arm.cc ('k') | src/elements.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698