// API consistent with Microsoft's ObjectPool. using System.Runtime.CompilerServices; namespace Mirror { /// Pool of NetworkWriters to avoid allocations. public static class NetworkWriterPool { // reuse Pool // we still wrap it in NetworkWriterPool.Get/Recycle so we can reset the // position before reusing. // this is also more consistent with NetworkReaderPool where we need to // assign the internal buffer before reusing. static readonly Pool Pool = new Pool( () => new NetworkWriterPooled(), // initial capacity to avoid allocations in the first few frames // 1000 * 1200 bytes = around 1 MB. 1000 ); /// Get a writer from the pool. Creates new one if pool is empty. public static NetworkWriterPooled Get() { // grab from pool & reset position NetworkWriterPooled writer = Pool.Get(); writer.Reset(); return writer; } /// Return a writer to the pool. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(NetworkWriterPooled writer) { Pool.Return(writer); } } }