Skip to content

Commit 20a6abb

Browse files
Juneezeelemire
authored andcommitted
Use newer slices.Sort in pruneDuplicates
The new `slices.Sort` introduced in Go 1.21 [1] is faster than the old `sort.Slice` approach. func BenchmarkSort(b *testing.B) { array := []uint64{0, 42, 10, 8} b.Run("sort.Slice", func(b *testing.B) { for i := 0; i < b.N; i++ { sort.Slice(array, func(i, j int) bool { return array[i] < array[j] }) } }) b.Run("slices.Sort", func(b *testing.B) { for i := 0; i < b.N; i++ { slices.Sort(array) } }) } goos: linux goarch: amd64 pkg: github.com/FastFilter/xorfilter cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkSort/sort.Slice-16 4290086 284.0 ns/op 56 B/op 2 allocs/op BenchmarkSort/slices.Sort-16 62395050 19.53 ns/op 0 B/op 0 allocs/op PASS ok github.com/FastFilter/xorfilter 3.739s [1]: https://pkg.go.dev/slices#Sort Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 26c7ccf commit 20a6abb

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

xorfilter.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package xorfilter
33
import (
44
"errors"
55
"math"
6-
"sort"
6+
"slices"
77
)
88

99
func murmur64(h uint64) uint64 {
@@ -287,9 +287,7 @@ func Populate(keys []uint64) (*Xor8, error) {
287287
}
288288

289289
func pruneDuplicates(array []uint64) []uint64 {
290-
sort.Slice(array, func(i, j int) bool {
291-
return array[i] < array[j]
292-
})
290+
slices.Sort(array)
293291
pos := 0
294292
for i := 1; i < len(array); i++ {
295293
if array[i] != array[pos] {

0 commit comments

Comments
 (0)