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

Side by Side Diff: src/array.js

Issue 6004001: Implement an adaption of Dual-Pivot Quicksort for Array.prototype.sort.
Patch Set: Created 10 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
« no previous file with comments | « no previous file | no next file » | 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 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 if (order > 0) { 668 if (order > 0) {
669 a[j + 1] = tmp; 669 a[j + 1] = tmp;
670 } else { 670 } else {
671 break; 671 break;
672 } 672 }
673 } 673 }
674 a[j + 1] = element; 674 a[j + 1] = element;
675 } 675 }
676 } 676 }
677 677
678 function QuickSort(a, from, to) { 678 // Adaption of dual pivot quicksort.
679 // See http://gdtoolbox.com/DualPivotQuicksort.pdf
680 function DualPivotQuickSort(a, from, to, div) {
Karl Klose 2010/12/17 14:35:42 Why is the divider for the medians a variable? We
679 // Insertion sort is faster for short arrays. 681 // Insertion sort is faster for short arrays.
680 if (to - from <= 13) { 682 var length = to - from;
681 InsertionSort(a, from, to); 683 while (length > 8) {
682 return; 684 // Find pivots.
685 var pivotOffset = (length / div) | 0;
686 var pivot1 = a[from + pivotOffset];
687 var pivot2 = a[to - pivotOffset];
688 var cp = %_CallFunction(global_receiver, pivot1, pivot2, comparefn);
689 if (cp > 0) {
690 var tmp = pivot1;
691 pivot1 = pivot2;
692 pivot2 = tmp;
693 %_SwapElements(a, from, to - pivotOffset);
694 %_SwapElements(a, to - 1, from + pivotOffset);
695 } else {
696 %_SwapElements(a, from, from + pivotOffset);
697 %_SwapElements(a, to - 1, to - pivotOffset);
698 }
699 // Pivots are stored outside of the sorting area until we are done.
700 // pivot1 < pivot2.
701
702 var low_end = from + 1; // Upper bound of elements lower than pivots.
703 var high_start = to - 1; // Lower bound of elements greater than pivots.
704
705 // From low_end to i are elements between the pivots (inclusive).
706 // From i to high_start are elements that haven't been compared yet.
707 splitting: for (var i = low_end; i < high_start; ) {
708 var element = a[i];
709 if (%_CallFunction(global_receiver, element, pivot1, comparefn) < 0) {
710 // element < pivot1.
711 %_SwapElements(a, low_end, i);
712 i++;
713 low_end++;
714 } else if (
715 %_CallFunction(global_receiver, element, pivot2, comparefn) > 0) {
716 // element > pivot2.
717 do {
718 high_start--;
719 if (i == high_start) {
720 // All elements from i and up are greater than pivot2.
721 break splitting;
722 }
723 var top_elem = a[high_start];
724 var ct2 =
725 %_CallFunction(global_receiver, top_elem, pivot2, comparefn);
726 } while (ct2 > 0);
727 %_SwapElements(a, i, high_start); // Works even if i == high_start.
728 if (%_CallFunction(global_receiver, top_elem, pivot1, comparefn)
729 < 0) {
730 %_SwapElements(a, low_end, i);
731 low_end++;
732 }
733 i++;
734 } else {
735 // pivot1 <= element <= pivot2.
736 i++;
737 }
738 }
739 %_SwapElements(a, from, low_end - 1);
740 %_SwapElements(a, to - 1, high_start);
741 if (high_start - low_end < 13) div++;
742 DualPivotQuickSort(a, from, low_end - 1, div);
743 if (cp != 0) {
744 DualPivotQuickSort(a, low_end, high_start, div);
745 }
746 // Final tail-recursion is converted to loop.
747 from = high_start + 1;
748 length = to - from;
683 } 749 }
684 750 if (length > 1) InsertionSort(a, from, to);
685 // Find a pivot as the median of first, last and middle element.
686 var v0 = a[from];
687 var v1 = a[to - 1];
688 var middle_index = from + ((to - from) >> 1);
689 var v2 = a[middle_index];
690 var c01 = comparefn(v0, v1);
691 if (c01 > 0) {
692 // v1 < v0, so swap them.
693 var tmp = v0;
694 v0 = v1;
695 v1 = tmp;
696 } // v0 <= v1.
697 var c02 = comparefn(v0, v2);
698 if (c02 >= 0) {
699 // v2 <= v0 <= v1.
700 var tmp = v0;
701 v0 = v2;
702 v2 = v1;
703 v1 = tmp;
704 } else {
705 // v0 <= v1 && v0 < v2
706 var c12 = comparefn(v1, v2);
707 if (c12 > 0) {
708 // v0 <= v2 < v1
709 var tmp = v1;
710 v1 = v2;
711 v2 = tmp;
712 }
713 }
714 // v0 <= v1 <= v2
715 a[from] = v0;
716 a[to - 1] = v2;
717 var pivot = v1;
718 var low_end = from + 1; // Upper bound of the elements lower than pivot.
719 var high_start = to - 1; // Lower bound of the elements greater than pivot.
720 a[middle_index] = a[low_end];
721 a[low_end] = pivot;
722
723 // From low_end to i are elements equal to pivot.
724 // From i to high_start are elements that haven't been compared yet.
725 for (var i = low_end + 1; i < high_start; ) {
726 var element = a[i];
727 var order = %_CallFunction(global_receiver, element, pivot, comparefn);
728 if (order < 0) {
729 %_SwapElements(a, i, low_end);
730 i++;
731 low_end++;
732 } else if (order > 0) {
733 high_start--;
734 %_SwapElements(a, i, high_start);
735 } else { // order == 0
736 i++;
737 }
738 }
739 QuickSort(a, from, low_end);
740 QuickSort(a, high_start, to);
741 } 751 }
742 752
743 // Copy elements in the range 0..length from obj's prototype chain 753 // Copy elements in the range 0..length from obj's prototype chain
744 // to obj itself, if obj has holes. Return one more than the maximal index 754 // to obj itself, if obj has holes. Return one more than the maximal index
745 // of a prototype property. 755 // of a prototype property.
746 function CopyFromPrototype(obj, length) { 756 function CopyFromPrototype(obj, length) {
747 var max = 0; 757 var max = 0;
748 for (var proto = obj.__proto__; proto; proto = proto.__proto__) { 758 for (var proto = obj.__proto__; proto; proto = proto.__proto__) {
749 var indices = %GetArrayKeys(proto, length); 759 var indices = %GetArrayKeys(proto, length);
750 if (indices.length > 0) { 760 if (indices.length > 0) {
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 } 885 }
876 886
877 var num_non_undefined = %RemoveArrayHoles(this, length); 887 var num_non_undefined = %RemoveArrayHoles(this, length);
878 if (num_non_undefined == -1) { 888 if (num_non_undefined == -1) {
879 // There were indexed accessors in the array. Move array holes and 889 // There were indexed accessors in the array. Move array holes and
880 // undefineds to the end using a Javascript function that is safe 890 // undefineds to the end using a Javascript function that is safe
881 // in the presence of accessors. 891 // in the presence of accessors.
882 num_non_undefined = SafeRemoveArrayHoles(this); 892 num_non_undefined = SafeRemoveArrayHoles(this);
883 } 893 }
884 894
885 QuickSort(this, 0, num_non_undefined); 895 DualPivotQuickSort(this, 0, num_non_undefined, 3);
886 896
887 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) { 897 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) {
888 // For compatibility with JSC, we shadow any elements in the prototype 898 // For compatibility with JSC, we shadow any elements in the prototype
889 // chain that has become exposed by sort moving a hole to its position. 899 // chain that has become exposed by sort moving a hole to its position.
890 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element); 900 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element);
891 } 901 }
892 902
893 return this; 903 return this;
894 } 904 }
895 905
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
1204 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1214 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1205 "reduce", getFunction("reduce", ArrayReduce, 1), 1215 "reduce", getFunction("reduce", ArrayReduce, 1),
1206 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) 1216 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1)
1207 )); 1217 ));
1208 1218
1209 %FinishArrayPrototypeSetup($Array.prototype); 1219 %FinishArrayPrototypeSetup($Array.prototype);
1210 } 1220 }
1211 1221
1212 1222
1213 SetupArray(); 1223 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698