Skip to content
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

z: use zeebo/xxh3 instead of cespare/xxhash/v2 #437

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ go 1.22.11
toolchain go1.23.5

require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da
github.com/dustin/go-humanize v1.0.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.10.0
github.com/zeebo/xxh3 v1.0.3-0.20230502181907-7a4a65f1295e
golang.org/x/sys v0.30.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da h1:aIftn67I1fkbMa512G+w+Pxci9hJPB8oMnkcP3iZF38=
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/xxh3 v1.0.3-0.20230502181907-7a4a65f1295e h1:WwGTO/6KCjWBrM77YIl/+8uC1XYVsegq3qVSxOIfmRs=
github.com/zeebo/xxh3 v1.0.3-0.20230502181907-7a4a65f1295e/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
6 changes: 3 additions & 3 deletions z/z.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"sync"

"github.com/cespare/xxhash/v2"
"github.com/zeebo/xxh3"
)

type Key interface {
Expand All @@ -29,9 +29,9 @@ func KeyToHash[K Key](key K) (uint64, uint64) {
case uint64:
return k, 0
case string:
return MemHashString(k), xxhash.Sum64String(k)
return MemHashString(k), xxh3.HashString(k)
case []byte:
return MemHash(k), xxhash.Sum64(k)
return MemHash(k), xxh3.Hash(k)
case byte:
return uint64(k), 0
case int:
Expand Down
42 changes: 42 additions & 0 deletions z/z_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package z

import (
"fmt"
"math"
"testing"

Expand Down Expand Up @@ -94,3 +95,44 @@ func TestZeroOut(t *testing.T) {
ZeroOut(dst, 0, len(dst))
check(dst, 0x00)
}

func BenchmarkKeyToHashBytes(b *testing.B) {
bench := [][]byte{
[]byte("foo"),
[]byte("barbaz"),
[]byte("quxquuxquuz"),
[]byte("corgegraultgarplywaldo"),
}
benchmarkKeyToHash(b, bench)
}

func BenchmarkKeyToHashString(b *testing.B) {
bench := []string{
"foo",
"barbaz",
"quxquuxquuz",
"corgegraultgarplywaldo",
}
benchmarkKeyToHash(b, bench)
}

var key, conflict uint64

func benchmarkKeyToHash[T Key](b *testing.B, bench []T) {
for _, bb := range bench {
b.Run(fmt.Sprintf("%s", any(bb)), func(b *testing.B) {
switch bb := any(bb).(type) {
case []byte:
b.SetBytes(int64(len(bb)))
case string:
b.SetBytes(int64(len(bb)))
}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
key, conflict = KeyToHash(bb)
}
})
}
}