[PATCH] Enhancement proposal for TimSort and ComparableTimSort

Сергей Цыпанов sergei.tsypanov at yandex.ru
Fri Sep 15 09:49:01 UTC 2017

Current implementations of TimSort and ComparableTimSort both have fast path for the case when the size of array = 1 i.e. such array is considered to be sorted.

My proposal is to add similar fast path for the case when the size of the array = 2. In this case it's enough to check if the first item is greater than the second one and swap them.
Otherwise array is considered to be sorted.

I have attached the patch to this mail.

As for performance I have measured array sort with size = 2 using JDK-provided and patched implementations of TimSort and ComparableTimSort both execution time and throughput in two environments:

1)  Intel Core i5-4690 3,50 GHz
2)  Intel Core i3-3220 3,30 GHz

On both environments I have Java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Measurement results are


Benchmark                                                        Mode  Cnt   Score     Error   Units

SortingBenchmark.measureConventionalSort  avgt    500  17,692 ± 0,040  ns/op
SortingBenchmark.measureEnhancedSort       avgt    500  13,206 ± 0,025  ns/op

SortingBenchmark.measureConventionalSort  thrpt    500  56,962 ± 0,051  ops/us
SortingBenchmark.measureEnhancedSort       thrpt    500  72,278 ± 0,724  ops/us


Benchmark                                                        Mode  Cnt   Score    Error  Units

SortingBenchmark.measureConventionalSort  avgt    500  26,173 ± 0,251  ns/op
SortingBenchmark.measureEnhancedSort       avgt    500  18,191 ± 0,106  ns/op

SortingBenchmark.measureConventionalSort  thrpt    500  36,987 ± 0,437  ops/us
SortingBenchmark.measureEnhancedSort       thrpt    500  53,499 ± 0,456  ops/us

So in both cases patched implementation demonstrates better throughput and execution time for the case array.lenght = 2.

Sergei Tsypanov
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-diff
Size: 1817 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20170915/5386bc4a/patch.txt>

More information about the core-libs-dev mailing list