github.com/fufuok/utils@v1.0.10/xhash/hasher_xsync.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 package xhash 5 6 /* 7 From https://github.com/puzpuzpuz/xsync 8 9 MIT License 10 11 Copyright (c) 2021 Andrey Pechkurov 12 13 Permission is hereby granted, free of charge, to any person obtaining a copy 14 of this software and associated documentation files (the "Software"), to deal 15 in the Software without restriction, including without limitation the rights 16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 copies of the Software, and to permit persons to whom the Software is 18 furnished to do so, subject to the following conditions: 19 20 The above copyright notice and this permission notice shall be included in all 21 copies or substantial portions of the Software. 22 23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 29 SOFTWARE. 30 */ 31 32 import ( 33 "reflect" 34 "unsafe" 35 ) 36 37 // MakeHasher creates a fast hash function for the given comparable type. 38 // The only limitation is that the type should not contain interfaces inside 39 // based on runtime.typehash. 40 func MakeHasher[T comparable]() func(T) uint64 { 41 var zero T 42 seed := MakeSeed() 43 if reflect.TypeOf(&zero).Elem().Kind() == reflect.Interface { 44 return func(value T) uint64 { 45 iValue := any(value) 46 i := (*iface)(unsafe.Pointer(&iValue)) 47 return runtimeTypehash64(i.typ, i.word, seed) 48 } 49 } 50 var iZero any = zero 51 i := (*iface)(unsafe.Pointer(&iZero)) 52 return func(value T) uint64 { 53 return runtimeTypehash64(i.typ, unsafe.Pointer(&value), seed) 54 } 55 } 56 57 // MakeSeed creates a random seed. 58 func MakeSeed() uint64 { 59 var s1 uint32 60 for { 61 s1 = runtimeFastrand() 62 // We use seed 0 to indicate an uninitialized seed/hash, 63 // so keep trying until we get a non-zero seed. 64 if s1 != 0 { 65 break 66 } 67 } 68 s2 := runtimeFastrand() 69 return uint64(s1)<<32 | uint64(s2) 70 } 71 72 // how interface is represented in memory 73 type iface struct { 74 typ uintptr 75 word unsafe.Pointer 76 } 77 78 // same as runtimeTypehash, but always returns a uint64 79 // see: maphash.rthash function for details 80 func runtimeTypehash64(t uintptr, p unsafe.Pointer, seed uint64) uint64 { 81 if unsafe.Sizeof(uintptr(0)) == 8 { 82 return uint64(runtimeTypehash(t, p, uintptr(seed))) 83 } 84 85 lo := runtimeTypehash(t, p, uintptr(seed)) 86 hi := runtimeTypehash(t, p, uintptr(seed>>32)) 87 return uint64(hi)<<32 | uint64(lo) 88 } 89 90 //go:noescape 91 //go:linkname runtimeTypehash runtime.typehash 92 func runtimeTypehash(t uintptr, p unsafe.Pointer, h uintptr) uintptr 93 94 //go:noescape 95 //go:linkname runtimeFastrand runtime.fastrand 96 func runtimeFastrand() uint32