// API consistent with Microsoft's ObjectPool. using System; using System.Runtime.CompilerServices; namespace Mirror { /// Pool of NetworkReaders to avoid allocations. public static class NetworkReaderPool { // reuse Pool // we still wrap it in NetworkReaderPool.Get/Recyle so we can reset the // position and array before reusing. static readonly Pool Pool = new Pool( // byte[] will be assigned in GetReader () => new NetworkReaderPooled(new byte[]{}), // initial capacity to avoid allocations in the first few frames 1000 ); /// Get the next reader in the pool. If pool is empty, creates a new Reader public static NetworkReaderPooled Get(byte[] bytes) { // grab from pool & set buffer NetworkReaderPooled reader = Pool.Get(); reader.SetBuffer(bytes); return reader; } /// Get the next reader in the pool. If pool is empty, creates a new Reader public static NetworkReaderPooled Get(ArraySegment segment) { // grab from pool & set buffer NetworkReaderPooled reader = Pool.Get(); reader.SetBuffer(segment); return reader; } /// Returns a reader to the pool. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(NetworkReaderPooled reader) { Pool.Return(reader); } } }