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

Side by Side Diff: src/array.js

Issue 21507: Experimental: port bleeding_edge r1276 (a grab bag of optimizations)... (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/toiger/
Patch Set: Created 11 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | src/codegen-ia32.cc » ('j') | src/codegen-ia32.cc » ('J')
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 602 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } 613 }
614 614
615 615
616 function ArraySort(comparefn) { 616 function ArraySort(comparefn) {
617 // In-place QuickSort algorithm. 617 // In-place QuickSort algorithm.
618 // For short (length <= 22) arrays, insertion sort is used for efficiency. 618 // For short (length <= 22) arrays, insertion sort is used for efficiency.
619 619
620 var custom_compare = IS_FUNCTION(comparefn); 620 var custom_compare = IS_FUNCTION(comparefn);
621 621
622 function Compare(x,y) { 622 function Compare(x,y) {
623 // Assume the comparefn, if any, is a consistent comparison function.
624 // If it isn't, we are allowed arbitrary behavior by ECMA 15.4.4.11.
625 if (x === y) return 0;
623 if (custom_compare) { 626 if (custom_compare) {
627 // Don't call directly to avoid exposing the builtin's global object.
624 return comparefn.call(null, x, y); 628 return comparefn.call(null, x, y);
625 } 629 }
626 if (%_IsSmi(x) && %_IsSmi(y)) { 630 if (%_IsSmi(x) && %_IsSmi(y)) {
627 return %SmiLexicographicCompare(x, y); 631 return %SmiLexicographicCompare(x, y);
628 } 632 }
629 x = ToString(x); 633 x = ToString(x);
630 y = ToString(y); 634 y = ToString(y);
631 if (x == y) return 0; 635 if (x == y) return 0;
632 else return x < y ? -1 : 1; 636 else return x < y ? -1 : 1;
633 }; 637 };
634 638
635 function InsertionSort(a, from, to) { 639 function InsertionSort(a, from, to) {
636 for (var i = from + 1; i < to; i++) { 640 for (var i = from + 1; i < to; i++) {
637 var element = a[i]; 641 var element = a[i];
642 // Pre-convert the element to a string for comparison if we know
643 // it will happen on each compare anyway.
644 var key =
645 (custom_compare || %_IsSmi(element)) ? element : ToString(element);
638 // place element in a[from..i[ 646 // place element in a[from..i[
639 // binary search 647 // binary search
640 var min = from; 648 var min = from;
641 var max = i; 649 var max = i;
642 // The search interval is a[min..max[ 650 // The search interval is a[min..max[
643 while (min < max) { 651 while (min < max) {
644 var mid = min + ((max - min) >> 1); 652 var mid = min + ((max - min) >> 1);
645 var order = Compare(a[mid], element); 653 var order = Compare(a[mid], key);
646 if (order == 0) { 654 if (order == 0) {
647 min = max = mid; 655 min = max = mid;
648 break; 656 break;
649 } 657 }
650 if (order < 0) { 658 if (order < 0) {
651 min = mid + 1; 659 min = mid + 1;
652 } else { 660 } else {
653 max = mid; 661 max = mid;
654 } 662 }
655 } 663 }
656 // place element at position min==max. 664 // place element at position min==max.
657 for (var j = i; j > min; j--) { 665 for (var j = i; j > min; j--) {
658 a[j] = a[j - 1]; 666 a[j] = a[j - 1];
659 } 667 }
660 a[min] = element; 668 a[min] = element;
661 } 669 }
662 } 670 }
663 671
664 function QuickSort(a, from, to) { 672 function QuickSort(a, from, to) {
665 // Insertion sort is faster for short arrays. 673 // Insertion sort is faster for short arrays.
666 if (to - from <= 22) { 674 if (to - from <= 22) {
667 InsertionSort(a, from, to); 675 InsertionSort(a, from, to);
668 return; 676 return;
669 } 677 }
670 var pivot_index = $floor($random() * (to - from)) + from; 678 var pivot_index = $floor($random() * (to - from)) + from;
671 var pivot = a[pivot_index]; 679 var pivot = a[pivot_index];
680 // Pre-convert the element to a string for comparison if we know
681 // it will happen on each compare anyway.
682 var pivot_key =
683 (custom_compare || %_IsSmi(pivot)) ? pivot : ToString(pivot);
672 // Issue 95: Keep the pivot element out of the comparisons to avoid 684 // Issue 95: Keep the pivot element out of the comparisons to avoid
673 // infinite recursion if comparefn(pivot, pivot) != 0. 685 // infinite recursion if comparefn(pivot, pivot) != 0.
674 a[pivot_index] = a[to - 1]; 686 var low_end = from; // Upper bound of the elements lower than pivot.
675 a[to - 1] = pivot; 687 var high_start = to; // Lower bound of the elements greater than pivot.
676 var low_end = from; // Upper bound of the elements lower than pivot. 688 var eq_start = to - 1; // Lower bound of elements equal to pivot.
677 var high_start = to - 1; // Lower bound of the elements greater than pivot. 689 a[pivot_index] = a[eq_start];
678 for (var i = from; i < high_start; ) { 690 a[eq_start] = pivot;
679 var element = a[i]; 691 // From eq_start to high_start are elements equal to the pivot
680 var order = Compare(element, pivot); 692 // (including the pivot).
693 // From low_end to eq_start are elements that have not been compared yet.
694 while (low_end < eq_start) {
695 var element = a[low_end];
696 var order = Compare(element, pivot_key);
681 if (order < 0) { 697 if (order < 0) {
682 a[i] = a[low_end];
683 a[low_end] = element;
684 low_end++; 698 low_end++;
685 i++;
686 } else if (order > 0) { 699 } else if (order > 0) {
700 eq_start--;
687 high_start--; 701 high_start--;
688 a[i] = a[high_start]; 702 a[low_end] = a[eq_start];
703 a[eq_start] = a[high_start];
689 a[high_start] = element; 704 a[high_start] = element;
690 } else { // order == 0 705 } else { // order == 0
691 i++; 706 eq_start--;
707 a[low_end] = a[eq_start];
708 a[eq_start] = element;
692 } 709 }
693 } 710 }
694 // Restore the pivot element to its rightful place.
695 a[to - 1] = a[high_start];
696 a[high_start] = pivot;
697 high_start++;
698 QuickSort(a, from, low_end); 711 QuickSort(a, from, low_end);
699 QuickSort(a, high_start, to); 712 QuickSort(a, high_start, to);
700 } 713 }
701 714
702 var old_length = ToUint32(this.length); 715 var old_length = ToUint32(this.length);
716 if (old_length < 2) return this;
703 717
704 %RemoveArrayHoles(this); 718 %RemoveArrayHoles(this);
705 719
706 var length = ToUint32(this.length); 720 var length = ToUint32(this.length);
707 721
708 // Move undefined elements to the end of the array. 722 // Move undefined elements to the end of the array.
709 for (var i = 0; i < length; ) { 723 for (var i = 0; i < length; ) {
710 if (IS_UNDEFINED(this[i])) { 724 if (IS_UNDEFINED(this[i])) {
711 length--; 725 length--;
712 this[i] = this[length]; 726 this[i] = this[length];
(...skipping 21 matching lines...) Expand all
734 // preserving the semantics, since the calls to the receiver function can add 748 // preserving the semantics, since the calls to the receiver function can add
735 // or delete elements from the array. 749 // or delete elements from the array.
736 function ArrayFilter(f, receiver) { 750 function ArrayFilter(f, receiver) {
737 if (!IS_FUNCTION(f)) { 751 if (!IS_FUNCTION(f)) {
738 throw MakeTypeError('called_non_callable', [ f ]); 752 throw MakeTypeError('called_non_callable', [ f ]);
739 } 753 }
740 // Pull out the length so that modifications to the length in the 754 // Pull out the length so that modifications to the length in the
741 // loop will not affect the looping. 755 // loop will not affect the looping.
742 var length = this.length; 756 var length = this.length;
743 var result = []; 757 var result = [];
758 var result_length = 0;
744 for (var i = 0; i < length; i++) { 759 for (var i = 0; i < length; i++) {
745 var current = this[i]; 760 var current = this[i];
746 if (!IS_UNDEFINED(current) || i in this) { 761 if (!IS_UNDEFINED(current) || i in this) {
747 if (f.call(receiver, current, i, this)) result.push(current); 762 if (f.call(receiver, current, i, this)) result[result_length++] = current;
748 } 763 }
749 } 764 }
750 return result; 765 return result;
751 } 766 }
752 767
753 768
754 function ArrayForEach(f, receiver) { 769 function ArrayForEach(f, receiver) {
755 if (!IS_FUNCTION(f)) { 770 if (!IS_FUNCTION(f)) {
756 throw MakeTypeError('called_non_callable', [ f ]); 771 throw MakeTypeError('called_non_callable', [ f ]);
757 } 772 }
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
917 ArrayEvery: 1, 932 ArrayEvery: 1,
918 ArrayMap: 1, 933 ArrayMap: 1,
919 ArrayIndexOf: 1, 934 ArrayIndexOf: 1,
920 ArrayLastIndexOf: 1, 935 ArrayLastIndexOf: 1,
921 ArrayPush: 1 936 ArrayPush: 1
922 }); 937 });
923 } 938 }
924 939
925 940
926 SetupArray(); 941 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | src/codegen-ia32.cc » ('j') | src/codegen-ia32.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698