remove dependency for github.com/dgraph-io/ristretto/z

dgraph-io/ristretto use google/glog. glog rewrites flags on its own, which
causes unexpected behaviour.
This commit is contained in:
Yasuhiro Matsumoto
2023-12-29 20:54:22 +09:00
committed by fiatjaf_
parent 781aecc6ac
commit 8b1839bfc1

View File

@ -1,9 +1,10 @@
package nostr
import (
"reflect"
"sync"
"unsafe"
"github.com/dgraph-io/ristretto/z"
"golang.org/x/exp/constraints"
)
@ -11,8 +12,13 @@ const MAX_LOCKS = 50
var namedMutexPool = make([]sync.Mutex, MAX_LOCKS)
//go:noescape
//go:linkname memhash runtime.memhash
func memhash(p unsafe.Pointer, h, s uintptr) uintptr
func namedLock(name string) (unlock func()) {
idx := z.MemHashString(name) % MAX_LOCKS
ss := (*reflect.StringHeader)(unsafe.Pointer(&name))
idx := uint64(memhash(unsafe.Pointer(ss.Data), 0, uintptr(ss.Len))) % MAX_LOCKS
namedMutexPool[idx].Lock()
return namedMutexPool[idx].Unlock
}