mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.0 KiB
44 lines
1.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
|
|
/*https://github.com/sidristij/memory-pools*/
|
|
|
|
namespace MemoryPools.Memory.Pooling
|
|
{
|
|
public sealed class BucketsBasedCrossThreadsArrayPool<T>
|
|
{
|
|
[ThreadStatic]
|
|
private static BucketsBasedCrossThreadsArrayPool<T> _shared;
|
|
private static readonly Queue<T[]>[] _pool = new Queue<T[]>[24];
|
|
|
|
static BucketsBasedCrossThreadsArrayPool()
|
|
{
|
|
for (var i = 0; i < _pool.Length; i++) _pool[i] = new Queue<T[]>();
|
|
}
|
|
|
|
public static BucketsBasedCrossThreadsArrayPool<T> Shared => _shared ??= new BucketsBasedCrossThreadsArrayPool<T>();
|
|
|
|
public T[] Rent(int minimumLength)
|
|
{
|
|
var queueIndex = Utilities.GetBucket(minimumLength);
|
|
var queue = _pool[queueIndex];
|
|
T[] arr;
|
|
|
|
if (queue.Count == 0)
|
|
{
|
|
var length = Utilities.GetMaxSizeForBucket(queueIndex);
|
|
arr = new T[length];
|
|
return arr;
|
|
}
|
|
|
|
arr = queue.Dequeue();
|
|
return arr;
|
|
}
|
|
|
|
public void Return(T[] array)
|
|
{
|
|
_pool[Utilities.GetBucket(array.Length)].Enqueue(array);
|
|
}
|
|
}
|
|
}
|