Switch ops queue a singly linked list. (#3949)
Seeing a good chunk of CPU in deque usage in some profiles.
This commit is contained in:
+37
-10
@@ -17,8 +17,6 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gammazero/deque"
|
|
||||||
|
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,12 +27,16 @@ type OpsQueueParams struct {
|
|||||||
Logger logger.Logger
|
Logger logger.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------
|
||||||
|
|
||||||
type UntypedQueueOp func()
|
type UntypedQueueOp func()
|
||||||
|
|
||||||
func (op UntypedQueueOp) run() {
|
func (op UntypedQueueOp) run() {
|
||||||
op()
|
op()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------
|
||||||
|
|
||||||
type OpsQueue struct {
|
type OpsQueue struct {
|
||||||
opsQueueBase[UntypedQueueOp]
|
opsQueueBase[UntypedQueueOp]
|
||||||
}
|
}
|
||||||
@@ -43,6 +45,8 @@ func NewOpsQueue(params OpsQueueParams) *OpsQueue {
|
|||||||
return &OpsQueue{*newOpsQueueBase[UntypedQueueOp](params)}
|
return &OpsQueue{*newOpsQueueBase[UntypedQueueOp](params)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------
|
||||||
|
|
||||||
type typedQueueOp[T any] struct {
|
type typedQueueOp[T any] struct {
|
||||||
fn func(T)
|
fn func(T)
|
||||||
arg T
|
arg T
|
||||||
@@ -52,6 +56,8 @@ func (op typedQueueOp[T]) run() {
|
|||||||
op.fn(op.arg)
|
op.fn(op.arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------
|
||||||
|
|
||||||
type TypedOpsQueue[T any] struct {
|
type TypedOpsQueue[T any] struct {
|
||||||
opsQueueBase[typedQueueOp[T]]
|
opsQueueBase[typedQueueOp[T]]
|
||||||
}
|
}
|
||||||
@@ -64,15 +70,25 @@ func (oq *TypedOpsQueue[T]) Enqueue(fn func(T), arg T) {
|
|||||||
oq.opsQueueBase.Enqueue(typedQueueOp[T]{fn, arg})
|
oq.opsQueueBase.Enqueue(typedQueueOp[T]{fn, arg})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------
|
||||||
|
|
||||||
type opsQueueItem interface {
|
type opsQueueItem interface {
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type element struct {
|
||||||
|
opsQueueItem
|
||||||
|
next *element
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------
|
||||||
|
|
||||||
type opsQueueBase[T opsQueueItem] struct {
|
type opsQueueBase[T opsQueueItem] struct {
|
||||||
params OpsQueueParams
|
params OpsQueueParams
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
ops deque.Deque[T]
|
opsHead *element
|
||||||
|
opsTail *element
|
||||||
wake chan struct{}
|
wake chan struct{}
|
||||||
isStarted bool
|
isStarted bool
|
||||||
doneChan chan struct{}
|
doneChan chan struct{}
|
||||||
@@ -85,7 +101,6 @@ func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *opsQueueBase[T] {
|
|||||||
wake: make(chan struct{}, 1),
|
wake: make(chan struct{}, 1),
|
||||||
doneChan: make(chan struct{}),
|
doneChan: make(chan struct{}),
|
||||||
}
|
}
|
||||||
o.ops.SetBaseCap(int(min(params.MinSize, 128)))
|
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,12 +138,19 @@ func (oq *opsQueueBase[T]) Enqueue(op T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
oq.ops.PushBack(op)
|
if oq.opsHead == nil {
|
||||||
if oq.ops.Len() == 1 {
|
oq.opsHead = &element{op, nil}
|
||||||
|
oq.opsTail = oq.opsHead
|
||||||
|
|
||||||
|
// wake up on first entry in queue
|
||||||
select {
|
select {
|
||||||
case oq.wake <- struct{}{}:
|
case oq.wake <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
elem := &element{op, nil}
|
||||||
|
oq.opsTail.next = elem
|
||||||
|
oq.opsTail = elem
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,19 +161,24 @@ func (oq *opsQueueBase[T]) process() {
|
|||||||
<-oq.wake
|
<-oq.wake
|
||||||
for {
|
for {
|
||||||
oq.lock.Lock()
|
oq.lock.Lock()
|
||||||
if oq.isStopped && (!oq.params.FlushOnStop || oq.ops.Len() == 0) {
|
if oq.isStopped && (!oq.params.FlushOnStop || oq.opsHead == nil) {
|
||||||
oq.lock.Unlock()
|
oq.lock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if oq.ops.Len() == 0 {
|
elem := oq.opsHead
|
||||||
|
if elem == nil {
|
||||||
oq.lock.Unlock()
|
oq.lock.Unlock()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
op := oq.ops.PopFront()
|
|
||||||
|
oq.opsHead = elem.next
|
||||||
|
if oq.opsHead == nil {
|
||||||
|
oq.opsTail = nil
|
||||||
|
}
|
||||||
oq.lock.Unlock()
|
oq.lock.Unlock()
|
||||||
|
|
||||||
op.run()
|
elem.opsQueueItem.run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user