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.
This commit is contained in:
RoosterDragon
2017-11-12 12:06:16 +00:00
committed by Paul Chote
parent f4502e9aa7
commit 713cdaef5d

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Network
class Chunk class Chunk
{ {
public int Frame; public int Frame;
public List<Pair<int, byte[]>> Packets = new List<Pair<int, byte[]>>(); public Pair<int, byte[]>[] Packets;
} }
Queue<Chunk> chunks = new Queue<Chunk>(); Queue<Chunk> chunks = new Queue<Chunk>();
@@ -45,6 +45,8 @@ namespace OpenRA.Network
// to avoid issues with all immediate orders being resolved on the first tick. // to avoid issues with all immediate orders being resolved on the first tick.
using (var rs = File.OpenRead(replayFilename)) using (var rs = File.OpenRead(replayFilename))
{ {
var packets = new List<Pair<int, byte[]>>();
var chunk = new Chunk(); var chunk = new Chunk();
while (rs.Position < rs.Length) while (rs.Position < rs.Length)
@@ -55,7 +57,7 @@ namespace OpenRA.Network
var packetLen = rs.ReadInt32(); var packetLen = rs.ReadInt32();
var packet = rs.ReadBytes(packetLen); var packet = rs.ReadBytes(packetLen);
var frame = BitConverter.ToInt32(packet, 0); var frame = BitConverter.ToInt32(packet, 0);
chunk.Packets.Add(Pair.New(client, packet)); packets.Add(Pair.New(client, packet));
if (frame != int.MaxValue && if (frame != int.MaxValue &&
(!lastClientsFrame.ContainsKey(client) || frame > lastClientsFrame[client])) (!lastClientsFrame.ContainsKey(client) || frame > lastClientsFrame[client]))
@@ -81,6 +83,8 @@ namespace OpenRA.Network
{ {
// Regular order - finalize the chunk // Regular order - finalize the chunk
chunk.Frame = frame; chunk.Frame = frame;
chunk.Packets = packets.ToArray();
packets.Clear();
chunks.Enqueue(chunk); chunks.Enqueue(chunk);
chunk = new Chunk(); chunk = new Chunk();