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

Side by Side Diff: src/builtins.cc

Issue 954001: Add Array.concat builtin for the most common case. (Closed)
Patch Set: Adding counters Created 10 years, 9 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 | « src/builtins.h ('k') | src/runtime.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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 709 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 elms->set(k, args[3 + k - actual_start], mode); 720 elms->set(k, args[3 + k - actual_start], mode);
721 } 721 }
722 722
723 // Set the length. 723 // Set the length.
724 array->set_length(Smi::FromInt(new_length)); 724 array->set_length(Smi::FromInt(new_length));
725 725
726 return result_array; 726 return result_array;
727 } 727 }
728 728
729 729
730 BUILTIN(ArrayConcat) {
731 Counters::array_concat_builtin_total.Increment();
732 if (args.length() != 2) {
733 // Fast case only for concating two arrays.
734 return CallJsBuiltin("ArrayConcat", args);
735 }
736 Counters::array_concat_builtin_two_args.Increment();
737
738 Object* receiver_obj = *args.receiver();
739 FixedArray* receiver_elms = NULL;
740 Object* arg_obj = args[1];
741 FixedArray* arg_elms = NULL;
742 if (!IsJSArrayWithFastElements(receiver_obj, &receiver_elms)
Lasse Reichstein 2010/03/15 11:45:41 I personally don't like test functions with side e
antonm 2010/03/15 14:53:11 It used to save some cycles, at least in previous
743 || !IsJSArrayWithFastElements(arg_obj, &arg_elms)
744 || !ArrayPrototypeHasNoElements()) {
745 return CallJsBuiltin("ArrayConcat", args);
746 }
747
748 JSArray* receiver_array = JSArray::cast(receiver_obj);
749 ASSERT(receiver_array->HasFastElements());
750 JSArray* arg_array = JSArray::cast(arg_obj);
751 ASSERT(arg_array->HasFastElements());
752
753 int receiver_len = Smi::cast(receiver_array->length())->value();
754 int arg_len = Smi::cast(arg_array->length())->value();
755 ASSERT(receiver_len <= (Smi::kMaxValue - arg_len));
Lasse Reichstein 2010/03/15 11:45:41 Maybe comment that this assertion holds because bo
antonm 2010/03/15 14:53:11 Done
756
757 int result_len = receiver_len + arg_len;
758 if (result_len > FixedArray::kMaxSize) {
Lasse Reichstein 2010/03/15 11:45:41 FixedArray::kMaxLength (length is in elements, siz
antonm 2010/03/15 14:53:11 Oh yes, thanks a lot for spotting this.
759 return CallJsBuiltin("ArrayConcat", args);
760 }
761 if (result_len == 0) {
762 return AllocateEmptyJSArray();
763 }
764
765 // Allocate result.
766 Object* result = AllocateJSArray();
767 if (result->IsFailure()) return result;
768 JSArray* result_array = JSArray::cast(result);
769
770 result = Heap::AllocateUninitializedFixedArray(result_len);
771 if (result->IsFailure()) return result;
772 FixedArray* result_elms = FixedArray::cast(result);
773
774 // Copy data.
775 AssertNoAllocation no_gc;
776 CopyElements(&no_gc, result_elms, 0, receiver_elms, 0, receiver_len);
777 CopyElements(&no_gc, result_elms, receiver_len, arg_elms, 0, arg_len);
778
779 // Set the length and elements.
780 result_array->set_length(Smi::FromInt(result_len));
781 result_array->set_elements(result_elms);
782
783 return result_array;
784 }
785
786
730 // ----------------------------------------------------------------------------- 787 // -----------------------------------------------------------------------------
731 // 788 //
732 789
733 790
734 // Returns the holder JSObject if the function can legally be called 791 // Returns the holder JSObject if the function can legally be called
735 // with this receiver. Returns Heap::null_value() if the call is 792 // with this receiver. Returns Heap::null_value() if the call is
736 // illegal. Any arguments that don't fit the expected type is 793 // illegal. Any arguments that don't fit the expected type is
737 // overwritten with undefined. Arguments that do fit the expected 794 // overwritten with undefined. Arguments that do fit the expected
738 // type is overwritten with the object in the prototype chain that 795 // type is overwritten with the object in the prototype chain that
739 // actually has that type. 796 // actually has that type.
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
1370 if (entry->contains(pc)) { 1427 if (entry->contains(pc)) {
1371 return names_[i]; 1428 return names_[i];
1372 } 1429 }
1373 } 1430 }
1374 } 1431 }
1375 return NULL; 1432 return NULL;
1376 } 1433 }
1377 1434
1378 1435
1379 } } // namespace v8::internal 1436 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/builtins.h ('k') | src/runtime.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698