-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(query): use quickselect instead of sorting while pagination (#8995)
When we get pagination request, we still sort the entire structure and then return relevant results. This diff introduces mini select algorithm that will only sort till we get first k elements out of n. Both LDBC Benchmarks and MicroBenchmarks show that this is only useful until 50% ``` LDBC Queries Benchmarks Total elements : 300000 Current main: BenchmarkQueries/IC09/First_:20-8 1 36621508916 ns/op BenchmarkQueries/IC09/First_:304368-8 1 50905281926 ns/op BenchmarkQueries/IC09/First_:608716-8 1 63878833326 ns/op BenchmarkQueries/IC09/First_:913064-8 1 77455114015 ns/op BenchmarkQueries/IC09/First_:1217412-8 1 94143638593 ns/op BenchmarkQueries/IC09/First_:1521760-8 1 111483390712 ns/op BenchmarkQueries/IC09/First_:1826108-8 1 123910498449 ns/op ``` ``` QuickSelect: BenchmarkQueries/IC09/First_:20-8 1 32248068234 ns/op BenchmarkQueries/IC09/First_:304368-8 1 45877353021 ns/op BenchmarkQueries/IC09/First_:608716-8 1 60671818631 ns/op BenchmarkQueries/IC09/First_:913064-8 1 73447308743 ns/op BenchmarkQueries/IC09/First_:1217412-8 1 90371662972 ns/op BenchmarkQueries/IC09/First_:1521760-8 1 112880718231 ns/op BenchmarkQueries/IC09/First_:1826108-8 1 125487157700 ns/op ``` In microbenchmarks, first row is the current sort. Further rows are various different k values for the new algo ``` MicroBenchmarks Normal Sort vs QuickSelect BenchmarkSortQuickSort/Normal_Sort_Ratio_1000000_-8 1 1652489894 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_1-8 1 77946322 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_166667-8 1 327467804 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_333333-8 1 613629295 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_499999-8 1 899239726 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_666665-8 1 1231191998 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_833331-8 1 1565101591 ns/op BenchmarkSortQuickSort/QuickSort_Sort_Ratio_1000000_999997-8 1 1746166978 ns/op ```
- Loading branch information
1 parent
41a6895
commit 6357cb4
Showing
4 changed files
with
263 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2023 Dgraph Labs, Inc. and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package types | ||
|
||
// Below functions are taken from go's sort library zsortinterface.go | ||
// https://go.dev/src/sort/zsortinterface.go | ||
func insertionSort(data byValue, a, b int) { | ||
for i := a + 1; i < b; i++ { | ||
for j := i; j > a && data.Less(j, j-1); j-- { | ||
data.Swap(j, j-1) | ||
} | ||
} | ||
} | ||
|
||
func order2(data byValue, a, b int) (int, int) { | ||
if data.Less(b, a) { | ||
return b, a | ||
} | ||
return a, b | ||
} | ||
|
||
func median(data byValue, a, b, c int) int { | ||
a, b = order2(data, a, b) | ||
b, _ = order2(data, b, c) | ||
_, b = order2(data, a, b) | ||
return b | ||
} | ||
|
||
func medianAdjacent(data byValue, a int) int { | ||
return median(data, a-1, a, a+1) | ||
} | ||
|
||
// [shortestNinther,∞): uses the Tukey ninther method. | ||
func choosePivot(data byValue, a, b int) (pivot int) { | ||
const ( | ||
shortestNinther = 50 | ||
maxSwaps = 4 * 3 | ||
) | ||
|
||
l := b - a | ||
|
||
var ( | ||
i = a + l/4*1 | ||
j = a + l/4*2 | ||
k = a + l/4*3 | ||
) | ||
|
||
if l >= 8 { | ||
if l >= shortestNinther { | ||
// Tukey ninther method, the idea came from Rust's implementation. | ||
i = medianAdjacent(data, i) | ||
j = medianAdjacent(data, j) | ||
k = medianAdjacent(data, k) | ||
} | ||
// Find the median among i, j, k and stores it into j. | ||
j = median(data, i, j, k) | ||
} | ||
|
||
return j | ||
} | ||
|
||
func partition(data byValue, a, b, pivot int) int { | ||
partitionIndex := a | ||
data.Swap(pivot, b) | ||
for i := a; i < b; i++ { | ||
if data.Less(i, b) { | ||
data.Swap(i, partitionIndex) | ||
partitionIndex++ | ||
} | ||
} | ||
data.Swap(partitionIndex, b) | ||
return partitionIndex | ||
} | ||
|
||
func quickSelect(data byValue, low, high, k int) { | ||
var pivotIndex int | ||
|
||
for { | ||
if low >= high { | ||
return | ||
} else if high-low <= 8 { | ||
insertionSort(data, low, high+1) | ||
return | ||
} | ||
|
||
pivotIndex = choosePivot(data, low, high) | ||
pivotIndex = partition(data, low, high, pivotIndex) | ||
|
||
if k < pivotIndex { | ||
high = pivotIndex - 1 | ||
} else if k > pivotIndex { | ||
low = pivotIndex + 1 | ||
} else { | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters