diff --git a/PooledStream.Test/TestPooledBufferWriter.cs b/PooledStream.Test/TestPooledBufferWriter.cs index 288dcc9..31df624 100644 --- a/PooledStream.Test/TestPooledBufferWriter.cs +++ b/PooledStream.Test/TestPooledBufferWriter.cs @@ -9,7 +9,7 @@ public class TestPooledBufferWriter [Fact] public void Write() { - using var bw = new PooledBufferWriter(ArrayPool.Shared, 1024); + using var bw = new PooledMemoryBufferWriter(ArrayPool.Shared, 1024); var data = new byte[128]; for (int i = 0; i < 16; i++) { @@ -28,7 +28,7 @@ public void Write() [Fact] public void Reset() { - using var bw = new PooledBufferWriter(); + using var bw = new PooledMemoryBufferWriter(); var sp = bw.GetSpan(128); sp.Fill(1); bw.Advance(128); diff --git a/PooledStream/PooledBufferWriter.cs b/PooledStream/PooledBufferWriter.cs index 5b032eb..e99f293 100644 --- a/PooledStream/PooledBufferWriter.cs +++ b/PooledStream/PooledBufferWriter.cs @@ -4,31 +4,36 @@ namespace PooledStream { - public class PooledBufferWriter : IDisposable, IBufferWriter where T : struct + public class PooledMemoryBufferWriter : IDisposable, IBufferWriter where T : struct { ArrayPool _Pool; T[] _currentBuffer; int _Position; int _Length; const int DefaultSize = 1024; + [MethodImpl(MethodImplOptions.AggressiveInlining)] void Reallocate(int sizeHint) { var nar = _Pool.Rent(sizeHint); - if(_currentBuffer != null) + if (_currentBuffer != null) { Buffer.BlockCopy(_currentBuffer, 0, nar, 0, _currentBuffer.Length < nar.Length ? _currentBuffer.Length : nar.Length); _Pool.Return(_currentBuffer); } _currentBuffer = nar; } - public PooledBufferWriter(): this(ArrayPool.Shared) + public PooledMemoryBufferWriter() : this(ArrayPool.Shared) + { + + } + public PooledMemoryBufferWriter(int preallocateSize) : this(ArrayPool.Shared, preallocateSize) { } - public PooledBufferWriter(ArrayPool pool): this(pool, DefaultSize) + public PooledMemoryBufferWriter(ArrayPool pool) : this(pool, DefaultSize) { } - public PooledBufferWriter(ArrayPool pool, int preallocateSize) + public PooledMemoryBufferWriter(ArrayPool pool, int preallocateSize) { _Pool = pool; _currentBuffer = null; @@ -39,12 +44,12 @@ public PooledBufferWriter(ArrayPool pool, int preallocateSize) [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Advance(int count) { - if(_Position + count > _currentBuffer.Length) + if (_Position + count > _currentBuffer.Length) { throw new IndexOutOfRangeException("advance too many(" + count.ToString() + ")"); } _Position += count; - if(_Length < _Position) + if (_Length < _Position) { _Length = _Position; } @@ -53,7 +58,7 @@ public void Advance(int count) [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { - if(_currentBuffer != null) + if (_currentBuffer != null) { _Pool.Return(_currentBuffer); _currentBuffer = null; @@ -65,11 +70,11 @@ public void Dispose() [MethodImpl(MethodImplOptions.AggressiveInlining)] public Memory GetMemory(int sizeHint = 0) { - if(sizeHint == 0) + if (sizeHint == 0) { sizeHint = DefaultSize; } - if(_Position + sizeHint > _currentBuffer.Length) + if (_Position + sizeHint > _currentBuffer.Length) { Reallocate(_Position + sizeHint); } @@ -79,11 +84,11 @@ public Memory GetMemory(int sizeHint = 0) [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span GetSpan(int sizeHint = 0) { - if(sizeHint == 0) + if (sizeHint == 0) { sizeHint = DefaultSize; } - if(_Position + sizeHint > _currentBuffer.Length) + if (_Position + sizeHint > _currentBuffer.Length) { Reallocate(_Position + sizeHint); } @@ -102,11 +107,8 @@ public ReadOnlyMemory ToMemoryUnsafe() [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reset(int preallocateSize = DefaultSize) { - if(preallocateSize > _currentBuffer.Length) - { - _Pool.Return(_currentBuffer); - _currentBuffer = _Pool.Rent(preallocateSize); - } + _Pool.Return(_currentBuffer); + _currentBuffer = _Pool.Rent(preallocateSize); _Length = 0; _Position = 0; }