Skip to content

Commit

Permalink
帧同步使用MemoryPack做序列化,提升性能,稍加修改甚至可以做到无GC
Browse files Browse the repository at this point in the history
  • Loading branch information
egametang committed Nov 25, 2023
1 parent bbd08a9 commit 12ab008
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Unity/Assets/Scripts/Core/Serialize/MemoryPackHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ public static class MemoryPackHelper
{
public static byte[] Serialize(object message)
{
if (message is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
return MemoryPackSerializer.Serialize(message.GetType(), message);
}

public static void Serialize(object message, MemoryBuffer stream)
{
if (message is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
MemoryPackSerializer.Serialize(message.GetType(), stream, message);
}

Expand Down
4 changes: 2 additions & 2 deletions Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static LSWorld GetLSWorld(this Room self, SceneType sceneType, int frame)
{
MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
memoryBuffer.Seek(0, SeekOrigin.Begin);
LSWorld lsWorld = MongoHelper.Deserialize(typeof (LSWorld), memoryBuffer) as LSWorld;
LSWorld lsWorld = MemoryPackHelper.Deserialize(typeof (LSWorld), memoryBuffer) as LSWorld;
lsWorld.SceneType = sceneType;
memoryBuffer.Seek(0, SeekOrigin.Begin);
return lsWorld;
Expand All @@ -70,7 +70,7 @@ private static void SaveLSWorld(this Room self)
memoryBuffer.Seek(0, SeekOrigin.Begin);
memoryBuffer.SetLength(0);

MongoHelper.Serialize(self.LSWorld, memoryBuffer);
MemoryPackHelper.Serialize(self.LSWorld, memoryBuffer);
memoryBuffer.Seek(0, SeekOrigin.Begin);

long hash = memoryBuffer.GetBuffer().Hash(0, (int) memoryBuffer.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
<type fullname="MemoryPack.IMemoryPackFormatterRegister" preserve="all" />
<type fullname="MemoryPack.IMemoryPackable`1" preserve="all" />
<type fullname="MemoryPack.Internal.PreserveAttribute" preserve="all" />
<type fullname="MemoryPack.MemoryPackConstructorAttribute" preserve="all" />
<type fullname="MemoryPack.MemoryPackFormatterProvider" preserve="all" />
<type fullname="MemoryPack.MemoryPackFormatter`1" preserve="all" />
<type fullname="MemoryPack.MemoryPackIgnoreAttribute" preserve="all" />
<type fullname="MemoryPack.MemoryPackIncludeAttribute" preserve="all" />
<type fullname="MemoryPack.MemoryPackOrderAttribute" preserve="all" />
<type fullname="MemoryPack.MemoryPackReader" preserve="all" />
<type fullname="MemoryPack.MemoryPackSerializationException" preserve="all" />
Expand Down Expand Up @@ -317,6 +320,7 @@
<type fullname="System.Runtime.CompilerServices.RuntimeCompatibilityAttribute" preserve="all" />
<type fullname="System.Runtime.CompilerServices.TaskAwaiter" preserve="all" />
<type fullname="System.Runtime.CompilerServices.TaskAwaiter`1" preserve="all" />
<type fullname="System.Runtime.Versioning.TargetFrameworkAttribute" preserve="all" />
<type fullname="System.RuntimeTypeHandle" preserve="all" />
<type fullname="System.Security.Permissions.SecurityAction" preserve="all" />
<type fullname="System.Security.Permissions.SecurityPermissionAttribute" preserve="all" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using MemoryPack;

namespace ET
{
[ComponentOf(typeof(LSUnit))]
public class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
[MemoryPackable]
public partial class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
{
public LSInput LSInput { get; set; }
}
Expand Down
7 changes: 5 additions & 2 deletions Unity/Assets/Scripts/Model/Share/LockStep/LSUnitComponent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace ET
using MemoryPack;

namespace ET
{
[ComponentOf(typeof(LSWorld))]
public class LSUnitComponent: LSEntity, IAwake, ISerializeToEntity
[MemoryPackable]
public partial class LSUnitComponent: LSEntity, IAwake, ISerializeToEntity
{
}
}
8 changes: 7 additions & 1 deletion Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using MemoryPack;
using TrueSync;

namespace ET
Expand All @@ -26,8 +27,10 @@ public static TSRandom GetRandom(this LSEntity entity)
[EnableMethod]
[ChildOf]
[ComponentOf]
public class LSWorld: Entity, IAwake, IScene
[MemoryPackable]
public partial class LSWorld: Entity, IAwake, IScene
{
[MemoryPackConstructor]
public LSWorld()
{
}
Expand All @@ -42,9 +45,11 @@ public LSWorld(SceneType sceneType)
private readonly LSUpdater updater = new();

[BsonIgnore]
[MemoryPackIgnore]
public Fiber Fiber { get; set; }

[BsonElement]
[MemoryPackInclude]
private long idGenerator;

public long GetId()
Expand All @@ -55,6 +60,7 @@ public long GetId()
public TSRandom Random { get; set; }

[BsonIgnore]
[MemoryPackIgnore]
public SceneType SceneType { get; set; }

public int Frame { get; set; }
Expand Down
8 changes: 7 additions & 1 deletion Unity/Assets/Scripts/ThirdParty/TrueSync/TSRandom.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using MemoryPack;
using MongoDB.Bson.Serialization.Attributes;

namespace TrueSync {

/**
* @brief Generates random numbers based on a deterministic approach.
**/
public class TSRandom {
[MemoryPackable]
public partial class TSRandom {
// From http://www.codeproject.com/Articles/164087/Random-Number-Generation
// Class TSRandom generates random numbers
// from a uniform distribution using the Mersenne
Expand All @@ -18,12 +20,16 @@ public class TSRandom {
private const uint LOWER_MASK = 0x7fffffffU;
private const int MAX_RAND_INT = 0x7fffffff;
[BsonElement]
[MemoryPackInclude]
private uint[] mag01 = { 0x0U, MATRIX_A };
[BsonElement]
[MemoryPackInclude]
private uint[] mt = new uint[N];
[BsonElement]
[MemoryPackInclude]
private int mti = N + 1;

[MemoryPackConstructor]
private TSRandom() {
}

Expand Down
4 changes: 3 additions & 1 deletion Unity/Assets/Scripts/ThirdParty/Unity.ThirdParty.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "Unity.ThirdParty",
"rootNamespace": "ET",
"references": [],
"references": [
"MemoryPack"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
Expand Down

0 comments on commit 12ab008

Please sign in to comment.