Skip to content

Commit

Permalink
feat: network writer pool to avoid expensive allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Jun 23, 2019
1 parent b2ba589 commit 3659acb
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Assets/Mirror/Runtime/NetworkWriterPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;

namespace Mirror
{

public static class NetworkWriterPool
{
static readonly Stack<NetworkWriter> pool = new Stack<NetworkWriter>();

public static NetworkWriter GetWriter()
{
if (pool.Count != 0)
{
NetworkWriter writer = pool.Pop();
// reset cached writer length and position
writer.SetLength(0);
return writer;
}

return new NetworkWriter();
}

public static void Recycle(NetworkWriter writer)
{
pool.Push(writer);
}
}
}

0 comments on commit 3659acb

Please sign in to comment.