Horrible performance of Array.prototype.sort(cmp_fn) compared to other JS engines
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: qwertiest, Unassigned)
References
Details
Attachments
(1 file)
|
1.38 KB,
text/html
|
Details |
Comment 1•7 years ago
|
||
| Reporter | ||
Comment 2•7 years ago
|
||
| Reporter | ||
Comment 3•7 years ago
|
||
Comment 4•7 years ago
|
||
Comment 5•7 years ago
|
||
| Reporter | ||
Comment 6•7 years ago
|
||
Updated•6 years ago
|
Comment 7•6 years ago
|
||
Posted some patches which should improve this case:
- bug 1538690 should make
str_cmp_firstas fast asstr_cmp_first_charcode. - bug 1538692 and bug 1291463 should improve
str_cmp_full. In my dev-environment, from 13.8ms to 5.5ms; 4.5ms when--spectre-mitigations=offis used. (For comparison: JSC needs 3.2ms and V8 needs 3.3ms in the same environment.)
| Reporter | ||
Comment 8•6 years ago
|
||
That's awesome! Thanks for all the work!
It seems the only remaining problem is the sorting algorithm: merge-sort is not really suitable for expensive comparisons - my example used rather primitive comparison function (which happened to be expensive, because of other bugs that are fixed with the patches above), but it was more complex (and expensive) in the real scenario.
Comment 9•6 years ago
|
||
(In reply to TheQwertiest from comment #8)
That's awesome! Thanks for all the work!
\o/
It seems the only remaining problem is the sorting algorithm: merge-sort is not really suitable for expensive comparisons - my example used rather primitive comparison function (which happened to be expensive, because of other bugs that are fixed with the patches above), but it was more complex (and expensive) in the real scenario.
There have been attempts to use tim-sort (bug 715181), but in the end a simpler solution (= merge-sort) was chosen. Maybe it makes sense to revisit this decision. For the test case from comment #0, tim-sort seems to be better than merge-sort simply because the input contains many duplicates (~8M comparisons for tim-sort and ~12M comparisons in merge-sort). When fewer duplicates are present (comment #5), tim-sort will also call the comparator function about 12M times. (Which shouldn't be too surprising, because the worst case for both algorithms is O(n * log n). :-)
Comment 10•6 years ago
|
||
It seems the only remaining problem is the sorting algorithm: merge-sort is not really suitable for expensive comparisons - my example used rather primitive comparison function (which happened to be expensive, because of other bugs that are fixed with the patches above), but it was more complex (and expensive) in the real scenario.
In defense of merge sort, it is very efficient in terms of comparisons in the general case. Indeed, it approaches the theoretical limit for larger arrays (the best known sorting function in terms of comparison efficiency is a hybrid of Ford-Johnson sort and merge sort with some special case logic).
With that being said, I'm not denying that there are real world examples where you can do much better - e.g. arrays that start out nearly sorted (or sorted in reverse). Detecting such cases adds more overhead to the general case, of course, but I'm sure they're common enough that it's worth the cost. I just wanted to push back a little against the claim that "merge-sort is not really suitable for expensive comparisons" :)
| Reporter | ||
Comment 11•6 years ago
|
||
Fair enough, my statement was way too categorical =)
And I didn't test it in SM, so it might be not as useful in this case (e.g. bug 715181).
PS: a really good MIT-licensed tim-sort implementation is available here - https://github.com/tvanslyke/timsort-cpp
| Reporter | ||
Comment 12•5 years ago
|
||
It seems to be fixed in ESR68: it actually outperforms Chrome and IE Edge most of the time now!
| Reporter | ||
Comment 13•5 years ago
|
||
Updated JS fiddle with working console.log (the repro code is the same): https://jsfiddle.net/3k0jn6Ly/
Updated•3 years ago
|
Comment 14•1 year ago
|
||
Comment 15•1 year ago
|
||
Numbers from latest Nightly. And I have increased the number of iterations by 10X :
Nightly: https://share.firefox.dev/3TUlKA1 (2s)
Chrome: https://share.firefox.dev/3ShwYxd (1.4s)
Comment 16•1 year ago
|
||
Nightly: https://share.firefox.dev/3AmXv5K (2.1s)
Chrome: https://share.firefox.dev/4dkpNfJ (1.5s)
We appear to spend a lot of time in Baseline
Description
•