From 8b1839bfc1ec16f33da4879e47187f19fa7201f9 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 29 Dec 2023 20:54:22 +0900 Subject: [PATCH] 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. --- helpers.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/helpers.go b/helpers.go index 019e6c4..5190d46 100644 --- a/helpers.go +++ b/helpers.go @@ -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 }