From 139fe4291a229e944f97d1aec3dec001c1612c8f Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 23 Oct 2023 09:29:43 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20lockstep=20=E5=8C=85=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=B8=A7=E5=91=BD=E4=BB=A4=E9=80=BB=E8=BE=91=EF=BC=8C=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=A4=9A=E4=BD=99=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/lockstep/lockstep.go | 43 +++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/server/lockstep/lockstep.go b/server/lockstep/lockstep.go index c9b3d55..efbd983 100644 --- a/server/lockstep/lockstep.go +++ b/server/lockstep/lockstep.go @@ -2,7 +2,6 @@ package lockstep import ( "encoding/json" - "github.com/kercylan98/minotaur/utils/hash" "github.com/kercylan98/minotaur/utils/timer" "sync" "time" @@ -12,7 +11,6 @@ import ( func NewLockstep[ClientID comparable, Command any](options ...Option[ClientID, Command]) *Lockstep[ClientID, Command] { lockstep := &Lockstep[ClientID, Command]{ currentFrame: -1, - frames: make(map[int64][]Command), ticker: timer.GetTicker(10), frameRate: 15, serialization: func(frame int64, commands []Command) []byte { @@ -55,11 +53,11 @@ type Lockstep[ClientID comparable, Command any] struct { currentCommands []Command // 当前帧指令 currentFrameLock sync.RWMutex // 当前主要帧锁 - frames map[int64][]Command // 所有已经落帧完成的指令 - frameLock sync.RWMutex // 帧锁 - frameCache map[int64][]byte // 帧序列化缓存 - frameCacheLock sync.RWMutex // 帧序列化缓存锁 - ticker *timer.Ticker // 定时器 + //frames map[int64][]Command // 所有已经落帧完成的指令 + //frameLock sync.RWMutex // 帧锁 + frameCache map[int64][]byte // 帧序列化缓存 + frameCacheLock sync.RWMutex // 帧序列化缓存锁 + ticker *timer.Ticker // 定时器 lockstepStoppedEventHandles []StoppedEventHandle[ClientID, Command] } @@ -136,11 +134,10 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() { slf.currentCommands = make([]Command, 0, len(currentCommands)) slf.currentFrameLock.Unlock() - slf.frameLock.Lock() slf.clientLock.RLock() - defer slf.frameLock.Unlock() defer slf.clientLock.RUnlock() - slf.frames[currentFrame] = currentCommands + slf.frameCacheLock.Lock() + defer slf.frameCacheLock.Unlock() for clientId, client := range slf.clients { var i = slf.clientFrame[clientId] @@ -150,7 +147,7 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() { for ; i < currentFrame; i++ { cache, exist := slf.frameCache[i] if !exist { - cache = slf.serialization(i, slf.frames[i]) + cache = slf.serialization(i, currentCommands) slf.frameCache[i] = cache } client.Write(cache) @@ -178,13 +175,13 @@ func (slf *Lockstep[ClientID, Command]) StopBroadcast() { defer slf.currentFrameLock.Unlock() slf.frameCacheLock.Lock() defer slf.frameCacheLock.Unlock() - slf.frameLock.Lock() - defer slf.frameLock.Unlock() + slf.clientLock.Lock() + defer slf.clientLock.Unlock() + slf.frameCache = make(map[int64][]byte) slf.currentCommands = make([]Command, 0) slf.currentFrame = -1 slf.clientFrame = make(map[ClientID]int64) - slf.frames = make(map[int64][]Command) } // IsRunning 是否正在广播 @@ -196,11 +193,18 @@ func (slf *Lockstep[ClientID, Command]) IsRunning() bool { // AddCommand 添加命令到当前帧 func (slf *Lockstep[ClientID, Command]) AddCommand(command Command) { - slf.currentFrameLock.RLock() - defer slf.currentFrameLock.RUnlock() + slf.currentFrameLock.Lock() + defer slf.currentFrameLock.Unlock() slf.currentCommands = append(slf.currentCommands, command) } +// AddCommands 添加命令到当前帧 +func (slf *Lockstep[ClientID, Command]) AddCommands(commands []Command) { + slf.currentFrameLock.Lock() + defer slf.currentFrameLock.Unlock() + slf.currentCommands = append(slf.currentCommands, commands...) +} + // GetCurrentFrame 获取当前帧 func (slf *Lockstep[ClientID, Command]) GetCurrentFrame() int64 { slf.currentFrameLock.RLock() @@ -221,13 +225,6 @@ func (slf *Lockstep[ClientID, Command]) GetFrameLimit() int64 { return slf.frameLimit } -// GetFrames 获取所有落帧完成的数据 -func (slf *Lockstep[ClientID, Command]) GetFrames() map[int64][]Command { - slf.frameLock.RLock() - defer slf.frameLock.RUnlock() - return hash.Copy(slf.frames) -} - // GetCurrentCommands 获取当前帧还未结束时的所有指令 func (slf *Lockstep[ClientID, Command]) GetCurrentCommands() []Command { slf.currentFrameLock.RLock()