SorterTemplate.merge is slow

Details

Type: Improvement

Status:Resolved

Priority: Minor

Resolution:
Fixed

Affects Version/s:
None

Fix Version/s:
None

Component/s:
None

Labels:

None

Lucene Fields:

New

Description

SorterTemplate.mergeSort/timSort can be very slow. For example, I ran a quick benchmark that sorts an Integer[] array of 50M elements, and mergeSort was almost 4x slower than quickSort (~12.8s for quickSort, ~46.5s for mergeSort). This is even worse when the cost of a swap is higher (e.g. parallel arrays).

This is due to SorterTemplate.merge. I first feared that this method might not be linear, but it is, so the slowness is due to the fact that this method needs to swap lots of values in order not to require extra memory. Could we make it faster?

For reference, I hacked a SorterTemplate instance to use the usual merge routine (that requires n/2 elements in memory), and it was much faster: ~17s on average, so there is room for improvement.

The main problem is: The universal possinility to use SorterTemplate is the fact that you only have to define the swap operation and nothing more. If you need an additional array to buffer the merging, the API gets much more complex, as you would need to define the whole merge process for your "custom buffer implementation", the easy "compare-and-swap-with-pivot" would be gone.

SorterTemplate is something in the middle: Its not using the fastest merge, but it is in-place and has a very limited set of operations to implement. If you want a faster algorithm, you have to move a way from in-place. All the fast merge/timsort/quicksort algoth are working with arrays only and SorterTemplate wants to work around this in a simple generic way.

Uwe Schindler
added a comment - 21/Mar/13 13:35 The main problem is: The universal possinility to use SorterTemplate is the fact that you only have to define the swap operation and nothing more. If you need an additional array to buffer the merging, the API gets much more complex, as you would need to define the whole merge process for your "custom buffer implementation", the easy "compare-and-swap-with-pivot" would be gone.
SorterTemplate is something in the middle: Its not using the fastest merge, but it is in-place and has a very limited set of operations to implement. If you want a faster algorithm, you have to move a way from in-place. All the fast merge/timsort/quicksort algoth are working with arrays only and SorterTemplate wants to work around this in a simple generic way.

In that case, could we make SorterTemplate.merge overridable (protected) so that custom templates can use extra memory to merge? The attached patch modifies ArrayUtil to show how it could be used to implement a faster merge, which makes mergeSort more than 2x faster (~21s on average on my 50M array) although it only requires 1% of additional memory. What do you think?

Adrien Grand
added a comment - 21/Mar/13 14:30 If you want a faster algorithm, you have to move away from in-place.
In that case, could we make SorterTemplate.merge overridable (protected) so that custom templates can use extra memory to merge? The attached patch modifies ArrayUtil to show how it could be used to implement a faster merge, which makes mergeSort more than 2x faster (~21s on average on my 50M array) although it only requires 1% of additional memory. What do you think?

Making the merge method protected would offer the possibility to speed up merging. If you dont implement a custom merge, the code works only with swap and/or setPivot.

Why do we need a seperate getSorter() in ArrayUtils? Could the default SorterTemplate used also for quicksort not simply provide the faster merge? Or did you implement it separate to not allocate the extra array, if only quicksort is called?

Otherwise I am fine with doing it that way, if we do not enforce users to implement the merge code.

Uwe Schindler
added a comment - 21/Mar/13 15:01 - edited Making the merge method protected would offer the possibility to speed up merging. If you dont implement a custom merge, the code works only with swap and/or setPivot.
Why do we need a seperate getSorter() in ArrayUtils? Could the default SorterTemplate used also for quicksort not simply provide the faster merge? Or did you implement it separate to not allocate the extra array, if only quicksort is called?
Otherwise I am fine with doing it that way, if we do not enforce users to implement the merge code.

Adrien Grand
added a comment - 21/Mar/13 15:13 Otherwise I am fine with doing it that way, if we do not enforce users to implement the merge code.
OK. I'll update the patch to port the same behavior to CollectionUtil.

Adrien Grand
added a comment - 21/Mar/13 16:01 Patch that makes SorterTemplate.merge protected and makes ArrayUtil and CollectionUtil use specialized SorterTemplate instances that use up to 1% extra memory for faster merge-based sorts.
I'll open a separate issue to use the same optimizations for the sorter API's timsorts.