Implement chunk-based queue
This commit is contained in:
1 parent
93261808aa
commit
3a712dbe57
3 files changed
+277
-174
No files matched your search
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.ObjectPool;
|
||||
|
||||
namespace Recyclable.Collections.Pools
|
||||
{
|
||||
public static class RecyclableArrayPoolQueueChunkPool<T>
|
||||
{
|
||||
private static readonly ObjectPool<RecyclableArrayPoolQueueChunk<T>> _pool =
|
||||
new DefaultObjectPool<RecyclableArrayPoolQueueChunk<T>>(new DefaultPooledObjectPolicy<RecyclableArrayPoolQueueChunk<T>>());
|
||||
|
||||
public static RecyclableArrayPoolQueueChunk<T> Rent() => _pool.Get();
|
||||
|
||||
public static void Return(RecyclableArrayPoolQueueChunk<T> chunk)
|
||||
{
|
||||
chunk.Top = 0;
|
||||
chunk.Bottom = 0;
|
||||
chunk.Previous = null;
|
||||
chunk.Next = null;
|
||||
_pool.Return(chunk);
|
||||
}
|
||||
|
||||
public static void Dispose(RecyclableArrayPoolQueueChunk<T> chunk, bool needsClearing)
|
||||
{
|
||||
var buffer = chunk.Buffer;
|
||||
if (buffer.Length >= RecyclableDefaults.MinPooledArrayLength)
|
||||
{
|
||||
RecyclableArrayPool<T>.ReturnShared(buffer, needsClearing);
|
||||
}
|
||||
|
||||
chunk.Buffer = RecyclableArrayPoolQueueChunk<T>.Empty;
|
||||
chunk.Top = 0;
|
||||
chunk.Bottom = 0;
|
||||
chunk.Previous = null;
|
||||
chunk.Next = null;
|
||||
_pool.Return(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user