Closed Bug 1291463 Opened 9 years ago Closed 6 years ago

Array.prototype.sort performance improvements

Categories

(Core :: JavaScript: Standard Library, defect, P3)

defect

Tracking

()

RESOLVED FIXED
mozilla70
Tracking Status
firefox51 --- affected
firefox70 --- fixed

People

(Reporter: anba, Assigned: anba)

References

(Blocks 1 open bug)

Details

Attachments

(3 files, 3 obsolete files)

Attached file Benchmark script (obsolete) —
Possible performance improvements for Array.prototype.sort: - Move "ArraySort" to vm/CommonPropertyNames.h instead of calling Atomize repeatedly - Directly return in ArraySort() if the length less than 2 - Handle partially sorted sublists in Merge - Reduce stores in Merge() by using a single additional buffer - Use std_Array instead of List (also avoids a JIT bug [1] when there's only one element) - Handle packed arrays when storing from the array to the denseList - Use InsertionSort() instead of Merge() for windowSize=1 and windowSize=2 [1]: --- function t(array) { function c(a, b) { var d = a - b; return d; } var t = 0; for (var i = 0; i < 100000; ++i) { var arrays = []; for (var j = 0; j < 10; ++j) arrays[j] = array(); var d = dateNow(); for (var j = 0; j < 10; ++j) arrays[j].sort(c); t += (dateNow() - d); } print(t); } // Slow t(() => [1,,,,,,,,,,]); // Faster // t(() => [,,,,,,,,,,]); // t(() => [1,2,,,,,,,,,]); ---
Attached patch Array.sort-performance.patch (obsolete) — Splinter Review
Patch which implements the suggestions mentioned in comment #0
Attached image Benchmark results (obsolete) —
Results when running the attached benchmark, including comparisons to JavaScriptCore and V8.
SpiderMonkey version: m-c ffac2798999c JSC version: trunk rev204022 V8 version: master 8552e6822317c48638eb25d71c902cffebee21f1
I have difficulty thinking of pretty much any situation where it makes sense to have Atomize(cx, "...", ...) in regularly executed, non-test code. Things like ArraySort and similar should always be in CommonPropertyNames.h, IMO.
Attached file Benchmark script
Attachment #8777104 - Attachment is obsolete: true
Attached image Benchmark results
Attachment #8777107 - Attachment is obsolete: true
See Also: → 1331475
Priority: -- → P3

Also change the temporary buffer to a normal Array to ensure the Merge helper
is called with the same object class.

Attachment #8777105 - Attachment is obsolete: true

With bug 1538692 also applied, this improves µ-benchmark from bug 1502882 when the str_cmp_full comparator function is used from 6.2ms (when only bug 1538692 is applied) to 5.5ms for me.

Blocks: 1502882
Assignee: nobody → andrebargull
Status: NEW → ASSIGNED

Anba, do you want to land this?

Flags: needinfo?(andrebargull)

Pushed by nbeleuzu@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/a869bc3e91a4
Apply TypedArray sort optimisations to normal Array sort. r=jorendorff

Keywords: checkin-needed
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla70
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: