ThreadStaticPool.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using ARMeilleure.Translation.PTC;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. namespace ARMeilleure.Common
  6. {
  7. class ThreadStaticPool<T> where T : class, new()
  8. {
  9. [ThreadStatic]
  10. private static ThreadStaticPool<T> _instance;
  11. public static ThreadStaticPool<T> Instance
  12. {
  13. get
  14. {
  15. if (_instance == null)
  16. {
  17. PreparePool(); // So that we can still use a pool when blindly initializing one.
  18. }
  19. return _instance;
  20. }
  21. }
  22. private static readonly ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new();
  23. private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
  24. {
  25. return _pools.GetOrAdd(groupId, (groupId) => new());
  26. }
  27. public static void PreparePool(
  28. int groupId = 0,
  29. ChunkSizeLimit chunkSizeLimit = ChunkSizeLimit.Large,
  30. PoolSizeIncrement poolSizeIncrement = PoolSizeIncrement.Default)
  31. {
  32. if (Ptc.State == PtcState.Disabled)
  33. {
  34. PreparePoolDefault(groupId, (int)chunkSizeLimit, (int)poolSizeIncrement);
  35. }
  36. else
  37. {
  38. PreparePoolSlim((int)chunkSizeLimit, (int)poolSizeIncrement);
  39. }
  40. }
  41. private static void PreparePoolDefault(int groupId, int chunkSizeLimit, int poolSizeIncrement)
  42. {
  43. // Prepare the pool for this thread, ideally using an existing one from the specified group.
  44. if (_instance == null)
  45. {
  46. var pools = GetPools(groupId);
  47. lock (pools)
  48. {
  49. _instance = (pools.Count != 0) ? pools.Pop() : new(chunkSizeLimit, poolSizeIncrement);
  50. }
  51. }
  52. }
  53. private static void PreparePoolSlim(int chunkSizeLimit, int poolSizeIncrement)
  54. {
  55. // Prepare the pool for this thread.
  56. if (_instance == null)
  57. {
  58. _instance = new(chunkSizeLimit, poolSizeIncrement);
  59. }
  60. }
  61. public static void ResetPool(int groupId = 0)
  62. {
  63. if (Ptc.State == PtcState.Disabled)
  64. {
  65. ResetPoolDefault(groupId);
  66. }
  67. else
  68. {
  69. ResetPoolSlim();
  70. }
  71. }
  72. private static void ResetPoolDefault(int groupId)
  73. {
  74. // Reset, limit if necessary, and return the pool for this thread to the specified group.
  75. if (_instance != null)
  76. {
  77. var pools = GetPools(groupId);
  78. lock (pools)
  79. {
  80. _instance.Clear();
  81. _instance.ChunkSizeLimiter();
  82. pools.Push(_instance);
  83. _instance = null;
  84. }
  85. }
  86. }
  87. private static void ResetPoolSlim()
  88. {
  89. // Reset, limit if necessary, the pool for this thread.
  90. if (_instance != null)
  91. {
  92. _instance.Clear();
  93. _instance.ChunkSizeLimiter();
  94. }
  95. }
  96. public static void DisposePools()
  97. {
  98. if (Ptc.State == PtcState.Disabled)
  99. {
  100. DisposePoolsDefault();
  101. }
  102. else
  103. {
  104. DisposePoolSlim();
  105. }
  106. }
  107. private static void DisposePoolsDefault()
  108. {
  109. // Resets any static references to the pools used by threads for each group, allowing them to be garbage collected.
  110. foreach (var pools in _pools.Values)
  111. {
  112. foreach (var instance in pools)
  113. {
  114. instance.Dispose();
  115. }
  116. pools.Clear();
  117. }
  118. _pools.Clear();
  119. }
  120. private static void DisposePoolSlim()
  121. {
  122. // Dispose the pool for this thread.
  123. if (_instance != null)
  124. {
  125. _instance.Dispose();
  126. _instance = null;
  127. }
  128. }
  129. private List<T[]> _pool;
  130. private int _chunkIndex = -1;
  131. private int _poolIndex = -1;
  132. private int _chunkSizeLimit;
  133. private int _poolSizeIncrement;
  134. private ThreadStaticPool(int chunkSizeLimit, int poolSizeIncrement)
  135. {
  136. _chunkSizeLimit = chunkSizeLimit;
  137. _poolSizeIncrement = poolSizeIncrement;
  138. _pool = new(chunkSizeLimit * 2);
  139. AddChunkIfNeeded();
  140. }
  141. public T Allocate()
  142. {
  143. if (++_poolIndex >= _poolSizeIncrement)
  144. {
  145. AddChunkIfNeeded();
  146. _poolIndex = 0;
  147. }
  148. return _pool[_chunkIndex][_poolIndex];
  149. }
  150. private void AddChunkIfNeeded()
  151. {
  152. if (++_chunkIndex >= _pool.Count)
  153. {
  154. T[] pool = new T[_poolSizeIncrement];
  155. for (int i = 0; i < _poolSizeIncrement; i++)
  156. {
  157. pool[i] = new T();
  158. }
  159. _pool.Add(pool);
  160. }
  161. }
  162. public void Clear()
  163. {
  164. _chunkIndex = 0;
  165. _poolIndex = -1;
  166. }
  167. private void ChunkSizeLimiter()
  168. {
  169. if (_pool.Count >= _chunkSizeLimit)
  170. {
  171. int newChunkSize = _chunkSizeLimit / 2;
  172. _pool.RemoveRange(newChunkSize, _pool.Count - newChunkSize);
  173. _pool.Capacity = _chunkSizeLimit * 2;
  174. }
  175. }
  176. private void Dispose()
  177. {
  178. _pool = null;
  179. }
  180. }
  181. }