-
Notifications
You must be signed in to change notification settings - Fork 905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed the pivot selection in the group quick-sort #3800
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3800 +/- ##
=============================================
+ Coverage 56.41% 68.44% +12.03%
- Complexity 5531 6754 +1223
=============================================
Files 473 473
Lines 40934 40936 +2
Branches 5235 5235
=============================================
+ Hits 23092 28020 +4928
+ Misses 15744 10665 -5079
- Partials 2098 2251 +153
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Test errors:
|
@@ -61,11 +61,13 @@ private void quickSort(long[] array, int low, int high) { | |||
} | |||
|
|||
private int partition(long[] array, int low, int high) { | |||
int pivotIdx = high; | |||
int mid = low + (high - low) / 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the mid
may not be a multiple of groupSize
, that will introduce incorrect swap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch.. That's correct. I'll fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
* Fixed the pivot selection in the group quick-sort * Fixed formatting * Fixed alignment (cherry picked from commit 7f263fa)
* Fixed the pivot selection in the group quick-sort * Fixed formatting * Fixed alignment
Motivation
We have a custom implementation of quick-sort in the
WriteCache
that is used to sort an array of long that are grouped in 4: (ledgerId, entryId, offset, length).Because of the choice of the partition pivot, the sort results in very poor performance where the items are already pre-sorted, or mostly-sorted. Similarly, it happens when they are reverse-sorted. One of the side effects are also stack-overflow when the array is big and pre-sorted.
The problem is that we're always picking the last item in the segment as the pivot, leading to always the worst case partitioning (1, N-1) items.
The simplest solution is to pick the mid item as pivot, which works well for sorted, reverse and random scenarios. Later we can try more sophisticated approaches, such as the ones from
Arrays.sort()
.Benchmarks
I've compared the different cases with the baseline of
Arrays.sort()
:Before:
After:
The
preSortedGroupSort
goes from 6.2 to 1978.3 ops/s.