Array.sort makes duplicate comparisons
Reported by
david.w...@gmail.com,
Dec 1
|
|||||||
Issue descriptionUserAgent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0 Steps to reproduce the problem: 1. Create a custom sort function to count comparisons 2. Call [0, 2, 1].sort(countFunc) 3. Notice that 4 comparisons were used, when there are only 3 pairwise combinations of elements. What is the expected behavior? Array.sort should not make the same comparison twice. Permutations of length 3 should be sorted in 2 or 3 steps. What went wrong? Timsort makes the same comparison multiple times. The attached `index.html` demonstrates the problem for arrays of length 3. This issue can lead to thousands of extra comparisons on large arrays. I filed a parallel bug with Python[1], which is the model for Chromium's Torque implementation[2]. The `sort-fix.diff` attachment is my proposed fix for Python. It may be worth following the other bug to see if people reject or improve it. [1] https://bugs.python.org/issue35369 [2] https://v8.dev/blog/array-sort Did this work before? No Chrome version: 70.0.3538.77 (Official Build) Built on Ubuntu , running on Ubuntu 18.04 (64-bit) Channel: stable OS Version: Ubuntu 18.04 Flash Version:
,
Dec 1
,
Dec 3
Thanks for filing the issue! As per comment# 1, considering this issue as Feature request and marking it as Untriage. Thanks!
,
Dec 3
,
Dec 17
,
Dec 17
Handing this off to szuend@. Note that there's no bug here, this is just how TimSort works. Tim Peters' reply in the linked python bug sounds like no changes to TimSort will be forthcoming.
,
Jan 16
(6 days ago)
Thanks for filing! After looking into the diff and the analysis of Tim Peters, I have to agree with him. IMO, a minor improvement that probably won't be measurable in most cases, does not warrant the increased complexity, plus check for correctness.
,
Jan 16
(6 days ago)
The NextAction date has arrived: 2019-01-16 |
|||||||
►
Sign in to add a comment |
|||||||
Comment 1 by woxxom@gmail.com
, Dec 1