Skip to content

Conversation

@Juneezee
Copy link
Contributor

The new slices.Sort function introduced in Go 1.21 1 is faster than the older sort.Slice approach. The documentation for sort.Slice also recommends using slices.Sort in most cases 2.

Benchmark:

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)
		}
	})
}

Result:

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

Footnotes

  1. https://pkg.go.dev/slices#Sort

  2. https://pkg.go.dev/sort#Slice

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 <engzerjun@gmail.com>
@lemire
Copy link
Member

lemire commented Dec 26, 2024

Wow

@Juneezee
Copy link
Contributor Author

Juneezee commented Jan 1, 2025

@lemire Happy New Year! Does this PR look good to you, or is there anything else you would like me to address?

@lemire
Copy link
Member

lemire commented Jan 1, 2025

It will be part of the next release.

@lemire lemire merged commit 20a6abb into FastFilter:master Jan 1, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants