ArenaAllocator.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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<nint> _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 = [];
  30. _pageSize = pageSize;
  31. _pageCount = pageCount;
  32. _extras = [];
  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((nint)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. {
  71. Pointer = (byte*)NativeAllocator.Instance.Allocate(_pageSize)
  72. };
  73. _pages.Add(_page);
  74. }
  75. byte* result = _page.Pointer + _index;
  76. _index += size;
  77. return result;
  78. }
  79. public override void Free(void* block) { }
  80. public void Reset()
  81. {
  82. _index = _pageSize;
  83. _pageIndex = -1;
  84. _page = null;
  85. // Free excess pages that was allocated.
  86. while (_pages.Count > _pageCount)
  87. {
  88. NativeAllocator.Instance.Free(_pages[^1].Pointer);
  89. _pages.RemoveAt(_pages.Count - 1);
  90. }
  91. // Free extra blocks that are not page-sized
  92. foreach (nint ptr in _extras)
  93. {
  94. NativeAllocator.Instance.Free((void*)ptr);
  95. }
  96. _extras.Clear();
  97. // Free pooled pages that has not been used in a while. Remove pages at the back first, because we try to
  98. // keep the pages at the front alive, since they're more likely to be hot and in the d-cache.
  99. bool removing = true;
  100. // If arena is used frequently, keep pages for longer. Otherwise keep pages for a shorter amount of time.
  101. int now = Environment.TickCount;
  102. int count = (now - _lastReset) switch
  103. {
  104. >= 5000 => 0,
  105. >= 2500 => 50,
  106. >= 1000 => 100,
  107. >= 10 => 1500,
  108. _ => 5000,
  109. };
  110. for (int i = _pages.Count - 1; i >= 0; i--)
  111. {
  112. PageInfo page = _pages[i];
  113. if (page.Unused == 0)
  114. {
  115. page.UnusedCounter = 0;
  116. }
  117. page.UnusedCounter += page.Unused;
  118. page.Unused = 1;
  119. // If page not used after `count` resets, remove it.
  120. if (removing && page.UnusedCounter >= count)
  121. {
  122. NativeAllocator.Instance.Free(page.Pointer);
  123. _pages.RemoveAt(i);
  124. }
  125. else
  126. {
  127. removing = false;
  128. }
  129. }
  130. _lastReset = now;
  131. }
  132. protected override void Dispose(bool disposing)
  133. {
  134. if (_pages != null)
  135. {
  136. foreach (PageInfo info in _pages)
  137. {
  138. NativeAllocator.Instance.Free(info.Pointer);
  139. }
  140. foreach (nint ptr in _extras)
  141. {
  142. NativeAllocator.Instance.Free((void*)ptr);
  143. }
  144. _pages = null;
  145. }
  146. }
  147. ~ArenaAllocator()
  148. {
  149. Dispose(false);
  150. }
  151. }
  152. }