diff --git a/MailKit/ByteArrayBuilder.cs b/MailKit/ByteArrayBuilder.cs index 43e5b8b3ce..c080c17938 100644 --- a/MailKit/ByteArrayBuilder.cs +++ b/MailKit/ByteArrayBuilder.cs @@ -27,17 +27,20 @@ using System; using System.Text; using System.Buffers; +using System.Runtime.CompilerServices; namespace MailKit { class ByteArrayBuilder : IDisposable { + readonly int initialCapacity; byte[] buffer; int length; - public ByteArrayBuilder (int initialCapacity) + public ByteArrayBuilder (int capacity) { - buffer = ArrayPool.Shared.Rent (initialCapacity); + buffer = ArrayPool.Shared.Rent (capacity); + initialCapacity = capacity; length = 0; } @@ -54,6 +57,7 @@ public byte[] GetBuffer () return buffer; } + [MethodImpl (MethodImplOptions.AggressiveInlining)] void EnsureCapacity (int capacity) { if (capacity > buffer.Length) { @@ -79,6 +83,11 @@ public void Append (byte[] text, int startIndex, int count) public void Clear () { + if (buffer.Length > initialCapacity * 4) { + ArrayPool.Shared.Return (buffer); + buffer = ArrayPool.Shared.Rent (initialCapacity); + } + length = 0; }