ThreadStaticPool.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace ARMeilleure.Common
  5. {
  6. class ThreadStaticPool<T> where T : class, new()
  7. {
  8. private const int ChunkSizeLimit = 1000; // even
  9. private const int PoolSizeIncrement = 200; // > 0
  10. [ThreadStatic]
  11. private static ThreadStaticPool<T> _instance;
  12. public static ThreadStaticPool<T> Instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. PreparePool(0); // So that we can still use a pool when blindly initializing one.
  19. }
  20. return _instance;
  21. }
  22. }
  23. private static ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new();
  24. private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
  25. {
  26. return _pools.GetOrAdd(groupId, (groupId) => new());
  27. }
  28. public static void PreparePool(int groupId)
  29. {
  30. // Prepare the pool for this thread, ideally using an existing one from the specified group.
  31. if (_instance == null)
  32. {
  33. var pools = GetPools(groupId);
  34. lock (pools)
  35. {
  36. _instance = (pools.Count != 0) ? pools.Pop() : new();
  37. }
  38. }
  39. }
  40. public static void ReturnPool(int groupId)
  41. {
  42. // Reset, limit if necessary, and return the pool for this thread to the specified group.
  43. var pools = GetPools(groupId);
  44. lock (pools)
  45. {
  46. _instance.Clear();
  47. _instance.ChunkSizeLimiter();
  48. pools.Push(_instance);
  49. _instance = null;
  50. }
  51. }
  52. public static void ResetPools()
  53. {
  54. // Resets any static references to the pools used by threads for each group, allowing them to be garbage collected.
  55. foreach (var pools in _pools.Values)
  56. {
  57. foreach (var instance in pools)
  58. {
  59. instance.Dispose();
  60. }
  61. pools.Clear();
  62. }
  63. _pools.Clear();
  64. }
  65. private List<T[]> _pool;
  66. private int _chunkIndex = -1;
  67. private int _poolIndex = -1;
  68. private ThreadStaticPool()
  69. {
  70. _pool = new(ChunkSizeLimit * 2);
  71. AddChunkIfNeeded();
  72. }
  73. public T Allocate()
  74. {
  75. if (++_poolIndex >= PoolSizeIncrement)
  76. {
  77. AddChunkIfNeeded();
  78. _poolIndex = 0;
  79. }
  80. return _pool[_chunkIndex][_poolIndex];
  81. }
  82. private void AddChunkIfNeeded()
  83. {
  84. if (++_chunkIndex >= _pool.Count)
  85. {
  86. T[] pool = new T[PoolSizeIncrement];
  87. for (int i = 0; i < PoolSizeIncrement; i++)
  88. {
  89. pool[i] = new T();
  90. }
  91. _pool.Add(pool);
  92. }
  93. }
  94. public void Clear()
  95. {
  96. _chunkIndex = 0;
  97. _poolIndex = -1;
  98. }
  99. private void ChunkSizeLimiter()
  100. {
  101. if (_pool.Count >= ChunkSizeLimit)
  102. {
  103. int newChunkSize = ChunkSizeLimit / 2;
  104. _pool.RemoveRange(newChunkSize, _pool.Count - newChunkSize);
  105. _pool.Capacity = ChunkSizeLimit * 2;
  106. }
  107. }
  108. private void Dispose()
  109. {
  110. _pool.Clear();
  111. }
  112. }
  113. }