BufferSegment use ArrayPool for over-sized allocs (#13495)

This commit is contained in:
Ben Adams 2019-08-30 18:31:32 +01:00 committed by Stephen Halter
parent 725fa34cc8
commit 9399f09b72
2 changed files with 5 additions and 9 deletions

View File

@ -59,20 +59,16 @@ namespace System.IO.Pipelines
AvailableMemory = arrayPoolBuffer;
}
public void SetUnownedMemory(Memory<byte> memory)
{
AvailableMemory = memory;
}
public void ResetMemory()
{
if (_memoryOwner is IMemoryOwner<byte> owner)
{
owner.Dispose();
}
else if (_memoryOwner is byte[] array)
else
{
ArrayPool<byte>.Shared.Return(array);
byte[] poolArray = (byte[])_memoryOwner;
ArrayPool<byte>.Shared.Return(poolArray);
}
// Order of below field clears is significant as it clears in a sequential order

View File

@ -341,8 +341,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeW
}
else
{
// We can't use the pool so allocate an array
newSegment.SetUnownedMemory(new byte[sizeHint]);
// We can't use the recommended pool so use the ArrayPool
newSegment.SetOwnedMemory(ArrayPool<byte>.Shared.Rent(sizeHint));
}
_tailMemory = newSegment.AvailableMemory;