From 713cdaef5dc5e5fc114b47ae74614b12e08957ac Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sun, 12 Nov 2017 12:06:16 +0000 Subject: [PATCH] Reduce allocations needed by ReplayConnection. Packets from each chunk are now saved directly in an array, removing the overhead of a list. Additionally, a list is reused as a buffer for decoding packets into, preventing a new buffer from needing to be allocated for each chunk. --- OpenRA.Game/Network/ReplayConnection.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/Network/ReplayConnection.cs b/OpenRA.Game/Network/ReplayConnection.cs index 4a101d286e..2cc2176e26 100644 --- a/OpenRA.Game/Network/ReplayConnection.cs +++ b/OpenRA.Game/Network/ReplayConnection.cs @@ -22,7 +22,7 @@ namespace OpenRA.Network class Chunk { public int Frame; - public List> Packets = new List>(); + public Pair[] Packets; } Queue chunks = new Queue(); @@ -45,6 +45,8 @@ namespace OpenRA.Network // to avoid issues with all immediate orders being resolved on the first tick. using (var rs = File.OpenRead(replayFilename)) { + var packets = new List>(); + var chunk = new Chunk(); while (rs.Position < rs.Length) @@ -55,7 +57,7 @@ namespace OpenRA.Network var packetLen = rs.ReadInt32(); var packet = rs.ReadBytes(packetLen); var frame = BitConverter.ToInt32(packet, 0); - chunk.Packets.Add(Pair.New(client, packet)); + packets.Add(Pair.New(client, packet)); if (frame != int.MaxValue && (!lastClientsFrame.ContainsKey(client) || frame > lastClientsFrame[client])) @@ -81,6 +83,8 @@ namespace OpenRA.Network { // Regular order - finalize the chunk chunk.Frame = frame; + chunk.Packets = packets.ToArray(); + packets.Clear(); chunks.Enqueue(chunk); chunk = new Chunk();