Skip to content

Commit

Permalink
feat: add xhash.MakeHasher()
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Mar 21, 2024
1 parent 3c018d4 commit b28dd4a
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 57 deletions.
55 changes: 0 additions & 55 deletions xhash/hasher_test.go

This file was deleted.

96 changes: 96 additions & 0 deletions xhash/hasher_xsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build go1.18
// +build go1.18

package xhash

/*
From https://github.com/puzpuzpuz/xsync
MIT License
Copyright (c) 2021 Andrey Pechkurov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import (
"reflect"
"unsafe"
)

// MakeHasher creates a fast hash function for the given comparable type.
// The only limitation is that the type should not contain interfaces inside
// based on runtime.typehash.
func MakeHasher[T comparable]() func(T) uint64 {
var zero T
seed := MakeSeed()
if reflect.TypeOf(&zero).Elem().Kind() == reflect.Interface {
return func(value T) uint64 {
iValue := any(value)
i := (*iface)(unsafe.Pointer(&iValue))
return runtimeTypehash64(i.typ, i.word, seed)
}
}
var iZero any = zero
i := (*iface)(unsafe.Pointer(&iZero))
return func(value T) uint64 {
return runtimeTypehash64(i.typ, unsafe.Pointer(&value), seed)
}
}

// MakeSeed creates a random seed.
func MakeSeed() uint64 {
var s1 uint32
for {
s1 = runtimeFastrand()
// We use seed 0 to indicate an uninitialized seed/hash,
// so keep trying until we get a non-zero seed.
if s1 != 0 {
break
}
}
s2 := runtimeFastrand()
return uint64(s1)<<32 | uint64(s2)
}

// how interface is represented in memory
type iface struct {
typ uintptr
word unsafe.Pointer
}

// same as runtimeTypehash, but always returns a uint64
// see: maphash.rthash function for details
func runtimeTypehash64(t uintptr, p unsafe.Pointer, seed uint64) uint64 {
if unsafe.Sizeof(uintptr(0)) == 8 {
return uint64(runtimeTypehash(t, p, uintptr(seed)))
}

lo := runtimeTypehash(t, p, uintptr(seed))
hi := runtimeTypehash(t, p, uintptr(seed>>32))
return uint64(hi)<<32 | uint64(lo)
}

//go:noescape
//go:linkname runtimeTypehash runtime.typehash
func runtimeTypehash(t uintptr, p unsafe.Pointer, h uintptr) uintptr

//go:noescape
//go:linkname runtimeFastrand runtime.fastrand
func runtimeFastrand() uint32
166 changes: 166 additions & 0 deletions xhash/hasher_xsync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//go:build go1.18
// +build go1.18

package xhash

import (
"fmt"
"strconv"
"strings"
"testing"
)

func TestMakeHashFunc(t *testing.T) {
type User struct {
Name string
City string
}

sHasher := MakeHasher[string]()
iHasher := MakeHasher[User]()

// Not that much to test TBH.
// check that hash is not always the same
for i := 0; ; i++ {
if sHasher("foo") != sHasher("bar") {
break
}
if i >= 100 {
t.Error("sHasher is always the same")
break
}
}

if sHasher("foo") != sHasher("foo") {
t.Error("sHasher is not deterministic")
}

if iHasher(User{Name: "Ivan", City: "Sofia"}) != iHasher(User{Name: "Ivan", City: "Sofia"}) {
t.Error("iHasher is not deterministic")
}
}

func BenchmarkMakeHashFunc(b *testing.B) {
type Point struct {
X, Y, Z int
}

type User struct {
ID int
FirstName string
LastName string
IsActive bool
City string
}

type PadInside struct {
A int
B byte
C int
}

type PadTrailing struct {
A int
B byte
}

doBenchmarkMakeHashFunc(b, int64(116))
doBenchmarkMakeHashFunc(b, int32(116))
doBenchmarkMakeHashFunc(b, 3.14)
doBenchmarkMakeHashFunc(b, "test key test key test key test key test key test key test key test key test key ")
doBenchmarkMakeHashFunc(b, Point{1, 2, 3})
doBenchmarkMakeHashFunc(b, User{ID: 1, FirstName: "Ivan", LastName: "Ivanov", IsActive: true, City: "Sofia"})
doBenchmarkMakeHashFunc(b, PadInside{})
doBenchmarkMakeHashFunc(b, PadTrailing{})
doBenchmarkMakeHashFunc(b, [1024]byte{})
doBenchmarkMakeHashFunc(b, [128]Point{})
doBenchmarkMakeHashFunc(b, [128]User{})
doBenchmarkMakeHashFunc(b, [128]PadInside{})
doBenchmarkMakeHashFunc(b, [128]PadTrailing{})
}

func doBenchmarkMakeHashFunc[T comparable](b *testing.B, val T) {
hash := MakeHasher[T]()
b.Run(fmt.Sprintf("%T", val), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = hash(val)
}
})
}

func TestCollision_MakeHasher(t *testing.T) {
n := 20_000_000
sHasher := MakeHasher[string]()
iHasher := MakeHasher[int]()
ms := make(map[uint64]string)
mi := make(map[uint64]int)
for i := 0; i < n; i++ {
s := strconv.Itoa(i)
hs := sHasher(s)
hi := iHasher(i)
if v, ok := ms[hs]; ok {
t.Fatalf("Collision: %s(%d) == %s(%d)", v, sHasher(v), s, sHasher(s))
}
if v, ok := mi[hi]; ok {
t.Fatalf("Collision: %d(%d) == %d(%d)", v, iHasher(v), i, iHasher(i))
}
ms[hs] = s
mi[hi] = i
}

hi := iHasher(7)
if _, ok := mi[hi]; !ok {
t.Fatalf("The number 7 should exist")
}
if len(ms) != len(mi) || len(ms) != n {
t.Fatalf("Hash count: %d, %d, %d", len(ms), len(mi), n)
}
}

func BenchmarkHasher_MakeHasher(b *testing.B) {
sHasher := MakeHasher[string]()
iHasher := MakeHasher[int]()
b.ReportAllocs()
b.ResetTimer()
b.Run("string", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sHasher(strconv.Itoa(i))
}
})
b.Run("int", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = iHasher(i)
}
})
}

func BenchmarkHasher_Parallel_MakeHasher(b *testing.B) {
sHasher := MakeHasher[string]()
s := strings.Repeat(testString, 10)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = sHasher(s)
}
})
}

// go test -run=^$ -benchmem -count=2 -bench=BenchmarkHasher
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils/xhash
// cpu: AMD Ryzen 7 5700G with Radeon Graphics
// BenchmarkHasher_MakeHasher/string-16 48853290 24.60 ns/op 7 B/op 0 allocs/op
// BenchmarkHasher_MakeHasher/string-16 50446735 26.41 ns/op 7 B/op 0 allocs/op
// BenchmarkHasher_MakeHasher/int-16 314223793 3.722 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_MakeHasher/int-16 327831469 3.681 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_Parallel_MakeHasher-16 759237190 1.611 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_Parallel_MakeHasher-16 800960254 1.598 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_GenHasher64/string-16 43828345 26.33 ns/op 7 B/op 0 allocs/op
// BenchmarkHasher_GenHasher64/string-16 43891936 26.14 ns/op 7 B/op 0 allocs/op
// BenchmarkHasher_GenHasher64/int-16 470061992 2.594 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_GenHasher64/int-16 459792141 2.596 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_Parallel_GenHasher64-16 333951942 3.606 ns/op 0 B/op 0 allocs/op
// BenchmarkHasher_Parallel_GenHasher64-16 321419834 3.627 ns/op 0 B/op 0 allocs/op
29 changes: 27 additions & 2 deletions xhash/hasher.go → xhash/hasher_xxhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@

package xhash

/*
From https://github.com/cespare/xxhash
Copyright (c) 2016 Caleb Spare
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import (
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -40,8 +65,8 @@ type Hashable interface {
~float32 | ~float64 | ~string | ~complex64 | ~complex128
}

// GenHasher64 返回哈希函数
// Ref: smallnest/safemap, alphadose/haxmap, cornelk/hashmap
// GenHasher64 按数据类型生成哈希函数
// Ref: cespare/xxhash, smallnest/safemap, alphadose/haxmap, cornelk/hashmap
func GenHasher64[K comparable]() func(K) uint64 {
hasher := GenHasher[K]()
return func(k K) uint64 {
Expand Down

0 comments on commit b28dd4a

Please sign in to comment.