ArenaAllocator.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace ARMeilleure.Common
  5. {
  6. unsafe sealed class ArenaAllocator : Allocator
  7. {
  8. private class PageInfo
  9. {
  10. public byte* Pointer;
  11. public byte Unused;
  12. public int UnusedCounter;
  13. }
  14. private int _lastReset;
  15. private ulong _index;
  16. private int _pageIndex;
  17. private PageInfo _page;
  18. private List<PageInfo> _pages;
  19. private readonly ulong _pageSize;
  20. private readonly uint _pageCount;
  21. private readonly List<IntPtr> _extras;
  22. public ArenaAllocator(uint pageSize, uint pageCount)
  23. {
  24. _lastReset = Environment.TickCount;
  25. // Set _index to pageSize so that the first allocation goes through the slow path.
  26. _index = pageSize;
  27. _pageIndex = -1;
  28. _page = null;
  29. _pages = new List<PageInfo>();
  30. _pageSize = pageSize;
  31. _pageCount = pageCount;
  32. _extras = new List<IntPtr>();
  33. }
  34. public Span<T> AllocateSpan<T>(ulong count) where T : unmanaged
  35. {
  36. return new Span<T>(Allocate<T>(count), (int)count);
  37. }
  38. public override void* Allocate(ulong size)
  39. {
  40. if (_index + size <= _pageSize)
  41. {
  42. byte* result = _page.Pointer + _index;
  43. _index += size;
  44. return result;
  45. }
  46. return AllocateSlow(size);
  47. }
  48. [MethodImpl(MethodImplOptions.NoInlining)]
  49. private void* AllocateSlow(ulong size)
  50. {
  51. if (size > _pageSize)
  52. {
  53. void* extra = NativeAllocator.Instance.Allocate(size);
  54. _extras.Add((IntPtr)extra);
  55. return extra;
  56. }
  57. if (_index + size > _pageSize)
  58. {
  59. _index = 0;
  60. _pageIndex++;
  61. }
  62. if (_pageIndex < _pages.Count)
  63. {
  64. _page = _pages[_pageIndex];
  65. _page.Unused = 0;
  66. }
  67. else
  68. {
  69. _page = new PageInfo();
  70. _page.Pointer = (byte*)NativeAllocator.Instance.Allocate(_pageSize);
  71. _pages.Add(_page);
  72. }
  73. byte* result = _page.Pointer + _index;
  74. _index += size;
  75. return result;
  76. }
  77. public override void Free(void* block) { }
  78. public void Reset()
  79. {
  80. _index = _pageSize;
  81. _pageIndex = -1;
  82. _page = null;
  83. // Free excess pages that was allocated.
  84. while (_pages.Count > _pageCount)
  85. {
  86. NativeAllocator.Instance.Free(_pages[_pages.Count - 1].Pointer);
  87. _pages.RemoveAt(_pages.Count - 1);
  88. }
  89. // Free extra blocks that are not page-sized
  90. foreach (IntPtr ptr in _extras)
  91. {
  92. NativeAllocator.Instance.Free((void*)ptr);
  93. }
  94. _extras.Clear();
  95. // Free pooled pages that has not been used in a while. Remove pages at the back first, because we try to
  96. // keep the pages at the front alive, since they're more likely to be hot and in the d-cache.
  97. bool removing = true;
  98. // If arena is used frequently, keep pages for longer. Otherwise keep pages for a shorter amount of time.
  99. int now = Environment.TickCount;
  100. int count = (now - _lastReset) switch {
  101. >= 5000 => 0,
  102. >= 2500 => 50,
  103. >= 1000 => 100,
  104. >= 10 => 1500,
  105. _ => 5000
  106. };
  107. for (int i = _pages.Count - 1; i >= 0; i--)
  108. {
  109. PageInfo page = _pages[i];
  110. if (page.Unused == 0)
  111. {
  112. page.UnusedCounter = 0;
  113. }
  114. page.UnusedCounter += page.Unused;
  115. page.Unused = 1;
  116. // If page not used after `count` resets, remove it.
  117. if (removing && page.UnusedCounter >= count)
  118. {
  119. NativeAllocator.Instance.Free(page.Pointer);
  120. _pages.RemoveAt(i);
  121. }
  122. else
  123. {
  124. removing = false;
  125. }
  126. }
  127. _lastReset = now;
  128. }
  129. protected override void Dispose(bool disposing)
  130. {
  131. if (_pages != null)
  132. {
  133. foreach (PageInfo info in _pages)
  134. {
  135. NativeAllocator.Instance.Free(info.Pointer);
  136. }
  137. foreach (IntPtr ptr in _extras)
  138. {
  139. NativeAllocator.Instance.Free((void*)ptr);
  140. }
  141. _pages = null;
  142. }
  143. }
  144. ~ArenaAllocator()
  145. {
  146. Dispose(false);
  147. }
  148. }
  149. }