Skip to content

Commit 9399f09

Browse files
benaadamshalter73
authored andcommitted
BufferSegment use ArrayPool for over-sized allocs (#13495)
1 parent 725fa34 commit 9399f09

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

src/Servers/Kestrel/Core/src/Internal/Infrastructure/PipeWriterHelpers/BufferSegment.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,16 @@ public void SetOwnedMemory(byte[] arrayPoolBuffer)
5959
AvailableMemory = arrayPoolBuffer;
6060
}
6161

62-
public void SetUnownedMemory(Memory<byte> memory)
63-
{
64-
AvailableMemory = memory;
65-
}
66-
6762
public void ResetMemory()
6863
{
6964
if (_memoryOwner is IMemoryOwner<byte> owner)
7065
{
7166
owner.Dispose();
7267
}
73-
else if (_memoryOwner is byte[] array)
68+
else
7469
{
75-
ArrayPool<byte>.Shared.Return(array);
70+
byte[] poolArray = (byte[])_memoryOwner;
71+
ArrayPool<byte>.Shared.Return(poolArray);
7672
}
7773

7874
// Order of below field clears is significant as it clears in a sequential order

src/Servers/Kestrel/Core/src/Internal/Infrastructure/PipeWriterHelpers/ConcurrentPipeWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ private BufferSegment AllocateSegmentUnsynchronized(int sizeHint)
341341
}
342342
else
343343
{
344-
// We can't use the pool so allocate an array
345-
newSegment.SetUnownedMemory(new byte[sizeHint]);
344+
// We can't use the recommended pool so use the ArrayPool
345+
newSegment.SetOwnedMemory(ArrayPool<byte>.Shared.Rent(sizeHint));
346346
}
347347

348348
_tailMemory = newSegment.AvailableMemory;

0 commit comments

Comments
 (0)