Skip to content

[API Proposal]: Improve default sort algorithm #68325

Open

Description

@coderb

Background and motivation

please consider enhancing the default sort algorithm and following the lead below.

go, rust, and c++ have converged on pdqsort as the default sorting algorithm based on a lot of benchmarking.

https://news.ycombinator.com/item?id=31106157
golang/go@72e77a7
https://github.com/orlp/pdqsort

API Proposal

see above

API Usage

see above

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Apr 21, 2022
  2. ghost added
    untriaged
    on Apr 21, 2022
  3. kasperk81 commented on Apr 21, 2022

    @kasperk81
    Contributor

    and c++ have converged on pdqsort

    has std::sort() switched from introsort to pdqsort? in which version of STL?

  4. coderb commented on Apr 21, 2022

    @coderb
    Author

    @kasperk81 sorry... maybe i misread the golang commit message. there is an implementation for rust/c++, i guess it's not standard. my bad.

  5. added
    enhancement
    and removed
    api-suggestion
    on Apr 21, 2022
  6. teo-tsirpanis commented on Apr 21, 2022

    @teo-tsirpanis
    Contributor

    Hello @coderb, can you provide some benchmarks that exhibit that pdqsort is faster than the existing sorting algorithm in .NET? It would help to move the suggestion forward.

  7. coderb commented on Apr 21, 2022

    @coderb
    Author

    @teo-tsirpanis please see the link to the golang commit. the go team (google) has devoted a large amount of engineering resources to this and has done a far better job benchmarking than i would be able to. the thing to remember is that quicksort (and introsort) have very poor performance for certain types of inputs which pdqsort overcomes. see golang/go@72e77a7

  8. hez2010 commented on Apr 22, 2022

    @hez2010
    Contributor

    I would also like to recommend .NET to change its sort implementation to pdqsort. Generally pdqsort performs way better than introsort (std::sort):

    image

  9. hez2010 commented on Apr 22, 2022

    @hez2010
    Contributor

    I ported pdqsort(https://github.com/orlp/pdqsort) to C# and this is the benchmark result:

    Method Mean Error StdDev
    ArraySort_Random 98.50 ms 1.503 ms 1.406 ms
    PdqSort_Random 113.15 ms 0.892 ms 0.834 ms
    ArraySort_Sequence 75.258 ms 0.9969 ms 0.8837 ms
    PdqSort_Sequence 11.148 ms 0.0920 ms 0.0768 ms
    ArraySort_ReverseSequence 44.793 ms 0.5330 ms 0.4725 ms
    PdqSort_ReverseSequence 7.532 ms 0.1113 ms 0.0987 ms
    ArraySort_Peek 196.205 ms 1.9521 ms 1.6301 ms
    PdqSort_Peek 109.999 ms 0.6644 ms 0.6215 ms
    ArraySort_ReversePeek 195.456 ms 0.9002 ms 0.7028 ms
    PdqSort_ReversePeek 109.775 ms 0.9096 ms 0.8064 ms

    code: https://gist.github.com/hez2010/6b52929ee1755788c34818972c46aefb

    /cc:@stephentoub

  10. tfenise commented on Apr 22, 2022

    @tfenise
    Contributor

    For log2 there is System.Numerics.BitOperations.Log2(uint).
    PdqSort_Random looks worse than ArraySort_Random, possibly because PdqSort_Random calls Compare by interface while ArraySort_Random calls Compare through a delegate.

  11. tannergooding commented on Apr 22, 2022

    @tannergooding
    Member

    It would be very important to ensure that this is tested with multiple sizes of data to show the differences between "small scale" vs "large scale".

    Sorting 1 million elements faster is nice, but if that's not the "common" case and the common is more like 10-1000, then we'll end up regressing real world users.

    Testing random, already sorted, mostly sorted, etc is also important, but it looks like you've got that bit covered already.

  12. tannergooding commented on Apr 22, 2022

    @tannergooding
    Member

    Also worth noting that the benchmarks are including the overhead of allocating and copying the data to a large array. Ideally that would be moved out to some "per iteration setup" so that the measurement is only over the actual Sort function.

  13. coderb commented on Apr 22, 2022

    @coderb
    Author

    @hez2010 you may want to test on objects in addition to primitives.

  14. hez2010 commented on Apr 23, 2022

    @hez2010
    Contributor

    @tannergooding I modified my code and rerun it with .NET 7 (PGO+QJFL+OSR enabled), PdqSort out performs in every case:

    Method size Mean Error StdDev Median Ratio
    ArraySort_AllEqual 10 1,463.4 ns 32.23 ns 58.12 ns 1,500.0 ns 1
    PdqSort_AllEqual 10 834.1 ns 20.59 ns 78.67 ns 800.0 ns 0.57
    ArraySort_RandomNear 10 1,357.4 ns 33.12 ns 138.41 ns 1,300.0 ns 1
    PdqSort_RandomNear 10 933.5 ns 22.61 ns 91.20 ns 900.0 ns 0.69
    ArraySort_Random 10 1,465.6 ns 32.81 ns 76.05 ns 1,500.0 ns 1
    PdqSort_Random 10 954.5 ns 22.99 ns 67.42 ns 900.0 ns 0.65
    ArraySort_Sequence 10 1,279.4 ns 25.42 ns 41.04 ns 1,300.0 ns 1
    PdqSort_Sequence 10 829.0 ns 22.52 ns 80.72 ns 800.0 ns 0.65
    ArraySort_ReverseSequence 10 1,715.1 ns 38.26 ns 149.87 ns 1,700.0 ns 1
    PdqSort_ReverseSequence 10 931.0 ns 24.61 ns 78.48 ns 900.0 ns 0.54
    ArraySort_Peek 10 1,458.0 ns 32.85 ns 79.35 ns 1,500.0 ns 1
    PdqSort_Peek 10 981.1 ns 23.38 ns 39.71 ns 1,000.0 ns 0.67
    ArraySort_ReversePeek 10 1,429.2 ns 32.56 ns 93.94 ns 1,400.0 ns 1
    PdqSort_ReversePeek 10 937.8 ns 22.62 ns 78.10 ns 900.0 ns 0.66
    ArraySort_AllEqual 100 2,789.1 ns 61.69 ns 131.48 ns 2,800.0 ns 1
    PdqSort_AllEqual 100 1,585.6 ns 35.64 ns 111.07 ns 1,600.0 ns 0.57
    ArraySort_RandomNear 100 5,160.0 ns 88.53 ns 82.81 ns 5,200.0 ns 1
    PdqSort_RandomNear 100 3,783.1 ns 76.42 ns 169.33 ns 3,700.0 ns 0.73
    ArraySort_Random 100 5,508.3 ns 101.57 ns 79.30 ns 5,500.0 ns 1
    PdqSort_Random 100 4,245.5 ns 88.54 ns 166.29 ns 4,200.0 ns 0.77
    ArraySort_Sequence 100 3,443.8 ns 73.83 ns 145.73 ns 3,400.0 ns 1
    PdqSort_Sequence 100 1,253.0 ns 29.03 ns 68.43 ns 1,300.0 ns 0.36
    ArraySort_ReverseSequence 100 4,566.7 ns 90.67 ns 97.01 ns 4,600.0 ns 1
    PdqSort_ReverseSequence 100 1,621.5 ns 35.87 ns 83.84 ns 1,600.0 ns 0.36
    ArraySort_Peek 100 6,846.7 ns 139.19 ns 130.20 ns 6,900.0 ns 1
    PdqSort_Peek 100 4,163.0 ns 86.57 ns 121.36 ns 4,200.0 ns 0.61
    ArraySort_ReversePeek 100 7,307.7 ns 103.27 ns 86.23 ns 7,300.0 ns 1
    PdqSort_ReversePeek 100 4,050.0 ns 82.14 ns 94.59 ns 4,000.0 ns 0.55
    ArraySort_AllEqual 1000 22,796.2 ns 357.98 ns 298.93 ns 22,850.0 ns 1
    PdqSort_AllEqual 1000 2,746.8 ns 59.74 ns 91.23 ns 2,750.0 ns 0.12
    ArraySort_RandomNear 1000 60,628.6 ns 829.46 ns 735.30 ns 60,450.0 ns 1
    PdqSort_RandomNear 1000 38,600.0 ns 684.45 ns 571.55 ns 38,500.0 ns 0.64
    ArraySort_Random 1000 65,485.7 ns 829.97 ns 735.74 ns 65,600.0 ns 1
    PdqSort_Random 1000 40,953.3 ns 565.50 ns 528.97 ns 40,800.0 ns 0.63
    ArraySort_Sequence 1000 29,757.1 ns 130.62 ns 115.79 ns 29,700.0 ns 1
    PdqSort_Sequence 1000 3,255.2 ns 67.21 ns 98.51 ns 3,300.0 ns 0.11
    ArraySort_ReverseSequence 1000 47,523.1 ns 827.02 ns 690.60 ns 47,400.0 ns 1
    PdqSort_ReverseSequence 1000 4,520.0 ns 92.14 ns 86.19 ns 4,500.0 ns 0.1
    ArraySort_Peek 1000 102,807.7 ns 1,237.26 ns 1,033.17 ns 102,800.0 ns 1
    PdqSort_Peek 1000 32,327.3 ns 638.66 ns 784.33 ns 32,300.0 ns 0.31
    ArraySort_ReversePeek 1000 101,040.0 ns 399.60 ns 373.78 ns 101,200.0 ns 1
    PdqSort_ReversePeek 1000 32,715.4 ns 535.21 ns 446.93 ns 32,800.0 ns 0.32
    ArraySort_AllEqual 10000 331,531.5 ns 6,628.85 ns 25,170.65 ns 321,700.0 ns 1
    PdqSort_AllEqual 10000 17,445.8 ns 339.40 ns 441.32 ns 17,550.0 ns 0.05
    ArraySort_RandomNear 10000 796,320.7 ns 15,901.33 ns 44,849.99 ns 782,350.0 ns 1
    PdqSort_RandomNear 10000 495,809.5 ns 9,681.32 ns 11,524.93 ns 494,300.0 ns 0.62
    ArraySort_Random 10000 802,008.3 ns 10,294.17 ns 8,037.01 ns 798,100.0 ns 1
    PdqSort_Random 10000 523,595.2 ns 10,427.60 ns 12,413.32 ns 519,900.0 ns 0.65
    ArraySort_Sequence 10000 382,656.2 ns 6,258.24 ns 6,146.43 ns 386,250.0 ns 1
    PdqSort_Sequence 10000 22,395.7 ns 442.49 ns 559.61 ns 22,400.0 ns 0.06
    ArraySort_ReverseSequence 10000 671,491.3 ns 12,820.65 ns 16,214.02 ns 672,900.0 ns 1
    PdqSort_ReverseSequence 10000 32,521.1 ns 654.01 ns 726.93 ns 32,700.0 ns 0.05
    ArraySort_Peek 10000 1,653,403.6 ns 32,723.50 ns 69,736.44 ns 1,627,400.0 ns 1
    PdqSort_Peek 10000 332,523.5 ns 5,159.49 ns 5,298.41 ns 334,300.0 ns 0.2
    ArraySort_ReversePeek 10000 1,550,048.3 ns 29,011.67 ns 42,524.93 ns 1,534,900.0 ns 1
    PdqSort_ReversePeek 10000 334,310.3 ns 6,278.59 ns 9,203.08 ns 332,300.0 ns 0.22
    ArraySort_AllEqual 100000 3,829.3 us 71.13 us 55.54 us 3,830.1 us 1
    PdqSort_AllEqual 100000 302.7 us 5.87 us 15.47 us 297.7 us 0.08
    ArraySort_RandomNear 100000 9,242.4 us 135.47 us 126.71 us 9,264.8 us 1
    PdqSort_RandomNear 100000 6,155.5 us 79.26 us 70.26 us 6,160.2 us 0.67
    ArraySort_Random 100000 10,288.1 us 125.94 us 105.16 us 10,306.8 us 1
    PdqSort_Random 100000 6,454.4 us 121.92 us 140.41 us 6,423.8 us 0.63
    ArraySort_Sequence 100000 5,080.0 us 94.07 us 83.39 us 5,072.1 us 1
    PdqSort_Sequence 100000 421.7 us 5.25 us 4.66 us 422.9 us 0.08
    ArraySort_ReverseSequence 100000 9,445.3 us 136.01 us 127.22 us 9,454.7 us 1
    PdqSort_ReverseSequence 100000 323.9 us 6.12 us 19.18 us 311.6 us 0.03
    ArraySort_Peek 100000 21,027.5 us 410.74 us 384.21 us 21,061.0 us 1
    PdqSort_Peek 100000 3,614.3 us 70.26 us 91.36 us 3,590.5 us 0.17
    ArraySort_ReversePeek 100000 21,216.7 us 194.14 us 181.60 us 21,215.7 us 1
    PdqSort_ReversePeek 100000 3,690.6 us 55.66 us 46.48 us 3,681.0 us 0.17
    ArraySort_AllEqual 1000000 48,913.4 us 286.55 us 223.72 us 48,967.5 us 1
    PdqSort_AllEqual 1000000 5,175.4 us 103.50 us 1,085.55 us 5,314.0 us 0.11
    ArraySort_RandomNear 1000000 112,398.0 us 848.60 us 708.62 us 112,358.6 us 1
    PdqSort_RandomNear 1000000 74,713.3 us 1,222.39 us 1,143.43 us 74,416.4 us 0.66
    ArraySort_Random 1000000 116,767.4 us 1,071.41 us 1,002.20 us 116,511.4 us 1
    PdqSort_Random 1000000 77,163.1 us 541.12 us 451.86 us 77,059.6 us 0.66
    ArraySort_Sequence 1000000 66,112.9 us 1,227.28 us 1,147.99 us 65,879.6 us 1
    PdqSort_Sequence 1000000 5,845.8 us 116.89 us 1,016.63 us 6,014.9 us 0.09
    ArraySort_ReverseSequence 1000000 105,905.3 us 1,072.63 us 1,003.34 us 105,948.4 us 1
    PdqSort_ReverseSequence 1000000 7,452.4 us 149.00 us 1,133.04 us 7,546.2 us 0.07
    ArraySort_Peek 1000000 262,358.3 us 1,217.47 us 1,079.26 us 262,020.5 us 1
    PdqSort_Peek 1000000 42,478.6 us 657.92 us 549.40 us 42,695.4 us 0.16
    ArraySort_ReversePeek 1000000 261,885.1 us 1,831.46 us 1,623.54 us 262,027.0 us 1
    PdqSort_ReversePeek 1000000 42,101.3 us 499.68 us 390.11 us 42,174.1 us 0.16

    See code here: https://gist.github.com/hez2010/6b52929ee1755788c34818972c46aefb

  15. tfenise commented on Apr 23, 2022

    @tfenise
    Contributor

    The use of UnguardedAccess in UnguardedInsertionSort(and possibly elsewhere) could cause buffer overflow if the comparer is bugged.

    UnguardedAccess could use Unsafe.Add(ref MemoryMarshal.GetReference(span), index), as Unsafe.Add(ref span[0], index) has an extra range check that span.Length must be positive.

    UnguardedSlice should use MemoryMarshal.CreateSpan(ref UnguardedAccess(span, begin), end - begin). Unsafe.AsPointer could stop GC tracking.

  16. ghost added
    in-pr
    on Apr 23, 2022
  17. ghost removed
    in-pr
    on Jun 24, 2022
  18. jeffhandley commented on Jun 24, 2022

    @jeffhandley
    Member

    The discussion in #68436 revealed that we need to define acceptance criteria for a change to our default sort algorithm.

  19. added this to the Future milestone on Jun 24, 2022
  20. ghost removed
    untriaged
    on Jun 24, 2022
  21. blackholeearth commented on Nov 4, 2023

    @blackholeearth

    https://github.com/scandum/fluxsort

    • stable sort algo.
    • improved version of pdqsort.
    • can evade worstcase scenarios of pdq.
      • Most of the time flux is 2x better.
      • in rare cases flux is %10 better than pdq.
      • in another rare case its 10x faster.

    İmage is from the repo.

    Benchmark image

    Just to let u know,
    Maybe, in couple of years another sort algos might popup , which might outperform it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimeenhancementtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions