Skip to content

Commit

Permalink
BufferSegment use ArrayPool for over-sized allocs (#13495)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored and halter73 committed Aug 30, 2019
1 parent 725fa34 commit 9399f09
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,16 @@ public void SetOwnedMemory(byte[] arrayPoolBuffer)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ private BufferSegment AllocateSegmentUnsynchronized(int sizeHint)
}
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;
Expand Down

0 comments on commit 9399f09

Please sign in to comment.