Skip to content

Commit

Permalink
remove dependency for github.com/dgraph-io/ristretto/z
Browse files Browse the repository at this point in the history
dgraph-io/ristretto use google/glog. glog rewrites flags on its own, which
causes unexpected behaviour.
  • Loading branch information
mattn authored and fiatjaf committed Dec 29, 2023
1 parent 781aecc commit 8b1839b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package nostr

import (
"reflect"
"sync"
"unsafe"

"github.com/dgraph-io/ristretto/z"
"golang.org/x/exp/constraints"
)

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
}
Expand Down

0 comments on commit 8b1839b

Please sign in to comment.