Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit edfce36

Browse files
committed
ConcurrentStack -> ConcurrentQueue
Less allocs on Enqueue vs Push
1 parent e11504f commit edfce36

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPool2.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class MemoryPool2 : IDisposable
5252
/// Thread-safe collection of blocks which are currently in the pool. A slab will pre-allocate all of the block tracking objects
5353
/// and add them to this collection. When memory is requested it is taken from here first, and when it is returned it is re-added.
5454
/// </summary>
55-
private readonly ConcurrentStack<MemoryPoolBlock2> _blocks = new ConcurrentStack<MemoryPoolBlock2>();
55+
private readonly ConcurrentQueue<MemoryPoolBlock2> _blocks = new ConcurrentQueue<MemoryPoolBlock2>();
5656

5757
/// <summary>
5858
/// Thread-safe collection of slabs which have been allocated by this pool. As long as a slab is in this collection and slab.IsActive,
@@ -89,7 +89,7 @@ public MemoryPoolBlock2 Lease(int minimumSize = MaxPooledBlockLength)
8989
}
9090

9191
MemoryPoolBlock2 block;
92-
if (_blocks.TryPop(out block))
92+
if (_blocks.TryDequeue(out block))
9393
{
9494
// block successfully taken from the stack - return it
9595
return block;
@@ -146,7 +146,7 @@ private MemoryPoolBlock2 AllocateSlab()
146146
public void Return(MemoryPoolBlock2 block)
147147
{
148148
block.Reset();
149-
_blocks.Push(block);
149+
_blocks.Enqueue(block);
150150
}
151151

152152
protected virtual void Dispose(bool disposing)
@@ -159,7 +159,7 @@ protected virtual void Dispose(bool disposing)
159159
}
160160

161161
MemoryPoolBlock2 block;
162-
while (_blocks.TryPop(out block))
162+
while (_blocks.TryDequeue(out block))
163163
{
164164
// Deactivate finalizers
165165
block.Dispose();

0 commit comments

Comments
 (0)