Skip to content

Commit 2c8ffc7

Browse files
committed
perf(心跳): 使用对象池优化心跳消息对象创建
移除类级别的_NotifyHeartBeat字段,改为在每次处理心跳请求时从对象池获取新实例。这减少了内存分配和垃圾回收压力,提高了性能。
1 parent 433dfdf commit 2c8ffc7

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

GameFrameX.Hotfix/StartUp/AppStartUpHotfixGameByHeart.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ namespace GameFrameX.Hotfix.StartUp;
3333

3434
internal partial class AppStartUpHotfixGame
3535
{
36-
private readonly NotifyHeartBeat _notifyHeartBeat = new NotifyHeartBeat
37-
{
38-
Timestamp = TimeHelper.UnixTimeMilliseconds(),
39-
};
40-
4136
/// <summary>
4237
/// 回复心跳消息
4338
/// </summary>
@@ -49,9 +44,10 @@ private async void ReplyHeartBeat(INetWorkChannel netWorkChannel, MessageObject
4944
{
5045
// LogHelper.Info("收到心跳请求:" + req.Timestamp);
5146
netWorkChannel.UpdateReceiveMessageTime();
52-
_notifyHeartBeat.Timestamp = TimeHelper.UnixTimeMilliseconds();
53-
_notifyHeartBeat.UniqueId = req.UniqueId;
54-
await netWorkChannel.WriteAsync(_notifyHeartBeat);
47+
NotifyHeartBeat notifyHeartBeat = MessageObjectPoolHelper.Get<NotifyHeartBeat>();
48+
notifyHeartBeat.Timestamp = TimeHelper.UnixTimeMilliseconds();
49+
notifyHeartBeat.UniqueId = req.UniqueId;
50+
await netWorkChannel.WriteAsync(notifyHeartBeat);
5551
}
5652
}
5753
}

0 commit comments

Comments
 (0)