Bug 1259600 - Divert typed arrays passed to Array.sort to a typed array specific sort; r=till

--HG--
extra : rebase_source : 36465eb5e48d8e25250082e77b45a171dc321bfd
This commit is contained in:
Morgan Phillips 2016-03-28 13:02:17 -07:00
Родитель 6f83a1019a
Коммит f5282c201e
2 изменённых файлов: 30 добавлений и 9 удалений

Просмотреть файл

@ -232,6 +232,15 @@ function MergeSort(array, len, comparefn) {
var denseList = new List();
var denseLen = 0;
// Until recently typed arrays had no sort method. To work around that
// many users passed them to Array.prototype.sort. Now that we have a
// typed array specific sorting method it makes sense to divert to it
// when possible. Further, the MoveHoles utility function (used within
// MergeSort) is not currently compatible with typed arrays.
if (IsPossiblyWrappedTypedArray(array)) {
return TypedArraySort.call(array, comparefn);
}
for (var i = 0; i < len; i++) {
if (i in array)
denseList[denseLen++] = array[i];

Просмотреть файл

@ -10,20 +10,32 @@ function genRandomArray(size, seed) {
}
function SortTest(size, seed) {
let arrOne = genRandomArray(size, seed);
let arrTwo = Array.from(arrOne);
let arrThree = Array.from(arrOne);
let arrOne = genRandomArray(size, seed);
let arrTwo = Array.from(arrOne);
let arrThree = Array.from(arrOne);
let typedArrays = [
new Uint8Array(arrOne),
new Int32Array(arrOne),
new Float32Array(arrOne)
];
let sorted = Array.from((Int32Array.from(arrOne)).sort());
// Test numeric comparators against typed array sort.
assertDeepEq(Array.from((Int32Array.from(arrOne)).sort()),
arrTwo.sort((x, y) => (x - y)),
`The arr is not properly sorted! seed: ${SEED}`);
assertDeepEq(sorted, arrTwo.sort((x, y) => (x - y)),
`The array is not properly sorted! seed: ${SEED}`);
// Use multiplication to kill comparator optimization and trigger
// self-hosted sorting.
assertDeepEq(Array.from((Int32Array.from(arrOne)).sort()),
arrThree.sort((x, y) => (1*x - 1*y)),
`The arr is not properly sorted! seed: ${SEED}`);
assertDeepEq(sorted, arrThree.sort((x, y) => (1*x - 1*y)),
`The array is not properly sorted! seed: ${SEED}`);
// Ensure that typed arrays are also sorted property.
for (typedArr of typedArrays) {
let sortedTypedArray = Array.prototype.sort.call(typedArr, (x, y) => (1*x - 1*y))
assertDeepEq(sorted, Array.from(sortedTypedArray),
`The array is not properly sorted! seed: ${SEED}`);
}
}
SortTest(2048, SEED);