2021-01-27 06:21:37 +01:00
|
|
|
|
using ARMeilleure.Translation.PTC;
|
|
|
|
|
using System;
|
2020-03-18 12:44:32 +01:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace ARMeilleure.Common
|
|
|
|
|
{
|
2020-12-24 03:58:36 +01:00
|
|
|
|
class ThreadStaticPool<T> where T : class, new()
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
[ThreadStatic]
|
|
|
|
|
private static ThreadStaticPool<T> _instance;
|
2020-12-24 03:58:36 +01:00
|
|
|
|
|
2020-03-18 12:44:32 +01:00
|
|
|
|
public static ThreadStaticPool<T> Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
PreparePool(); // So that we can still use a pool when blindly initializing one.
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
2020-12-24 03:58:36 +01:00
|
|
|
|
|
2020-03-18 12:44:32 +01:00
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
private static readonly ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new();
|
2020-03-18 12:44:32 +01:00
|
|
|
|
|
|
|
|
|
private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
|
|
|
|
|
{
|
2021-01-12 19:04:02 +01:00
|
|
|
|
return _pools.GetOrAdd(groupId, (groupId) => new());
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
public static void PreparePool(
|
|
|
|
|
int groupId = 0,
|
|
|
|
|
ChunkSizeLimit chunkSizeLimit = ChunkSizeLimit.Large,
|
|
|
|
|
PoolSizeIncrement poolSizeIncrement = PoolSizeIncrement.Default)
|
|
|
|
|
{
|
|
|
|
|
if (Ptc.State == PtcState.Disabled)
|
|
|
|
|
{
|
|
|
|
|
PreparePoolDefault(groupId, (int)chunkSizeLimit, (int)poolSizeIncrement);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PreparePoolSlim((int)chunkSizeLimit, (int)poolSizeIncrement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void PreparePoolDefault(int groupId, int chunkSizeLimit, int poolSizeIncrement)
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
// Prepare the pool for this thread, ideally using an existing one from the specified group.
|
2020-12-24 03:58:36 +01:00
|
|
|
|
|
2020-03-18 12:44:32 +01:00
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
2020-12-24 03:58:36 +01:00
|
|
|
|
var pools = GetPools(groupId);
|
2020-03-18 12:44:32 +01:00
|
|
|
|
lock (pools)
|
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
_instance = (pools.Count != 0) ? pools.Pop() : new(chunkSizeLimit, poolSizeIncrement);
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
private static void PreparePoolSlim(int chunkSizeLimit, int poolSizeIncrement)
|
|
|
|
|
{
|
|
|
|
|
// Prepare the pool for this thread.
|
|
|
|
|
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new(chunkSizeLimit, poolSizeIncrement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ResetPool(int groupId = 0)
|
|
|
|
|
{
|
|
|
|
|
if (Ptc.State == PtcState.Disabled)
|
|
|
|
|
{
|
|
|
|
|
ResetPoolDefault(groupId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ResetPoolSlim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ResetPoolDefault(int groupId)
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
2021-01-12 19:04:02 +01:00
|
|
|
|
// Reset, limit if necessary, and return the pool for this thread to the specified group.
|
2020-12-24 03:58:36 +01:00
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
if (_instance != null)
|
|
|
|
|
{
|
|
|
|
|
var pools = GetPools(groupId);
|
|
|
|
|
lock (pools)
|
|
|
|
|
{
|
|
|
|
|
_instance.Clear();
|
|
|
|
|
_instance.ChunkSizeLimiter();
|
|
|
|
|
pools.Push(_instance);
|
|
|
|
|
|
|
|
|
|
_instance = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ResetPoolSlim()
|
|
|
|
|
{
|
|
|
|
|
// Reset, limit if necessary, the pool for this thread.
|
|
|
|
|
|
|
|
|
|
if (_instance != null)
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
_instance.Clear();
|
2021-01-12 19:04:02 +01:00
|
|
|
|
_instance.ChunkSizeLimiter();
|
2021-01-27 06:21:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-24 03:58:36 +01:00
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
public static void DisposePools()
|
|
|
|
|
{
|
|
|
|
|
if (Ptc.State == PtcState.Disabled)
|
|
|
|
|
{
|
|
|
|
|
DisposePoolsDefault();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DisposePoolSlim();
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
private static void DisposePoolsDefault()
|
2020-12-24 03:58:36 +01:00
|
|
|
|
{
|
|
|
|
|
// 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 19:04:02 +01:00
|
|
|
|
foreach (var instance in pools)
|
|
|
|
|
{
|
|
|
|
|
instance.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 03:58:36 +01:00
|
|
|
|
pools.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pools.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
private static void DisposePoolSlim()
|
|
|
|
|
{
|
|
|
|
|
// Dispose the pool for this thread.
|
|
|
|
|
|
|
|
|
|
if (_instance != null)
|
|
|
|
|
{
|
|
|
|
|
_instance.Dispose();
|
|
|
|
|
|
|
|
|
|
_instance = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
private List<T[]> _pool;
|
|
|
|
|
private int _chunkIndex = -1;
|
|
|
|
|
private int _poolIndex = -1;
|
2021-01-27 06:21:37 +01:00
|
|
|
|
private int _chunkSizeLimit;
|
|
|
|
|
private int _poolSizeIncrement;
|
2020-03-18 12:44:32 +01:00
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
private ThreadStaticPool(int chunkSizeLimit, int poolSizeIncrement)
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
_chunkSizeLimit = chunkSizeLimit;
|
|
|
|
|
_poolSizeIncrement = poolSizeIncrement;
|
|
|
|
|
|
|
|
|
|
_pool = new(chunkSizeLimit * 2);
|
2020-03-18 12:44:32 +01:00
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
AddChunkIfNeeded();
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Allocate()
|
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
if (++_poolIndex >= _poolSizeIncrement)
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
2021-01-12 19:04:02 +01:00
|
|
|
|
AddChunkIfNeeded();
|
|
|
|
|
|
|
|
|
|
_poolIndex = 0;
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
2020-12-24 03:58:36 +01:00
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
return _pool[_chunkIndex][_poolIndex];
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
private void AddChunkIfNeeded()
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
2021-01-12 19:04:02 +01:00
|
|
|
|
if (++_chunkIndex >= _pool.Count)
|
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
T[] pool = new T[_poolSizeIncrement];
|
2021-01-12 19:04:02 +01:00
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
|
for (int i = 0; i < _poolSizeIncrement; i++)
|
2021-01-12 19:04:02 +01:00
|
|
|
|
{
|
|
|
|
|
pool[i] = new T();
|
|
|
|
|
}
|
2020-03-18 12:44:32 +01:00
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
_pool.Add(pool);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 12:44:32 +01:00
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_chunkIndex = 0;
|
|
|
|
|
_poolIndex = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChunkSizeLimiter()
|
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
if (_pool.Count >= _chunkSizeLimit)
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
int newChunkSize = _chunkSizeLimit / 2;
|
2020-03-18 12:44:32 +01:00
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
_pool.RemoveRange(newChunkSize, _pool.Count - newChunkSize);
|
2021-01-27 06:21:37 +01:00
|
|
|
|
_pool.Capacity = _chunkSizeLimit * 2;
|
2021-01-12 19:04:02 +01:00
|
|
|
|
}
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 19:04:02 +01:00
|
|
|
|
private void Dispose()
|
2020-03-18 12:44:32 +01:00
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
|
_pool = null;
|
2020-03-18 12:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|