github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/cache/z/rtutil.go (about)

     1  // MIT License
     2  
     3  // Copyright (c) 2019 Ewan Chou
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package z
    24  
    25  import (
    26  	"unsafe"
    27  )
    28  
    29  // NanoTime returns the current time in nanoseconds from a monotonic clock.
    30  //go:linkname NanoTime runtime.nanotime
    31  func NanoTime() int64
    32  
    33  // CPUTicks is a faster alternative to NanoTime to measure time duration.
    34  //go:linkname CPUTicks runtime.cputicks
    35  func CPUTicks() int64
    36  
    37  type stringStruct struct {
    38  	str unsafe.Pointer
    39  	len int
    40  }
    41  
    42  //go:noescape
    43  //go:linkname memhash runtime.memhash
    44  func memhash(p unsafe.Pointer, h, s uintptr) uintptr
    45  
    46  // MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves
    47  // as aeshash if aes instruction is available).
    48  // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
    49  func MemHash(data []byte) uint64 {
    50  	ss := (*stringStruct)(unsafe.Pointer(&data))
    51  	return uint64(memhash(ss.str, 0, uintptr(ss.len)))
    52  }
    53  
    54  // MemHashString is the hash function used by go map, it utilizes available hardware instructions
    55  // (behaves as aeshash if aes instruction is available).
    56  // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
    57  func MemHashString(str string) uint64 {
    58  	ss := (*stringStruct)(unsafe.Pointer(&str))
    59  	return uint64(memhash(ss.str, 0, uintptr(ss.len)))
    60  }
    61  
    62  // FastRand is a fast thread local random function.
    63  //go:linkname FastRand runtime.fastrand
    64  func FastRand() uint32