Revert "perf: faster NetworkWriter pooling (#1616)"

This reverts commit 5128b122fe.
This commit is contained in:
Paul Pacheco 2020-03-28 19:04:06 -05:00
parent 96e690ef9c
commit e96b687563

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic;
namespace Mirror namespace Mirror
{ {
// a NetworkWriter that will recycle itself when disposed
public class PooledNetworkWriter : NetworkWriter, IDisposable public class PooledNetworkWriter : NetworkWriter, IDisposable
{ {
public void Dispose() public void Dispose()
@ -9,36 +11,27 @@ public void Dispose()
NetworkWriterPool.Recycle(this); NetworkWriterPool.Recycle(this);
} }
} }
public static class NetworkWriterPool public static class NetworkWriterPool
{ {
public const int MaxPoolSize = 10; static readonly Stack<PooledNetworkWriter> pool = new Stack<PooledNetworkWriter>();
static readonly PooledNetworkWriter[] pool = new PooledNetworkWriter[MaxPoolSize];
static int next = -1;
public static PooledNetworkWriter GetWriter() public static PooledNetworkWriter GetWriter()
{ {
if (next == -1) if (pool.Count != 0)
{ {
return new PooledNetworkWriter(); PooledNetworkWriter writer = pool.Pop();
}
PooledNetworkWriter writer = pool[next];
pool[next] = null;
next--;
// reset cached writer length and position // reset cached writer length and position
writer.SetLength(0); writer.SetLength(0);
return writer; return writer;
} }
return new PooledNetworkWriter();
}
public static void Recycle(PooledNetworkWriter writer) public static void Recycle(PooledNetworkWriter writer)
{ {
if ((next + 1) < MaxPoolSize) pool.Push(writer);
{
next++;
pool[next] = writer;
}
} }
} }
} }