BatchStreamArrayPool.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using Avalonia.Platform;
  6. using Avalonia.Threading;
  7. // Special license applies <see href="https://raw.githubusercontent.com/AvaloniaUI/Avalonia/master/src/Avalonia.Base/Rendering/Composition/License.md">License.md</see>
  8. namespace Avalonia.Rendering.Composition.Transport;
  9. /// <summary>
  10. /// A pool that keeps a number of elements that was used in the last 10 seconds
  11. /// </summary>
  12. internal abstract class BatchStreamPoolBase<T> : IDisposable
  13. {
  14. readonly Stack<T> _pool = new();
  15. bool _disposed;
  16. int _usage;
  17. readonly int[] _usageStatistics = new int[10];
  18. int _usageStatisticsSlot;
  19. bool _reclaimImmediately;
  20. public int CurrentUsage => _usage;
  21. public int CurrentPool => _pool.Count;
  22. public BatchStreamPoolBase(bool needsFinalize, Action<Func<bool>>? startTimer = null)
  23. {
  24. if(!needsFinalize)
  25. GC.SuppressFinalize(needsFinalize);
  26. var updateRef = new WeakReference<BatchStreamPoolBase<T>>(this);
  27. if (AvaloniaLocator.Current.GetService<IPlatformThreadingInterface>() == null
  28. && AvaloniaLocator.Current.GetService<IDispatcherImpl>() == null)
  29. _reclaimImmediately = true;
  30. else
  31. StartUpdateTimer(startTimer, updateRef);
  32. }
  33. static void StartUpdateTimer(Action<Func<bool>>? startTimer, WeakReference<BatchStreamPoolBase<T>> updateRef)
  34. {
  35. Func<bool> timerProc = () =>
  36. {
  37. if (updateRef.TryGetTarget(out var target))
  38. {
  39. target.UpdateStatistics();
  40. return true;
  41. }
  42. return false;
  43. };
  44. if (startTimer != null)
  45. startTimer(timerProc);
  46. else
  47. DispatcherTimer.Run(timerProc, TimeSpan.FromSeconds(1));
  48. }
  49. private void UpdateStatistics()
  50. {
  51. lock (_pool)
  52. {
  53. var maximumUsage = _usageStatistics.Max();
  54. var recentlyUsedPooledSlots = maximumUsage - _usage;
  55. var keepSlots = Math.Max(recentlyUsedPooledSlots, 10);
  56. while (keepSlots < _pool.Count)
  57. DestroyItem(_pool.Pop());
  58. _usageStatisticsSlot = (_usageStatisticsSlot + 1) % _usageStatistics.Length;
  59. _usageStatistics[_usageStatisticsSlot] = 0;
  60. }
  61. }
  62. protected abstract T CreateItem();
  63. protected virtual void DestroyItem(T item)
  64. {
  65. }
  66. public T Get()
  67. {
  68. lock (_pool)
  69. {
  70. _usage++;
  71. if (_usageStatistics[_usageStatisticsSlot] < _usage)
  72. _usageStatistics[_usageStatisticsSlot] = _usage;
  73. if (_pool.Count != 0)
  74. return _pool.Pop();
  75. }
  76. return CreateItem();
  77. }
  78. public void Return(T item)
  79. {
  80. lock (_pool)
  81. {
  82. _usage--;
  83. if (!_disposed && !_reclaimImmediately)
  84. {
  85. _pool.Push(item);
  86. return;
  87. }
  88. }
  89. DestroyItem(item);
  90. }
  91. public void Dispose()
  92. {
  93. lock (_pool)
  94. {
  95. _disposed = true;
  96. foreach (var item in _pool)
  97. DestroyItem(item);
  98. _pool.Clear();
  99. }
  100. }
  101. ~BatchStreamPoolBase()
  102. {
  103. Dispose();
  104. }
  105. }
  106. internal sealed class BatchStreamObjectPool<T> : BatchStreamPoolBase<T[]> where T : class?
  107. {
  108. public int ArraySize { get; }
  109. public BatchStreamObjectPool(int arraySize = 128, Action<Func<bool>>? startTimer = null) : base(false, startTimer)
  110. {
  111. ArraySize = arraySize;
  112. }
  113. protected override T[] CreateItem()
  114. {
  115. return new T[ArraySize];
  116. }
  117. protected override void DestroyItem(T[] item)
  118. {
  119. Array.Clear(item, 0, item.Length);
  120. }
  121. }
  122. internal sealed class BatchStreamMemoryPool : BatchStreamPoolBase<IntPtr>
  123. {
  124. public int BufferSize { get; }
  125. public BatchStreamMemoryPool(int bufferSize = 1024, Action<Func<bool>>? startTimer = null) : base(true, startTimer)
  126. {
  127. BufferSize = bufferSize;
  128. }
  129. protected override IntPtr CreateItem() => Marshal.AllocHGlobal(BufferSize);
  130. protected override void DestroyItem(IntPtr item) => Marshal.FreeHGlobal(item);
  131. }