github.com/oarkflow/sio@v0.0.6/internal/maps/maphash/hash.go (about)

     1  package maphash
     2  
     3  import "unsafe"
     4  
     5  // Hasher hashes values of type K.
     6  // Uses runtime AES-based hashing.
     7  type Hasher[K comparable] struct {
     8  	hash hashfn
     9  	seed uintptr
    10  }
    11  
    12  // NewHasher creates a new Hasher[K] with a random seed.
    13  func NewHasher[K comparable]() Hasher[K] {
    14  	return Hasher[K]{
    15  		hash: getRuntimeHasher[K](),
    16  		seed: newHashSeed(),
    17  	}
    18  }
    19  
    20  // NewSeed returns a copy of |h| with a new hash seed.
    21  func NewSeed[K comparable](h Hasher[K]) Hasher[K] {
    22  	return Hasher[K]{
    23  		hash: h.hash,
    24  		seed: newHashSeed(),
    25  	}
    26  }
    27  
    28  // Hash hashes |key|.
    29  func (h Hasher[K]) Hash(key K) uint64 {
    30  	return uint64(h.Hash2(key))
    31  }
    32  
    33  // Hash2 hashes |key| as more flexible uintptr.
    34  func (h Hasher[K]) Hash2(key K) uintptr {
    35  	// promise to the compiler that pointer
    36  	// |p| does not escape the stack.
    37  	p := noescape(unsafe.Pointer(&key))
    38  	return h.hash(p, h.seed)
    39  }