PartitionHashTable.cs 16 KB

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