PartitionHashTable.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. namespace Ryujinx.Graphics.Gpu.Shader.HashTable
  6. {
  7. /// <summary>
  8. /// Partitioned hash table.
  9. /// </summary>
  10. /// <typeparam name="T">Hash table entry type</typeparam>
  11. class PartitionHashTable<T>
  12. {
  13. /// <summary>
  14. /// Hash table entry.
  15. /// </summary>
  16. private struct Entry
  17. {
  18. /// <summary>
  19. /// Hash <see cref="OwnSize"/> bytes of <see cref="Data"/>.
  20. /// </summary>
  21. public readonly uint Hash;
  22. /// <summary>
  23. /// If this entry is only a sub-region of <see cref="Data"/>, this indicates the size in bytes
  24. /// of that region. Otherwise, it should be zero.
  25. /// </summary>
  26. public readonly int OwnSize;
  27. /// <summary>
  28. /// Data used to compute the hash for this entry.
  29. /// </summary>
  30. /// <remarks>
  31. /// To avoid additional allocations, this might be a instance of the full entry data,
  32. /// and only a sub-region of it might be actually used by this entry. Such sub-region
  33. /// has its size indicated by <see cref="OwnSize"/> in this case.
  34. /// </remarks>
  35. public readonly byte[] Data;
  36. /// <summary>
  37. /// Item associated with this entry.
  38. /// </summary>
  39. public T Item;
  40. /// <summary>
  41. /// Indicates if the entry is partial, which means that this entry is only for a sub-region of the data.
  42. /// </summary>
  43. /// <remarks>
  44. /// Partial entries have no items associated with them. They just indicates that the data might be present on
  45. /// the table, and one must keep looking for the full entry on other tables of larger data size.
  46. /// </remarks>
  47. public bool IsPartial => OwnSize != 0;
  48. /// <summary>
  49. /// Creates a new partial hash table entry.
  50. /// </summary>
  51. /// <param name="hash">Hash of the data</param>
  52. /// <param name="ownerData">Full data</param>
  53. /// <param name="ownSize">Size of the sub-region of data that belongs to this entry</param>
  54. public Entry(uint hash, byte[] ownerData, int ownSize)
  55. {
  56. Hash = hash;
  57. OwnSize = ownSize;
  58. Data = ownerData;
  59. Item = default;
  60. }
  61. /// <summary>
  62. /// Creates a new full hash table entry.
  63. /// </summary>
  64. /// <param name="hash">Hash of the data</param>
  65. /// <param name="data">Data</param>
  66. /// <param name="item">Item associated with this entry</param>
  67. public Entry(uint hash, byte[] data, T item)
  68. {
  69. Hash = hash;
  70. OwnSize = 0;
  71. Data = data;
  72. Item = item;
  73. }
  74. /// <summary>
  75. /// Gets the data for this entry, either full or partial.
  76. /// </summary>
  77. /// <returns>Data sub-region</returns>
  78. public ReadOnlySpan<byte> GetData()
  79. {
  80. if (OwnSize != 0)
  81. {
  82. return new ReadOnlySpan<byte>(Data).Slice(0, OwnSize);
  83. }
  84. return Data;
  85. }
  86. }
  87. /// <summary>
  88. /// Hash table bucket.
  89. /// </summary>
  90. private struct Bucket
  91. {
  92. /// <summary>
  93. /// Inline entry, to avoid allocations for the common single entry case.
  94. /// </summary>
  95. public Entry InlineEntry;
  96. /// <summary>
  97. /// List of additional entries for the not-so-common multiple entries case.
  98. /// </summary>
  99. public List<Entry> MoreEntries;
  100. }
  101. private Bucket[] _buckets;
  102. private int _count;
  103. /// <summary>
  104. /// Total amount of entries on the hash table.
  105. /// </summary>
  106. public int Count => _count;
  107. /// <summary>
  108. /// Creates a new instance of the partitioned hash table.
  109. /// </summary>
  110. public PartitionHashTable()
  111. {
  112. _buckets = Array.Empty<Bucket>();
  113. }
  114. /// <summary>
  115. /// Gets an item on the table, or adds a new one if not present.
  116. /// </summary>
  117. /// <param name="data">Data</param>
  118. /// <param name="dataHash">Hash of the data</param>
  119. /// <param name="item">Item to be added if not found</param>
  120. /// <returns>Existing item if found, or <paramref name="item"/> if not found</returns>
  121. public T GetOrAdd(byte[] data, uint dataHash, T item)
  122. {
  123. if (TryFindItem(dataHash, data, out T existingItem))
  124. {
  125. return existingItem;
  126. }
  127. Entry entry = new Entry(dataHash, data, item);
  128. AddToBucket(dataHash, ref entry);
  129. return item;
  130. }
  131. /// <summary>
  132. /// Adds an item to the hash table.
  133. /// </summary>
  134. /// <param name="data">Data</param>
  135. /// <param name="dataHash">Hash of the data</param>
  136. /// <param name="item">Item to be added</param>
  137. /// <returns>True if the item was added, false due to an item associated with the data already being on the table</returns>
  138. public bool Add(byte[] data, uint dataHash, T item)
  139. {
  140. if (TryFindItem(dataHash, data, out _))
  141. {
  142. return false;
  143. }
  144. Entry entry = new Entry(dataHash, data, item);
  145. AddToBucket(dataHash, ref entry);
  146. return true;
  147. }
  148. /// <summary>
  149. /// Adds a partial entry to the hash table.
  150. /// </summary>
  151. /// <param name="ownerData">Full data</param>
  152. /// <param name="ownSize">Size of the sub-region of <paramref name="ownerData"/> used by the partial entry</param>
  153. /// <returns>True if added, false otherwise</returns>
  154. public bool AddPartial(byte[] ownerData, int ownSize)
  155. {
  156. ReadOnlySpan<byte> data = new ReadOnlySpan<byte>(ownerData).Slice(0, ownSize);
  157. return AddPartial(ownerData, HashState.CalcHash(data), ownSize);
  158. }
  159. /// <summary>
  160. /// Adds a partial entry to the hash table.
  161. /// </summary>
  162. /// <param name="ownerData">Full data</param>
  163. /// <param name="dataHash">Hash of the data sub-region</param>
  164. /// <param name="ownSize">Size of the sub-region of <paramref name="ownerData"/> used by the partial entry</param>
  165. /// <returns>True if added, false otherwise</returns>
  166. public bool AddPartial(byte[] ownerData, uint dataHash, int ownSize)
  167. {
  168. ReadOnlySpan<byte> data = new ReadOnlySpan<byte>(ownerData).Slice(0, ownSize);
  169. if (TryFindItem(dataHash, data, out _))
  170. {
  171. return false;
  172. }
  173. Entry entry = new Entry(dataHash, ownerData, ownSize);
  174. AddToBucket(dataHash, ref entry);
  175. return true;
  176. }
  177. /// <summary>
  178. /// Adds entry with a given hash to the table.
  179. /// </summary>
  180. /// <param name="dataHash">Hash of the entry</param>
  181. /// <param name="entry">Entry</param>
  182. private void AddToBucket(uint dataHash, ref Entry entry)
  183. {
  184. int pow2Count = GetPow2Count(++_count);
  185. if (pow2Count != _buckets.Length)
  186. {
  187. Rebuild(pow2Count);
  188. }
  189. ref Bucket bucket = ref GetBucketForHash(dataHash);
  190. AddToBucket(ref bucket, ref entry);
  191. }
  192. /// <summary>
  193. /// Adds an entry to a bucket.
  194. /// </summary>
  195. /// <param name="bucket">Bucket to add the entry into</param>
  196. /// <param name="entry">Entry to be added</param>
  197. private void AddToBucket(ref Bucket bucket, ref Entry entry)
  198. {
  199. if (bucket.InlineEntry.Data == null)
  200. {
  201. bucket.InlineEntry = entry;
  202. }
  203. else
  204. {
  205. (bucket.MoreEntries ??= new List<Entry>()).Add(entry);
  206. }
  207. }
  208. /// <summary>
  209. /// Creates partial entries on a new hash table for all existing full entries.
  210. /// </summary>
  211. /// <remarks>
  212. /// This should be called every time a new hash table is created, and there are hash
  213. /// tables with data sizes that are higher than that of the new table.
  214. /// This will then fill the new hash table with "partial" entries of full entries
  215. /// on the hash tables with higher size.
  216. /// </remarks>
  217. /// <param name="newTable">New hash table</param>
  218. /// <param name="newEntrySize">Size of the data on the new hash table</param>
  219. public void FillPartials(PartitionHashTable<T> newTable, int newEntrySize)
  220. {
  221. for (int i = 0; i < _buckets.Length; i++)
  222. {
  223. ref Bucket bucket = ref _buckets[i];
  224. ref Entry inlineEntry = ref bucket.InlineEntry;
  225. if (inlineEntry.Data != null)
  226. {
  227. if (!inlineEntry.IsPartial)
  228. {
  229. newTable.AddPartial(inlineEntry.Data, newEntrySize);
  230. }
  231. if (bucket.MoreEntries != null)
  232. {
  233. foreach (Entry entry in bucket.MoreEntries)
  234. {
  235. if (entry.IsPartial)
  236. {
  237. continue;
  238. }
  239. newTable.AddPartial(entry.Data, newEntrySize);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// Tries to find an item on the table.
  247. /// </summary>
  248. /// <param name="dataHash">Hash of <paramref name="data"/></param>
  249. /// <param name="data">Data to find</param>
  250. /// <param name="item">Item associated with the data</param>
  251. /// <returns>True if an item was found, false otherwise</returns>
  252. private bool TryFindItem(uint dataHash, ReadOnlySpan<byte> data, out T item)
  253. {
  254. if (_count == 0)
  255. {
  256. item = default;
  257. return false;
  258. }
  259. ref Bucket bucket = ref GetBucketForHash(dataHash);
  260. if (bucket.InlineEntry.Data != null)
  261. {
  262. if (bucket.InlineEntry.Hash == dataHash && bucket.InlineEntry.GetData().SequenceEqual(data))
  263. {
  264. item = bucket.InlineEntry.Item;
  265. return true;
  266. }
  267. if (bucket.MoreEntries != null)
  268. {
  269. foreach (Entry entry in bucket.MoreEntries)
  270. {
  271. if (entry.Hash == dataHash && entry.GetData().SequenceEqual(data))
  272. {
  273. item = entry.Item;
  274. return true;
  275. }
  276. }
  277. }
  278. }
  279. item = default;
  280. return false;
  281. }
  282. /// <summary>
  283. /// Indicates the result of a hash table lookup.
  284. /// </summary>
  285. public enum SearchResult
  286. {
  287. /// <summary>
  288. /// No entry was found, the search must continue on hash tables of lower size.
  289. /// </summary>
  290. NotFound,
  291. /// <summary>
  292. /// A partial entry was found, the search must continue on hash tables of higher size.
  293. /// </summary>
  294. FoundPartial,
  295. /// <summary>
  296. /// A full entry was found, the search was concluded and the item can be retrieved.
  297. /// </summary>
  298. FoundFull
  299. }
  300. /// <summary>
  301. /// Tries to find an item on the table.
  302. /// </summary>
  303. /// <param name="dataAccessor">Data accessor</param>
  304. /// <param name="size">Size of the hash table data</param>
  305. /// <param name="item">The item on the table, if found, otherwise unmodified</param>
  306. /// <param name="data">The data on the table, if found, otherwise unmodified</param>
  307. /// <returns>Table lookup result</returns>
  308. public SearchResult TryFindItem(ref SmartDataAccessor dataAccessor, int size, ref T item, ref byte[] data)
  309. {
  310. if (_count == 0)
  311. {
  312. return SearchResult.NotFound;
  313. }
  314. ReadOnlySpan<byte> dataSpan = dataAccessor.GetSpanAndHash(size, out uint dataHash);
  315. if (dataSpan.Length != size)
  316. {
  317. return SearchResult.NotFound;
  318. }
  319. ref Bucket bucket = ref GetBucketForHash(dataHash);
  320. if (bucket.InlineEntry.Data != null)
  321. {
  322. if (bucket.InlineEntry.Hash == dataHash && bucket.InlineEntry.GetData().SequenceEqual(dataSpan))
  323. {
  324. item = bucket.InlineEntry.Item;
  325. data = bucket.InlineEntry.Data;
  326. return bucket.InlineEntry.IsPartial ? SearchResult.FoundPartial : SearchResult.FoundFull;
  327. }
  328. if (bucket.MoreEntries != null)
  329. {
  330. foreach (Entry entry in bucket.MoreEntries)
  331. {
  332. if (entry.Hash == dataHash && entry.GetData().SequenceEqual(dataSpan))
  333. {
  334. item = entry.Item;
  335. data = entry.Data;
  336. return entry.IsPartial ? SearchResult.FoundPartial : SearchResult.FoundFull;
  337. }
  338. }
  339. }
  340. }
  341. return SearchResult.NotFound;
  342. }
  343. /// <summary>
  344. /// Rebuilds the table for a new count.
  345. /// </summary>
  346. /// <param name="newPow2Count">New power of two count of the table</param>
  347. private void Rebuild(int newPow2Count)
  348. {
  349. Bucket[] newBuckets = new Bucket[newPow2Count];
  350. uint mask = (uint)newPow2Count - 1;
  351. for (int i = 0; i < _buckets.Length; i++)
  352. {
  353. ref Bucket bucket = ref _buckets[i];
  354. if (bucket.InlineEntry.Data != null)
  355. {
  356. AddToBucket(ref newBuckets[(int)(bucket.InlineEntry.Hash & mask)], ref bucket.InlineEntry);
  357. if (bucket.MoreEntries != null)
  358. {
  359. foreach (Entry entry in bucket.MoreEntries)
  360. {
  361. Entry entryCopy = entry;
  362. AddToBucket(ref newBuckets[(int)(entry.Hash & mask)], ref entryCopy);
  363. }
  364. }
  365. }
  366. }
  367. _buckets = newBuckets;
  368. }
  369. /// <summary>
  370. /// Gets the bucket for a given hash.
  371. /// </summary>
  372. /// <param name="hash">Data hash</param>
  373. /// <returns>Bucket for the hash</returns>
  374. private ref Bucket GetBucketForHash(uint hash)
  375. {
  376. int index = (int)(hash & (_buckets.Length - 1));
  377. return ref _buckets[index];
  378. }
  379. /// <summary>
  380. /// Gets a power of two count from a regular count.
  381. /// </summary>
  382. /// <param name="count">Count</param>
  383. /// <returns>Power of two count</returns>
  384. private static int GetPow2Count(int count)
  385. {
  386. // This returns the nearest power of two that is lower than count.
  387. // This was done to optimize memory usage rather than performance.
  388. return 1 << BitOperations.Log2((uint)count);
  389. }
  390. }
  391. }