From dc73c298d81c7a62066cc98f10a6d1e45df0daae Mon Sep 17 00:00:00 2001 From: snower Date: Tue, 21 May 2024 11:43:38 +0800 Subject: [PATCH] Optimize the initial size of LockManager Locked queue and wait queue memory --- server/lock.go | 4 ++-- server/lock_test.go | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/server/lock.go b/server/lock.go index 7c34262..bafc854 100755 --- a/server/lock.go +++ b/server/lock.go @@ -53,7 +53,7 @@ type LockManagerLockQueue struct { } func NewLockManagerLockQueue() *LockManagerLockQueue { - return &LockManagerLockQueue{NewLockQueue(4, 16, 4), make(map[[16]byte]*Lock)} + return &LockManagerLockQueue{NewLockQueue(4, 8, 4), make(map[[16]byte]*Lock)} } func (self *LockManagerLockQueue) Push(lock *Lock) { @@ -78,7 +78,7 @@ type LockManagerWaitQueue struct { } func NewLockManagerWaitQueue() *LockManagerWaitQueue { - return &LockManagerWaitQueue{make([]*Lock, 0, 16), 0, nil} + return &LockManagerWaitQueue{make([]*Lock, 0, 8), 0, nil} } func (self *LockManagerWaitQueue) Push(lock *Lock) { diff --git a/server/lock_test.go b/server/lock_test.go index 5da7612..cd89c6e 100644 --- a/server/lock_test.go +++ b/server/lock_test.go @@ -63,17 +63,18 @@ func TestLockManagerRingQueue(t *testing.T) { for i := 0; i < 1000000; i++ { queue.Push(lock) } - if len(queue.queue) != 1000000 || cap(queue.queue) != 1135616 { + if len(queue.queue) != 1000000 || cap(queue.queue) < 1000000 { t.Errorf("LockManagerRingQueue Push Size fail") return } + capSize := cap(queue.queue) for i := 0; i < 1000000; i++ { if queue.Pop() != lock { t.Errorf("LockManagerRingQueue Pop fail") return } } - if len(queue.queue) != 0 || cap(queue.queue) != 1135616 || queue.index != 0 { + if len(queue.queue) != 0 || cap(queue.queue) != capSize || queue.index != 0 { t.Errorf("LockManagerRingQueue Pop Size fail") return } @@ -89,45 +90,45 @@ func TestLockManagerWaitQueue(t *testing.T) { return } - for i := 0; i < 16; i++ { + for i := 0; i < 8; i++ { queue.Push(lock) - if len(queue.fastQueue) != i+1 || cap(queue.fastQueue) != 16 { + if len(queue.fastQueue) != i+1 || cap(queue.fastQueue) != 8 { t.Errorf("LockManagerWaitQueue Push Size fail") return } } - for i := 0; i < 15; i++ { + for i := 0; i < 7; i++ { if queue.Pop() != lock || queue.fastIndex != i+1 { t.Errorf("LockManagerWaitQueue Pop fail") return } } queue.Push(lock) - if len(queue.fastQueue) != 2 || cap(queue.fastQueue) != 16 || queue.fastIndex != 0 { + if len(queue.fastQueue) != 2 || cap(queue.fastQueue) != 8 || queue.fastIndex != 0 { t.Errorf("LockManagerWaitQueue Push Size fail") return } - for i := 0; i < 14; i++ { + for i := 0; i < 6; i++ { queue.Push(lock) - if len(queue.fastQueue) != i+3 || cap(queue.fastQueue) != 16 { + if len(queue.fastQueue) != i+3 || cap(queue.fastQueue) != 8 { t.Errorf("LockManagerWaitQueue Push Size fail") return } } for i := 0; i < 1024; i++ { queue.Push(lock) - if len(queue.fastQueue) != 16 || cap(queue.fastQueue) != 16 || queue.ringQueue == nil || len(queue.ringQueue.queue) != i+1 { + if len(queue.fastQueue) != 8 || cap(queue.fastQueue) != 8 || queue.ringQueue == nil || len(queue.ringQueue.queue) != i+1 { t.Errorf("LockManagerWaitQueue Push Size fail") return } } - for i := 0; i < 15; i++ { - if queue.Pop() != lock || queue.fastIndex != i+1 || cap(queue.fastQueue) != 16 { + for i := 0; i < 7; i++ { + if queue.Pop() != lock || queue.fastIndex != i+1 || cap(queue.fastQueue) != 8 { t.Errorf("LockManagerWaitQueue Pop fail") return } } - if queue.Head() != lock || queue.Pop() != lock || queue.fastIndex != 0 || cap(queue.fastQueue) != 16 || queue.Head() != lock { + if queue.Head() != lock || queue.Pop() != lock || queue.fastIndex != 0 || cap(queue.fastQueue) != 8 || queue.Head() != lock { t.Errorf("LockManagerWaitQueue Pop fail") return } @@ -142,7 +143,7 @@ func TestLockManagerWaitQueue(t *testing.T) { return } queue.Rellac() - if queue.fastIndex != 0 || len(queue.fastQueue) != 0 || cap(queue.fastQueue) != 16 || queue.ringQueue != nil { + if queue.fastIndex != 0 || len(queue.fastQueue) != 0 || cap(queue.fastQueue) != 8 || queue.ringQueue != nil { t.Errorf("LockManagerWaitQueue Rellac fail") return }