github.com/vicanso/lru-ttl@v1.5.1/memhash.go (about) 1 // copy from https://github.com/dgraph-io/ristretto/blob/master/z/rtutil.go 2 3 package lruttl 4 5 import ( 6 "unsafe" 7 ) 8 9 // NanoTime returns the current time in nanoseconds from a monotonic clock. 10 //go:linkname NanoTime runtime.nanotime 11 func NanoTime() int64 12 13 // CPUTicks is a faster alternative to NanoTime to measure time duration. 14 //go:linkname CPUTicks runtime.cputicks 15 func CPUTicks() int64 16 17 type stringStruct struct { 18 str unsafe.Pointer 19 len int 20 } 21 22 //go:noescape 23 //go:linkname memhash runtime.memhash 24 func memhash(p unsafe.Pointer, h, s uintptr) uintptr 25 26 // MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves 27 // as aeshash if aes instruction is available). 28 // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash. 29 func MemHash(data []byte) uint64 { 30 ss := (*stringStruct)(unsafe.Pointer(&data)) 31 return uint64(memhash(ss.str, 0, uintptr(ss.len))) 32 } 33 34 // MemHashString is the hash function used by go map, it utilizes available hardware instructions 35 // (behaves as aeshash if aes instruction is available). 36 // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash. 37 func MemHashString(str string) uint64 { 38 ss := (*stringStruct)(unsafe.Pointer(&str)) 39 return uint64(memhash(ss.str, 0, uintptr(ss.len))) 40 } 41 42 // FastRand is a fast thread local random function. 43 //go:linkname FastRand runtime.fastrand 44 func FastRand() uint32 45 46 //go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers 47 func memclrNoHeapPointers(p unsafe.Pointer, n uintptr) 48 49 func Memclr(b []byte) { 50 if len(b) == 0 { 51 return 52 } 53 p := unsafe.Pointer(&b[0]) 54 memclrNoHeapPointers(p, uintptr(len(b))) 55 }