ObjectPool.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #pragma warning disable CA1822
  2. #nullable disable
  3. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  4. // define TRACE_LEAKS to get additional diagnostics that can lead to the leak sources. note: it will
  5. // make everything about 2-3x slower
  6. //
  7. // #define TRACE_LEAKS
  8. // define DETECT_LEAKS to detect possible leaks
  9. // #if DEBUG
  10. // #define DETECT_LEAKS //for now always enable DETECT_LEAKS in debug.
  11. // #endif
  12. using System.Diagnostics;
  13. #if DETECT_LEAKS
  14. using System.Runtime.CompilerServices;
  15. #endif
  16. namespace Jint.Pooling;
  17. /// <summary>
  18. /// Generic implementation of object pooling pattern with predefined pool size limit. The main
  19. /// purpose is that limited number of frequently used objects can be kept in the pool for
  20. /// further recycling.
  21. ///
  22. /// Notes:
  23. /// 1) it is not the goal to keep all returned objects. Pool is not meant for storage. If there
  24. /// is no space in the pool, extra returned objects will be dropped.
  25. ///
  26. /// 2) it is implied that if object was obtained from a pool, the caller will return it back in
  27. /// a relatively short time. Keeping checked out objects for long durations is ok, but
  28. /// reduces usefulness of pooling. Just new up your own.
  29. ///
  30. /// Not returning objects to the pool in not detrimental to the pool's work, but is a bad practice.
  31. /// Rationale:
  32. /// If there is no intent for reusing the object, do not use pool - just use "new".
  33. /// </summary>
  34. internal sealed class ObjectPool<T> where T : class
  35. {
  36. [DebuggerDisplay("{Value,nq}")]
  37. private struct Element
  38. {
  39. internal T Value;
  40. }
  41. /// <remarks>
  42. /// Not using System.Func{T} because this file is linked into the (debugger) Formatter,
  43. /// which does not have that type (since it compiles against .NET 2.0).
  44. /// </remarks>
  45. internal delegate T Factory();
  46. // Storage for the pool objects. The first item is stored in a dedicated field because we
  47. // expect to be able to satisfy most requests from it.
  48. private T _firstItem;
  49. private readonly Element[] _items;
  50. // factory is stored for the lifetime of the pool. We will call this only when pool needs to
  51. // expand. compared to "new T()", Func gives more flexibility to implementers and faster
  52. // than "new T()".
  53. private readonly Factory _factory;
  54. #if DETECT_LEAKS
  55. private static readonly ConditionalWeakTable<T, LeakTracker> leakTrackers = new ConditionalWeakTable<T, LeakTracker>();
  56. private class LeakTracker : IDisposable
  57. {
  58. private volatile bool disposed;
  59. #if TRACE_LEAKS
  60. internal volatile object Trace = null;
  61. #endif
  62. public void Dispose()
  63. {
  64. disposed = true;
  65. GC.SuppressFinalize(this);
  66. }
  67. private string GetTrace()
  68. {
  69. #if TRACE_LEAKS
  70. return Trace == null ? "" : Trace.ToString();
  71. #else
  72. return "Leak tracing information is disabled. Define TRACE_LEAKS on ObjectPool`1.cs to get more info \n";
  73. #endif
  74. }
  75. ~LeakTracker()
  76. {
  77. if (!this.disposed && !Environment.HasShutdownStarted)
  78. {
  79. var trace = GetTrace();
  80. // If you are seeing this message it means that object has been allocated from the pool
  81. // and has not been returned back. This is not critical, but turns pool into rather
  82. // inefficient kind of "new".
  83. Debug.WriteLine($"TRACEOBJECTPOOLLEAKS_BEGIN\nPool detected potential leaking of {typeof(T)}. \n Location of the leak: \n {GetTrace()} TRACEOBJECTPOOLLEAKS_END");
  84. }
  85. }
  86. }
  87. #endif
  88. internal ObjectPool(Factory factory)
  89. : this(factory, Environment.ProcessorCount * 2)
  90. { }
  91. internal ObjectPool(Factory factory, int size)
  92. {
  93. Debug.Assert(size >= 1);
  94. _factory = factory;
  95. _items = new Element[size - 1];
  96. }
  97. private T CreateInstance()
  98. {
  99. var inst = _factory();
  100. return inst;
  101. }
  102. /// <summary>
  103. /// Produces an instance.
  104. /// </summary>
  105. /// <remarks>
  106. /// Search strategy is a simple linear probing which is chosen for it cache-friendliness.
  107. /// Note that Free will try to store recycled objects close to the start thus statistically
  108. /// reducing how far we will typically search.
  109. /// </remarks>
  110. internal T Allocate()
  111. {
  112. // PERF: Examine the first element. If that fails, AllocateSlow will look at the remaining elements.
  113. // Note that the initial read is optimistically not synchronized. That is intentional.
  114. // We will interlock only when we have a candidate. in a worst case we may miss some
  115. // recently returned objects. Not a big deal.
  116. T inst = _firstItem;
  117. if (inst is not null)
  118. {
  119. _firstItem = null;
  120. return inst;
  121. }
  122. inst = AllocateSlow();
  123. #if DETECT_LEAKS
  124. var tracker = new LeakTracker();
  125. leakTrackers.Add(inst, tracker);
  126. #if TRACE_LEAKS
  127. var frame = CaptureStackTrace();
  128. tracker.Trace = frame;
  129. #endif
  130. #endif
  131. return inst;
  132. }
  133. private T AllocateSlow()
  134. {
  135. var items = _items;
  136. for (int i = 0; i < items.Length; i++)
  137. {
  138. T inst = items[i].Value;
  139. if (inst is not null)
  140. {
  141. items[i].Value = null;
  142. return inst;
  143. }
  144. }
  145. return CreateInstance();
  146. }
  147. /// <summary>
  148. /// Returns objects to the pool.
  149. /// </summary>
  150. /// <remarks>
  151. /// Search strategy is a simple linear probing which is chosen for it cache-friendliness.
  152. /// Note that Free will try to store recycled objects close to the start thus statistically
  153. /// reducing how far we will typically search in Allocate.
  154. /// </remarks>
  155. internal void Free(T obj)
  156. {
  157. Validate(obj);
  158. ForgetTrackedObject(obj);
  159. if (_firstItem is null)
  160. {
  161. // Intentionally not using interlocked here.
  162. // In a worst case scenario two objects may be stored into same slot.
  163. // It is very unlikely to happen and will only mean that one of the objects will get collected.
  164. _firstItem = obj;
  165. }
  166. else
  167. {
  168. FreeSlow(obj);
  169. }
  170. }
  171. private void FreeSlow(T obj)
  172. {
  173. var items = _items;
  174. for (int i = 0; i < items.Length; i++)
  175. {
  176. if (ReferenceEquals(items[i].Value, null))
  177. {
  178. // Intentionally not using interlocked here.
  179. // In a worst case scenario two objects may be stored into same slot.
  180. // It is very unlikely to happen and will only mean that one of the objects will get collected.
  181. items[i].Value = obj;
  182. break;
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// Removes an object from leak tracking.
  188. ///
  189. /// This is called when an object is returned to the pool. It may also be explicitly
  190. /// called if an object allocated from the pool is intentionally not being returned
  191. /// to the pool. This can be of use with pooled arrays if the consumer wants to
  192. /// return a larger array to the pool than was originally allocated.
  193. /// </summary>
  194. [Conditional("DEBUG")]
  195. internal void ForgetTrackedObject(T old, T replacement = null)
  196. {
  197. #if DETECT_LEAKS
  198. LeakTracker tracker;
  199. if (leakTrackers.TryGetValue(old, out tracker))
  200. {
  201. tracker.Dispose();
  202. leakTrackers.Remove(old);
  203. }
  204. else
  205. {
  206. var trace = CaptureStackTrace();
  207. Debug.WriteLine($"TRACEOBJECTPOOLLEAKS_BEGIN\nObject of type {typeof(T)} was freed, but was not from pool. \n Callstack: \n {trace} TRACEOBJECTPOOLLEAKS_END");
  208. }
  209. if (replacement != null)
  210. {
  211. tracker = new LeakTracker();
  212. leakTrackers.Add(replacement, tracker);
  213. }
  214. #endif
  215. }
  216. #if DETECT_LEAKS
  217. private static Lazy<Type> _stackTraceType = new Lazy<Type>(() => Type.GetType("System.Diagnostics.StackTrace"));
  218. private static object CaptureStackTrace()
  219. {
  220. return Activator.CreateInstance(_stackTraceType.Value);
  221. }
  222. #endif
  223. [Conditional("DEBUG")]
  224. private void Validate(object obj)
  225. {
  226. Debug.Assert(obj != null, "freeing null?");
  227. Debug.Assert(_firstItem != obj, "freeing twice?");
  228. var items = _items;
  229. for (int i = 0; i < items.Length; i++)
  230. {
  231. var value = items[i].Value;
  232. if (value is null)
  233. {
  234. return;
  235. }
  236. Debug.Assert(value != obj, "freeing twice?");
  237. }
  238. }
  239. }