We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d7e18d8 commit eee143eCopy full SHA for eee143e
mutexmap.go
@@ -24,6 +24,8 @@ type lockCtr struct {
24
waiters atomic.Int32 // Number of callers waiting to acquire the lock
25
}
26
27
+var lockCtrPool = sync.Pool{New: func() any { return new(lockCtr) }}
28
+
29
// Lock locks the mutex identified by key.
30
func (l *MutexMap[T]) Lock(key T) {
31
l.mu.Lock()
@@ -33,7 +35,7 @@ func (l *MutexMap[T]) Lock(key T) {
33
35
34
36
nameLock, exists := l.locks[key]
37
if !exists {
- nameLock = &lockCtr{}
38
+ nameLock = lockCtrPool.Get().(*lockCtr)
39
l.locks[key] = nameLock
40
41
@@ -62,6 +64,7 @@ func (l *MutexMap[T]) Unlock(key T) {
62
64
63
65
if nameLock.waiters.Load() <= 0 {
66
delete(l.locks, key)
67
+ defer lockCtrPool.Put(nameLock)
68
69
nameLock.Unlock()
70
0 commit comments