mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Revert "perf: faster NetworkWriter pooling (#1616)"
This reverts commit 5128b122fe
.
This commit is contained in:
parent
96e690ef9c
commit
e96b687563
@ -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();
|
||||||
|
// reset cached writer length and position
|
||||||
|
writer.SetLength(0);
|
||||||
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
PooledNetworkWriter writer = pool[next];
|
return new PooledNetworkWriter();
|
||||||
pool[next] = null;
|
|
||||||
next--;
|
|
||||||
|
|
||||||
// reset cached writer length and position
|
|
||||||
writer.SetLength(0);
|
|
||||||
return writer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Recycle(PooledNetworkWriter writer)
|
public static void Recycle(PooledNetworkWriter writer)
|
||||||
{
|
{
|
||||||
if ((next + 1) < MaxPoolSize)
|
pool.Push(writer);
|
||||||
{
|
|
||||||
next++;
|
|
||||||
pool[next] = writer;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user