2020-03-18 07:44:32 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace ARMeilleure.Common
|
|
|
|
|
{
|
2020-12-23 21:58:36 -05:00
|
|
|
|
class ThreadStaticPool<T> where T : class, new()
|
2020-03-18 07:44:32 -04:00
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
private const int ChunkSizeLimit = 1000; // even
|
|
|
|
|
private const int PoolSizeIncrement = 200; // > 0
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
|
|
|
|
[ThreadStatic]
|
|
|
|
|
private static ThreadStaticPool<T> _instance;
|
2020-12-23 21:58:36 -05:00
|
|
|
|
|
2020-03-18 07:44:32 -04:00
|
|
|
|
public static ThreadStaticPool<T> Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
PreparePool(0); // So that we can still use a pool when blindly initializing one.
|
|
|
|
|
}
|
2020-12-23 21:58:36 -05:00
|
|
|
|
|
2020-03-18 07:44:32 -04:00
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
private static ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new();
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
|
|
|
|
private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
|
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
return _pools.GetOrAdd(groupId, (groupId) => new());
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void PreparePool(int groupId)
|
|
|
|
|
{
|
|
|
|
|
// Prepare the pool for this thread, ideally using an existing one from the specified group.
|
2020-12-23 21:58:36 -05:00
|
|
|
|
|
2020-03-18 07:44:32 -04:00
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
2020-12-23 21:58:36 -05:00
|
|
|
|
var pools = GetPools(groupId);
|
2020-03-18 07:44:32 -04:00
|
|
|
|
lock (pools)
|
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
_instance = (pools.Count != 0) ? pools.Pop() : new();
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ReturnPool(int groupId)
|
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
// Reset, limit if necessary, and return the pool for this thread to the specified group.
|
2020-12-23 21:58:36 -05:00
|
|
|
|
|
|
|
|
|
var pools = GetPools(groupId);
|
2020-03-18 07:44:32 -04:00
|
|
|
|
lock (pools)
|
|
|
|
|
{
|
|
|
|
|
_instance.Clear();
|
2021-01-12 13:04:02 -05:00
|
|
|
|
_instance.ChunkSizeLimiter();
|
2020-03-18 07:44:32 -04:00
|
|
|
|
pools.Push(_instance);
|
2020-12-23 21:58:36 -05:00
|
|
|
|
|
2020-03-18 07:44:32 -04:00
|
|
|
|
_instance = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 21:58:36 -05:00
|
|
|
|
public static void ResetPools()
|
|
|
|
|
{
|
|
|
|
|
// Resets any static references to the pools used by threads for each group, allowing them to be garbage collected.
|
|
|
|
|
|
|
|
|
|
foreach (var pools in _pools.Values)
|
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
foreach (var instance in pools)
|
|
|
|
|
{
|
|
|
|
|
instance.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 21:58:36 -05:00
|
|
|
|
pools.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pools.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
private List<T[]> _pool;
|
|
|
|
|
private int _chunkIndex = -1;
|
|
|
|
|
private int _poolIndex = -1;
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
private ThreadStaticPool()
|
2020-03-18 07:44:32 -04:00
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
_pool = new(ChunkSizeLimit * 2);
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
AddChunkIfNeeded();
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Allocate()
|
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
if (++_poolIndex >= PoolSizeIncrement)
|
2020-03-18 07:44:32 -04:00
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
AddChunkIfNeeded();
|
|
|
|
|
|
|
|
|
|
_poolIndex = 0;
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
2020-12-23 21:58:36 -05:00
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
return _pool[_chunkIndex][_poolIndex];
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
private void AddChunkIfNeeded()
|
2020-03-18 07:44:32 -04:00
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
if (++_chunkIndex >= _pool.Count)
|
|
|
|
|
{
|
|
|
|
|
T[] pool = new T[PoolSizeIncrement];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < PoolSizeIncrement; i++)
|
|
|
|
|
{
|
|
|
|
|
pool[i] = new T();
|
|
|
|
|
}
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
_pool.Add(pool);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_chunkIndex = 0;
|
|
|
|
|
_poolIndex = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChunkSizeLimiter()
|
|
|
|
|
{
|
|
|
|
|
if (_pool.Count >= ChunkSizeLimit)
|
2020-03-18 07:44:32 -04:00
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
int newChunkSize = ChunkSizeLimit / 2;
|
2020-03-18 07:44:32 -04:00
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
_pool.RemoveRange(newChunkSize, _pool.Count - newChunkSize);
|
|
|
|
|
_pool.Capacity = ChunkSizeLimit * 2;
|
|
|
|
|
}
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 13:04:02 -05:00
|
|
|
|
private void Dispose()
|
2020-03-18 07:44:32 -04:00
|
|
|
|
{
|
2021-01-12 13:04:02 -05:00
|
|
|
|
_pool.Clear();
|
2020-03-18 07:44:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|