// 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 ); // DEPRECATED 2022-03-10 [Obsolete("GetReader() was renamed to Get()")] public static NetworkReaderPooled GetReader(byte[] bytes) => Get(bytes); /// Get the next reader in the pool. If pool is empty, creates a new Reader [MethodImpl(MethodImplOptions.AggressiveInlining)] public static NetworkReaderPooled Get(byte[] bytes) { // grab from pool & set buffer NetworkReaderPooled reader = Pool.Get(); reader.SetBuffer(bytes); return reader; } // DEPRECATED 2022-03-10 [Obsolete("GetReader() was renamed to Get()")] public static NetworkReaderPooled GetReader(ArraySegment segment) => Get(segment); /// Get the next reader in the pool. If pool is empty, creates a new Reader [MethodImpl(MethodImplOptions.AggressiveInlining)] public static NetworkReaderPooled Get(ArraySegment segment) { // grab from pool & set buffer NetworkReaderPooled reader = Pool.Get(); reader.SetBuffer(segment); return reader; } // DEPRECATED 2022-03-10 [Obsolete("Recycle() was renamed to Return()")] public static void Recycle(NetworkReaderPooled reader) => Return(reader); /// Returns a reader to the pool. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(NetworkReaderPooled reader) { Pool.Return(reader); } } }