Skip to content

Commit

Permalink
feat: adding overload to create pools without requiring buffer size
Browse files Browse the repository at this point in the history
not all items need buffer size so removing it from create new
  • Loading branch information
James-Frowen committed Jun 12, 2023
1 parent 85561be commit 9842b40
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Assets/Mirage/Runtime/Serialization/NetworkReaderPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Mirage.Serialization
public static class NetworkReaderPool
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(NetworkReaderPool));
private static Pool<PooledNetworkReader> pool = new Pool<PooledNetworkReader>(PooledNetworkReader.CreateNew, 0, 5, 100, logger);
private static Pool<PooledNetworkReader> pool = new Pool<PooledNetworkReader>(PooledNetworkReader.CreateNew, 5, 100, logger);

public static void Configure(int startPoolSize = 5, int maxPoolSize = 100)
{
Expand All @@ -21,7 +21,7 @@ public static void Configure(int startPoolSize = 5, int maxPoolSize = 100)
}
else
{
pool = new Pool<PooledNetworkReader>(PooledNetworkReader.CreateNew, 0, startPoolSize, maxPoolSize, logger);
pool = new Pool<PooledNetworkReader>(PooledNetworkReader.CreateNew, startPoolSize, maxPoolSize, logger);
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ private PooledNetworkReader(Pool<PooledNetworkReader> pool) : base()
_pool = pool ?? throw new ArgumentNullException(nameof(pool));
}

public static PooledNetworkReader CreateNew(int _, Pool<PooledNetworkReader> pool)
public static PooledNetworkReader CreateNew(Pool<PooledNetworkReader> pool)
{
return new PooledNetworkReader(pool);
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/SocketLayer/Connection/AckSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public AckSystem(IRawConnection connection, Config config, int maxPacketSize, IT
_connection = connection;
_time = time;
_bufferPool = bufferPool;
_reliablePool = new Pool<ReliablePacket>(ReliablePacket.CreateNew, default, 0, config.MaxReliablePacketsInSendBufferPerConnection);
_reliablePool = new Pool<ReliablePacket>(ReliablePacket.CreateNew, 0, config.MaxReliablePacketsInSendBufferPerConnection);
_metrics = metrics;

_ackTimeout = config.TimeBeforeEmptyAck;
Expand Down Expand Up @@ -759,7 +759,7 @@ public override bool Equals(object obj)
return false;
}

public static ReliablePacket CreateNew(int size, Pool<ReliablePacket> pool)
public static ReliablePacket CreateNew(Pool<ReliablePacket> pool)
{
return new ReliablePacket(pool);
}
Expand Down
24 changes: 19 additions & 5 deletions Assets/Mirage/Runtime/SocketLayer/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace Mirage.SocketLayer
public class Pool<T> where T : class
{
private const int POOL_EMPTY = -1;
private int _maxPoolSize;
private readonly int _bufferSize;
private readonly ILogger _logger;
public delegate T CreateNewItem(int bufferSize, Pool<T> pool);

private readonly ILogger _logger;
private readonly CreateNewItem _createNew;
private readonly int _bufferSize;
private T[] _pool;
private int _maxPoolSize;
private int _next = -1;
private int _created = 0;

private OverMaxLog _overMaxLog = new OverMaxLog();

/// <summary>
Expand All @@ -46,7 +46,17 @@ public void Configure(int startPoolSize, int maxPoolSize)
}

/// <summary>
///
/// Creates pool, that does not require Buffer size
/// </summary>
/// <param name="bufferSize">size of each buffer</param>
/// <param name="startPoolSize">how many buffers to create at start</param>
/// <param name="maxPoolSize">max number of buffers in pool</param>
/// <param name="logger"></param>
public Pool(CreateNewItemNoCount createNew, int startPoolSize, int maxPoolSize, ILogger logger = null)
: this((_, p) => createNew.Invoke(p), default, startPoolSize, maxPoolSize, logger) { }

/// <summary>
/// Creates pool where buffer size will be passed to items when created them
/// </summary>
/// <param name="bufferSize">size of each buffer</param>
/// <param name="startPoolSize">how many buffers to create at start</param>
Expand All @@ -68,6 +78,7 @@ public Pool(CreateNewItem createNew, int bufferSize, int startPoolSize, int maxP
}
}


private T CreateNewBuffer()
{
_created++;
Expand Down Expand Up @@ -107,6 +118,9 @@ public void Put(T buffer)
}
}

public delegate T CreateNewItemNoCount(Pool<T> pool);
public delegate T CreateNewItem(int bufferSize, Pool<T> pool);

private struct OverMaxLog
{
// 10 seconds log interval
Expand Down

0 comments on commit 9842b40

Please sign in to comment.