Skip to content

Commit

Permalink
Optimize the initial size of LockManager Locked queue and wait queue …
Browse files Browse the repository at this point in the history
…memory
  • Loading branch information
snower committed May 21, 2024
1 parent 92470f3 commit dc73c29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions server/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
27 changes: 14 additions & 13 deletions server/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit dc73c29

Please sign in to comment.