Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations needed by ReplayConnection. #14374

Merged
merged 1 commit into from Nov 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions OpenRA.Game/Network/ReplayConnection.cs
Expand Up @@ -22,7 +22,7 @@ public sealed class ReplayConnection : IConnection
class Chunk
{
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>();
Expand All @@ -45,6 +45,8 @@ public ReplayConnection(string replayFilename)
// to avoid issues with all immediate orders being resolved on the first tick.
using (var rs = File.OpenRead(replayFilename))
{
var packets = new List<Pair<int, byte[]>>();

var chunk = new Chunk();

while (rs.Position < rs.Length)
Expand All @@ -55,7 +57,7 @@ public ReplayConnection(string replayFilename)
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]))
Expand All @@ -81,6 +83,8 @@ public ReplayConnection(string replayFilename)
{
// Regular order - finalize the chunk
chunk.Frame = frame;
chunk.Packets = packets.ToArray();
packets.Clear();
chunks.Enqueue(chunk);
chunk = new Chunk();

Expand Down