Closed Bug 1763606 Opened 3 years ago Closed 2 years ago

Remove detach check in TypedArray.prototype.sort comparator

Categories

(Core :: JavaScript: Standard Library, task, P2)

task

Tracking

()

RESOLVED FIXED
103 Branch
Tracking Status
firefox103 --- fixed

People

(Reporter: anba, Assigned: anba)

References

(Blocks 1 open bug)

Details

(Keywords: perf-alert)

Attachments

(8 files)

Invoke std::sort without a comparator function for integral types including
uint8_clamped. For floating point types invoke std::sort with a comparator
function to ensure negative zero is sorted before positive zero. To further
improve the performance when sorting floating point numbers, compare numbers
using their unsigned sort value using the same approach as currently used in
RadixSort.

When the backing buffer is a SharedArrayBuffer, we need to copy the TypedArray
contents to avoid UB in std::sort when a different thread is modifying the
same SharedArrayBuffer memory.

The next two patches in this queue will provide specialisations for
TypedArraySort() to select faster sort algorithms than std::sort for
certain TypedArray element types.

Direct translation of the JS counting sort implementation to C++. The stack
allocated buffer has inline storage to store the complete uint8_t range. (This
function will also be used in the next part for uint16_t, but in that case the
complete buffer will be heap allocated.)

The C++ implementation is faster than the JS implementation and therefore we
can use a smaller limit when we fallback to std::sort for small arrays.

Depends on D143285

An almost direct translation of the JS implementation to C++. In contrast to
the JS implementation, we're using a smaller fallback to std::sort, because
the C++ implementation is faster, but we're also calling into std::sort for
large arrays to avoid allocating a large heap buffer.

As a further improvement, the radix sort implementation calls the counting sort
function for 16-bit types when the number of elements reaches 2^16. That way
we can further reduce the number of allocated heap memory.

The pre- and post-process steps from the JS implementation can be omitted,
because we can directly interpret the TypedArray memory contents as unsigned
integers in C++.

The concurrent write access handling in SortByColumn is necessary to avoid
an out-of-bounds access when a different thread writes to the same shared
memory location. This was also an issue in the JS implementation, except the
only visible outcome in that implementation was that the internal aux array
could leak to the user.

Depends on D143286

https://github.com/tc39/ecma262/pull/1585 changed Array.prototype.sort to
first collect all elements in a temporary list. And because TypedArray.prototype.sort
follows Array.prototype.sort, the same copying has to happen here.

Depends on D143288

Similar to part 5, concurrent write accesses shouldn't affect the sort algorithm.

Depends on D143289

Severity: -- → N/A
Priority: -- → P2
Pushed by andre.bargull@gmail.com: https://hg.mozilla.org/integration/autoland/rev/80dc9032e75f Part 1: Replace TypedArray QuickSort JS-function with C++. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/5d55dc85cdbe Part 2: Port TypedArray CountingSort function to C++. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/403a6eebf0a4 Part 3: Port TypedArray RadixSort function to C++. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/e477519e66c5 Part 4: Add a test case for concurrent modifications when sorting. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/1e0e956513ab Part 5: Sort TypedArray elements in a temporary buffer. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/993971e98bc6 Part 6: Copy shared memory for RadixSort. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/cac8af0a3542 Part 7: Remove detached ArrayBuffer checks from TypedArray.prototype.sort comparator. r=tcampbell

Meh, test262 upstream now calls detachArrayBuffer on an already detached array buffer, which isn't supported in the browser.js implementation.

Flags: needinfo?(andrebargull)
Pushed by andre.bargull@gmail.com: https://hg.mozilla.org/integration/autoland/rev/0214766ba072 Part 1: Replace TypedArray QuickSort JS-function with C++. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/b245af1692e1 Part 2: Port TypedArray CountingSort function to C++. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/2c8f470e2be8 Part 3: Port TypedArray RadixSort function to C++. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/b69a3b47f56d Part 4: Add a test case for concurrent modifications when sorting. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/d76311eb2427 Part 5: Sort TypedArray elements in a temporary buffer. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/0dc0d70ce93b Part 6: Copy shared memory for RadixSort. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/416ec1f14c2d Part 7: Remove detached ArrayBuffer checks from TypedArray.prototype.sort comparator. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/0b95325e435a Part 8: Support calling detachArrayBuffer on detached array buffer objects . r=tcampbell

== Change summary for alert #34391 (as of Fri, 10 Jun 2022 08:34:46 GMT) ==

Improvements:

Ratio Test Platform Options Absolute values (old vs new)
0.33% Base Content JS linux1804-64-shippable-qr fission 1,606,063.33 -> 1,600,736.00
0.32% Base Content JS windows10-64-2004-shippable-qr fission 1,608,013.33 -> 1,602,886.67
0.27% Base Content JS linux1804-64-shippable-qr fission 1,605,460.67 -> 1,601,052.00

For up to date results, see: https://treeherder.mozilla.org/perfherder/alerts?id=34391

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: