PartitionedHashTable.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace Ryujinx.Graphics.Gpu.Shader.HashTable
  5. {
  6. /// <summary>
  7. /// Partitioned hash table.
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. public class PartitionedHashTable<T>
  11. {
  12. /// <summary>
  13. /// Entry for a given data size.
  14. /// </summary>
  15. private readonly struct SizeEntry
  16. {
  17. /// <summary>
  18. /// Size for the data that will be stored on the hash table on this entry.
  19. /// </summary>
  20. public int Size { get; }
  21. /// <summary>
  22. /// Number of entries on the hash table.
  23. /// </summary>
  24. public int TableCount => _table.Count;
  25. private readonly PartitionHashTable<T> _table;
  26. /// <summary>
  27. /// Creates an entry for a given size.
  28. /// </summary>
  29. /// <param name="size">Size of the data to be stored on this entry</param>
  30. public SizeEntry(int size)
  31. {
  32. Size = size;
  33. _table = new PartitionHashTable<T>();
  34. }
  35. /// <summary>
  36. /// Gets an item for existing data, or adds a new one.
  37. /// </summary>
  38. /// <param name="data">Data associated with the item</param>
  39. /// <param name="dataHash">Hash of <paramref name="data"/></param>
  40. /// <param name="item">Item to be added</param>
  41. /// <returns>Existing item, or <paramref name="item"/> if not present</returns>
  42. public T GetOrAdd(byte[] data, uint dataHash, T item)
  43. {
  44. Debug.Assert(data.Length == Size);
  45. return _table.GetOrAdd(data, dataHash, item);
  46. }
  47. /// <summary>
  48. /// Adds a new item.
  49. /// </summary>
  50. /// <param name="data">Data associated with the item</param>
  51. /// <param name="dataHash">Hash of <paramref name="data"/></param>
  52. /// <param name="item">Item to be added</param>
  53. /// <returns>True if added, false otherwise</returns>
  54. public bool Add(byte[] data, uint dataHash, T item)
  55. {
  56. Debug.Assert(data.Length == Size);
  57. return _table.Add(data, dataHash, item);
  58. }
  59. /// <summary>
  60. /// Adds a partial entry.
  61. /// </summary>
  62. /// <param name="ownerData">Full entry data</param>
  63. /// <param name="dataHash">Hash of the sub-region of the data that belongs to this entry</param>
  64. /// <returns>True if added, false otherwise</returns>
  65. public bool AddPartial(byte[] ownerData, uint dataHash)
  66. {
  67. return _table.AddPartial(ownerData, dataHash, Size);
  68. }
  69. /// <summary>
  70. /// Fills a new hash table with "partials" of existing full entries of higher size.
  71. /// </summary>
  72. /// <param name="newEntry">Entry with the new hash table</param>
  73. public void FillPartials(SizeEntry newEntry)
  74. {
  75. Debug.Assert(newEntry.Size < Size);
  76. _table.FillPartials(newEntry._table, newEntry.Size);
  77. }
  78. /// <summary>
  79. /// Tries to find an item on the hash table.
  80. /// </summary>
  81. /// <param name="dataAccessor">Data accessor</param>
  82. /// <param name="item">The item on the table, if found, otherwise unmodified</param>
  83. /// <param name="data">The data on the table, if found, otherwise unmodified</param>
  84. /// <returns>Table lookup result</returns>
  85. public PartitionHashTable<T>.SearchResult TryFindItem(scoped ref SmartDataAccessor dataAccessor, scoped ref T item, scoped ref byte[] data)
  86. {
  87. return _table.TryFindItem(ref dataAccessor, Size, ref item, ref data);
  88. }
  89. }
  90. private readonly List<SizeEntry> _sizeTable;
  91. /// <summary>
  92. /// Creates a new partitioned hash table.
  93. /// </summary>
  94. public PartitionedHashTable()
  95. {
  96. _sizeTable = new List<SizeEntry>();
  97. }
  98. /// <summary>
  99. /// Adds a new item to the table.
  100. /// </summary>
  101. /// <param name="data">Data</param>
  102. /// <param name="item">Item associated with the data</param>
  103. public void Add(byte[] data, T item)
  104. {
  105. GetOrAdd(data, item);
  106. }
  107. /// <summary>
  108. /// Gets an existing item from the table, or adds a new one if not present.
  109. /// </summary>
  110. /// <param name="data">Data</param>
  111. /// <param name="item">Item associated with the data</param>
  112. /// <returns>Existing item, or <paramref name="item"/> if not present</returns>
  113. public T GetOrAdd(byte[] data, T item)
  114. {
  115. SizeEntry sizeEntry;
  116. int index = BinarySearch(_sizeTable, data.Length);
  117. if (index < _sizeTable.Count && _sizeTable[index].Size == data.Length)
  118. {
  119. sizeEntry = _sizeTable[index];
  120. }
  121. else
  122. {
  123. if (index < _sizeTable.Count && _sizeTable[index].Size < data.Length)
  124. {
  125. index++;
  126. }
  127. sizeEntry = new SizeEntry(data.Length);
  128. _sizeTable.Insert(index, sizeEntry);
  129. for (int i = index + 1; i < _sizeTable.Count; i++)
  130. {
  131. _sizeTable[i].FillPartials(sizeEntry);
  132. }
  133. }
  134. HashState hashState = new HashState();
  135. hashState.Initialize();
  136. for (int i = 0; i < index; i++)
  137. {
  138. ReadOnlySpan<byte> dataSlice = new ReadOnlySpan<byte>(data).Slice(0, _sizeTable[i].Size);
  139. hashState.Continue(dataSlice);
  140. _sizeTable[i].AddPartial(data, hashState.Finalize(dataSlice));
  141. }
  142. hashState.Continue(data);
  143. return sizeEntry.GetOrAdd(data, hashState.Finalize(data), item);
  144. }
  145. /// <summary>
  146. /// Performs binary search on a list of hash tables, each one with a fixed data size.
  147. /// </summary>
  148. /// <param name="entries">List of hash tables</param>
  149. /// <param name="size">Size to search for</param>
  150. /// <returns>Index of the hash table with the given size, or nearest one otherwise</returns>
  151. private static int BinarySearch(List<SizeEntry> entries, int size)
  152. {
  153. int left = 0;
  154. int middle = 0;
  155. int right = entries.Count - 1;
  156. while (left <= right)
  157. {
  158. middle = left + ((right - left) >> 1);
  159. SizeEntry entry = entries[middle];
  160. if (size == entry.Size)
  161. {
  162. break;
  163. }
  164. if (size < entry.Size)
  165. {
  166. right = middle - 1;
  167. }
  168. else
  169. {
  170. left = middle + 1;
  171. }
  172. }
  173. return middle;
  174. }
  175. /// <summary>
  176. /// Tries to find an item on the table.
  177. /// </summary>
  178. /// <param name="dataAccessor">Data accessor</param>
  179. /// <param name="item">Item, if found</param>
  180. /// <param name="data">Data, if found</param>
  181. /// <returns>True if the item was found on the table, false otherwise</returns>
  182. public bool TryFindItem(IDataAccessor dataAccessor, out T item, out byte[] data)
  183. {
  184. SmartDataAccessor sda = new SmartDataAccessor(dataAccessor);
  185. item = default;
  186. data = null;
  187. int left = 0;
  188. int right = _sizeTable.Count;
  189. while (left != right)
  190. {
  191. int index = left + ((right - left) >> 1);
  192. PartitionHashTable<T>.SearchResult result = _sizeTable[index].TryFindItem(ref sda, ref item, ref data);
  193. if (result == PartitionHashTable<T>.SearchResult.FoundFull)
  194. {
  195. return true;
  196. }
  197. if (result == PartitionHashTable<T>.SearchResult.NotFound)
  198. {
  199. right = index;
  200. }
  201. else /* if (result == PartitionHashTable<T>.SearchResult.FoundPartial) */
  202. {
  203. left = index + 1;
  204. }
  205. }
  206. data = null;
  207. return false;
  208. }
  209. }
  210. }