No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParallelHashMapPerformanceTests.cs 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using Unity.PerformanceTesting;
  5. using Unity.PerformanceTesting.Benchmark;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading;
  8. namespace Unity.Collections.PerformanceTests
  9. {
  10. static class ParallelHashMapUtil
  11. {
  12. static public void AllocInt(ref NativeParallelHashMap<int, int> container, int capacity, bool addValues)
  13. {
  14. if (capacity >= 0)
  15. {
  16. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_1);
  17. container = new NativeParallelHashMap<int, int>(capacity, Allocator.Persistent);
  18. if (addValues)
  19. {
  20. int keysAdded = 0;
  21. while (keysAdded < capacity)
  22. {
  23. int randKey = random.NextInt();
  24. if (container.TryAdd(randKey, keysAdded))
  25. {
  26. ++keysAdded;
  27. }
  28. }
  29. }
  30. }
  31. else
  32. container.Dispose();
  33. }
  34. static public void AllocInt(ref UnsafeParallelHashMap<int, int> container, int capacity, bool addValues)
  35. {
  36. if (capacity >= 0)
  37. {
  38. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_1);
  39. container = new UnsafeParallelHashMap<int, int>(capacity, Allocator.Persistent);
  40. if (addValues)
  41. {
  42. int keysAdded = 0;
  43. while (keysAdded < capacity)
  44. {
  45. int randKey = random.NextInt();
  46. if (container.TryAdd(randKey, keysAdded))
  47. {
  48. ++keysAdded;
  49. }
  50. }
  51. }
  52. }
  53. else
  54. container.Dispose();
  55. }
  56. static public object AllocBclContainer(int capacity, bool addValues)
  57. {
  58. if (capacity < 0)
  59. return null;
  60. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_1);
  61. // FROM MICROSOFT DOCUMENTATION
  62. // The higher the concurrencyLevel, the higher the theoretical number of operations
  63. // that could be performed concurrently on the ConcurrentDictionary. However, global
  64. // operations like resizing the dictionary take longer as the concurrencyLevel rises.
  65. // For the purposes of this example, we'll compromise at numCores * 2.
  66. var bclContainer = new System.Collections.Concurrent.ConcurrentDictionary<int, int>(System.Environment.ProcessorCount * 2, capacity);
  67. if (addValues)
  68. {
  69. int keysAdded = 0;
  70. while (keysAdded < capacity)
  71. {
  72. int randKey = random.NextInt();
  73. if (bclContainer.TryAdd(randKey, keysAdded))
  74. {
  75. ++keysAdded;
  76. }
  77. }
  78. }
  79. return bclContainer;
  80. }
  81. static public void CreateRandomKeys(int capacity, ref UnsafeList<int> keys)
  82. {
  83. if (capacity >= 0)
  84. {
  85. keys = new UnsafeList<int>(capacity, Allocator.Persistent);
  86. using (UnsafeHashSet<int> randomFilter = new UnsafeHashSet<int>(capacity, Allocator.Persistent))
  87. {
  88. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_2);
  89. int keysAdded = 0;
  90. while (keysAdded < capacity)
  91. {
  92. int randKey = random.NextInt();
  93. if (randomFilter.Add(randKey))
  94. {
  95. keys.Add(randKey);
  96. ++keysAdded;
  97. }
  98. }
  99. }
  100. }
  101. else
  102. keys.Dispose();
  103. }
  104. static public void CreateRandomKeys(int capacity, ref UnsafeList<int> keys, ref UnsafeParallelHashMap<int, int> hashMap)
  105. {
  106. if (capacity >= 0)
  107. {
  108. keys = new UnsafeList<int>(capacity, Allocator.Persistent);
  109. using (UnsafeHashSet<int> randomFilter = new UnsafeHashSet<int>(capacity, Allocator.Persistent))
  110. {
  111. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_2);
  112. int keysAdded = 0;
  113. while (keysAdded < capacity)
  114. {
  115. int randKey = random.NextInt();
  116. if (randomFilter.Add(randKey))
  117. {
  118. keys.Add(randKey);
  119. ++keysAdded;
  120. }
  121. }
  122. }
  123. }
  124. else
  125. keys.Dispose();
  126. }
  127. static public void CreateRandomKeys(int capacity, ref UnsafeList<int> keys, ref System.Collections.Concurrent.ConcurrentDictionary<int, int> hashMap)
  128. {
  129. if (capacity >= 0)
  130. {
  131. keys = new UnsafeList<int>(capacity, Allocator.Persistent);
  132. using (UnsafeHashSet<int> randomFilter = new UnsafeHashSet<int>(capacity, Allocator.Persistent))
  133. {
  134. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_2);
  135. int keysAdded = 0;
  136. while (keysAdded < capacity)
  137. {
  138. int randKey = random.NextInt();
  139. if (randomFilter.Add(randKey))
  140. {
  141. keys.Add(randKey);
  142. ++keysAdded;
  143. }
  144. }
  145. }
  146. }
  147. else
  148. keys.Dispose();
  149. }
  150. static public void CreateRandomKeys(int capacity, ref UnsafeList<int> keys, ref NativeParallelHashMap<int, int> hashMap)
  151. {
  152. if (capacity >= 0)
  153. {
  154. keys = new UnsafeList<int>(capacity, Allocator.Persistent);
  155. using (UnsafeHashSet<int> randomFilter = new UnsafeHashSet<int>(capacity, Allocator.Persistent))
  156. {
  157. Unity.Mathematics.Random random = new Unity.Mathematics.Random(HashMapUtil.K_RANDOM_SEED_2);
  158. int keysAdded = 0;
  159. while (keysAdded < capacity)
  160. {
  161. int randKey = random.NextInt();
  162. if (randomFilter.Add(randKey))
  163. {
  164. keys.Add(randKey);
  165. ++keysAdded;
  166. }
  167. }
  168. }
  169. }
  170. else
  171. keys.Dispose();
  172. }
  173. static public void RandomlyShuffleKeys(int capacity, ref UnsafeList<int> keys)
  174. {
  175. if (capacity >= 0)
  176. {
  177. Unity.Mathematics.Random random = new Mathematics.Random(HashMapUtil.K_RANDOM_SEED_3);
  178. for (int i = 0; i < capacity; i++)
  179. {
  180. int keyAt = keys[i];
  181. int randomIndex = random.NextInt(0, capacity - 1);
  182. keys[i] = keys[randomIndex];
  183. keys[randomIndex] = keyAt;
  184. }
  185. }
  186. }
  187. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  188. static public void SplitForWorkers(int count, int worker, int workers, out int startInclusive, out int endExclusive)
  189. {
  190. startInclusive = count * worker / workers;
  191. endExclusive = count * (worker + 1) / workers;
  192. }
  193. }
  194. struct ParallelHashMapIsEmpty100k : IBenchmarkContainerParallel
  195. {
  196. const int kIterations = 100_000;
  197. int workers;
  198. NativeParallelHashMap<int, int> nativeContainer;
  199. UnsafeParallelHashMap<int, int> unsafeContainer;
  200. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args) => workers = args[0];
  201. public void AllocNativeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, true);
  202. public void AllocUnsafeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, true);
  203. public object AllocBclContainer(int capacity) => ParallelHashMapUtil.AllocBclContainer(capacity, true);
  204. [MethodImpl(MethodImplOptions.NoOptimization)]
  205. public void MeasureNativeContainer(int worker, int threadIndex)
  206. {
  207. var reader = nativeContainer.AsReadOnly();
  208. ParallelHashMapUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  209. for (int i = start; i < end; i++)
  210. _ = reader.IsEmpty;
  211. }
  212. [MethodImpl(MethodImplOptions.NoOptimization)]
  213. public void MeasureUnsafeContainer(int worker, int threadIndex)
  214. {
  215. ParallelHashMapUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  216. for (int i = start; i < end; i++)
  217. _ = unsafeContainer.IsEmpty;
  218. }
  219. [MethodImpl(MethodImplOptions.NoOptimization)]
  220. public void MeasureBclContainer(object container, int worker)
  221. {
  222. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  223. ParallelHashMapUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  224. for (int i = start; i < end; i++)
  225. _ = bclContainer.IsEmpty;
  226. }
  227. }
  228. struct ParallelHashMapCount100k : IBenchmarkContainerParallel
  229. {
  230. const int kIterations = 100_000;
  231. int workers;
  232. NativeParallelHashMap<int, int> nativeContainer;
  233. UnsafeParallelHashMap<int, int> unsafeContainer;
  234. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args) => workers = args[0];
  235. public void AllocNativeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, true);
  236. public void AllocUnsafeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, true);
  237. public object AllocBclContainer(int capacity) => ParallelHashMapUtil.AllocBclContainer(capacity, true);
  238. [MethodImpl(MethodImplOptions.NoOptimization)]
  239. public void MeasureNativeContainer(int worker, int threadIndex)
  240. {
  241. var reader = nativeContainer.AsReadOnly();
  242. ParallelHashMapUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  243. for (int i = start; i < end; i++)
  244. _ = reader.Count();
  245. }
  246. [MethodImpl(MethodImplOptions.NoOptimization)]
  247. public void MeasureUnsafeContainer(int worker, int threadIndex)
  248. {
  249. ParallelHashMapUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  250. for (int i = start; i < end; i++)
  251. _ = unsafeContainer.Count();
  252. }
  253. [MethodImpl(MethodImplOptions.NoOptimization)]
  254. public void MeasureBclContainer(object container, int worker)
  255. {
  256. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  257. ParallelHashMapUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  258. for (int i = start; i < end; i++)
  259. _ = bclContainer.Count;
  260. }
  261. }
  262. struct ParallelHashMapToNativeArrayKeys : IBenchmarkContainerParallel
  263. {
  264. NativeParallelHashMap<int, int> nativeContainer;
  265. UnsafeParallelHashMap<int, int> unsafeContainer;
  266. public void AllocNativeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, true);
  267. public void AllocUnsafeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, true);
  268. public object AllocBclContainer(int capacity) => ParallelHashMapUtil.AllocBclContainer(capacity, true);
  269. public void MeasureNativeContainer(int worker, int threadIndex)
  270. {
  271. var asArray = nativeContainer.GetKeyArray(Allocator.Temp);
  272. asArray.Dispose();
  273. }
  274. public void MeasureUnsafeContainer(int worker, int threadIndex)
  275. {
  276. var asArray = unsafeContainer.GetKeyArray(Allocator.Temp);
  277. asArray.Dispose();
  278. }
  279. public void MeasureBclContainer(object container, int worker)
  280. {
  281. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  282. int[] asArray = new int[bclContainer.Count];
  283. bclContainer.Keys.CopyTo(asArray, 0);
  284. }
  285. }
  286. struct ParallelHashMapToNativeArrayValues : IBenchmarkContainerParallel
  287. {
  288. NativeParallelHashMap<int, int> nativeContainer;
  289. UnsafeParallelHashMap<int, int> unsafeContainer;
  290. public void AllocNativeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, true);
  291. public void AllocUnsafeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, true);
  292. public object AllocBclContainer(int capacity) => ParallelHashMapUtil.AllocBclContainer(capacity, true);
  293. public void MeasureNativeContainer(int worker, int threadIndex)
  294. {
  295. var asArray = nativeContainer.GetValueArray(Allocator.Temp);
  296. asArray.Dispose();
  297. }
  298. public void MeasureUnsafeContainer(int worker, int threadIndex)
  299. {
  300. var asArray = unsafeContainer.GetValueArray(Allocator.Temp);
  301. asArray.Dispose();
  302. }
  303. public void MeasureBclContainer(object container, int worker)
  304. {
  305. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  306. int[] asArray = new int[bclContainer.Count];
  307. bclContainer.Values.CopyTo(asArray, 0);
  308. }
  309. }
  310. struct ParallelHashMapInsert : IBenchmarkContainerParallel
  311. {
  312. int capacity;
  313. int workers;
  314. NativeParallelHashMap<int, int> nativeContainer;
  315. UnsafeParallelHashMap<int, int> unsafeContainer;
  316. UnsafeList<int> keys;
  317. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  318. {
  319. this.capacity = capacity;
  320. workers = args[0];
  321. }
  322. public void AllocNativeContainer(int capacity)
  323. {
  324. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, false);
  325. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  326. }
  327. public void AllocUnsafeContainer(int capacity)
  328. {
  329. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, false);
  330. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  331. }
  332. public object AllocBclContainer(int capacity)
  333. {
  334. object container = ParallelHashMapUtil.AllocBclContainer(capacity, false);
  335. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  336. return container;
  337. }
  338. public void MeasureNativeContainer(int worker, int threadIndex)
  339. {
  340. var writer = nativeContainer.AsParallelWriter();
  341. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  342. for (int i = start; i < end; i++)
  343. writer.TryAdd(keys[i], i, threadIndex);
  344. }
  345. public void MeasureUnsafeContainer(int worker, int threadIndex)
  346. {
  347. var writer = unsafeContainer.AsParallelWriter();
  348. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  349. for (int i = start; i < end; i++)
  350. writer.TryAdd(keys[i], i, threadIndex);
  351. }
  352. public void MeasureBclContainer(object container, int worker)
  353. {
  354. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  355. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  356. for (int i = start; i < end; i++)
  357. bclContainer.TryAdd(keys[i], i);
  358. }
  359. }
  360. struct ParallelHashMapAddGrow : IBenchmarkContainerParallel
  361. {
  362. int capacity;
  363. int toAdd;
  364. NativeParallelHashMap<int, int> nativeContainer;
  365. UnsafeParallelHashMap<int, int> unsafeContainer;
  366. UnsafeList<int> keys;
  367. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  368. {
  369. this.capacity = capacity;
  370. toAdd = args[0] - capacity;
  371. }
  372. public void AllocNativeContainer(int capacity)
  373. {
  374. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, true);
  375. int toAddCount = capacity < 0 ? -1 : toAdd;
  376. ParallelHashMapUtil.CreateRandomKeys(toAddCount, ref keys, ref nativeContainer);
  377. }
  378. public void AllocUnsafeContainer(int capacity)
  379. {
  380. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, true);
  381. int toAddCount = capacity < 0 ? -1 : toAdd;
  382. ParallelHashMapUtil.CreateRandomKeys(toAddCount, ref keys, ref unsafeContainer);
  383. }
  384. public object AllocBclContainer(int capacity)
  385. {
  386. object container = ParallelHashMapUtil.AllocBclContainer(capacity, true);
  387. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  388. int toAddCount = capacity < 0 ? -1 : toAdd;
  389. ParallelHashMapUtil.CreateRandomKeys(toAddCount, ref keys, ref bclContainer);
  390. return container;
  391. }
  392. public void MeasureNativeContainer(int _, int __)
  393. {
  394. // Intentionally setting capacity small and growing by adding more items
  395. for (int i = 0; i < toAdd; i++)
  396. nativeContainer.Add(keys[i], i);
  397. }
  398. public void MeasureUnsafeContainer(int _, int __)
  399. {
  400. // Intentionally setting capacity small and growing by adding more items
  401. for (int i = 0; i < toAdd; i++)
  402. unsafeContainer.Add(keys[i], i);
  403. }
  404. public void MeasureBclContainer(object container, int _)
  405. {
  406. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  407. // Intentionally setting capacity small and growing by adding more items
  408. for (int i = 0; i < toAdd; i++)
  409. bclContainer.TryAdd(keys[i], i);
  410. }
  411. }
  412. struct ParallelHashMapContains : IBenchmarkContainerParallel
  413. {
  414. int capacity;
  415. int workers;
  416. NativeParallelHashMap<int, int> nativeContainer;
  417. UnsafeParallelHashMap<int, int> unsafeContainer;
  418. UnsafeList<int> keys;
  419. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  420. {
  421. this.capacity = capacity;
  422. workers = args[0];
  423. }
  424. public void AllocNativeContainer(int capacity)
  425. {
  426. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, false);
  427. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  428. for (int i = 0; i < capacity; i++)
  429. nativeContainer.TryAdd(keys[i], i);
  430. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  431. }
  432. public void AllocUnsafeContainer(int capacity)
  433. {
  434. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, false);
  435. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  436. for (int i = 0; i < capacity; i++)
  437. unsafeContainer.TryAdd(keys[i], i);
  438. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  439. }
  440. public object AllocBclContainer(int capacity)
  441. {
  442. object container = ParallelHashMapUtil.AllocBclContainer(capacity, false);
  443. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  444. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  445. for (int i = 0; i < capacity; i++)
  446. bclContainer.TryAdd(keys[i], i);
  447. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  448. return container;
  449. }
  450. public void MeasureNativeContainer(int worker, int threadIndex)
  451. {
  452. var reader = nativeContainer.AsReadOnly();
  453. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  454. bool data = false;
  455. for (int i = start; i < end; i++)
  456. Volatile.Write(ref data, reader.ContainsKey(keys[i]));
  457. }
  458. public void MeasureUnsafeContainer(int worker, int threadIndex)
  459. {
  460. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  461. bool data = false;
  462. for (int i = start; i < end; i++)
  463. Volatile.Write(ref data, unsafeContainer.ContainsKey(keys[i]));
  464. }
  465. public void MeasureBclContainer(object container, int worker)
  466. {
  467. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  468. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  469. bool data = false;
  470. for (int i = start; i < end; i++)
  471. Volatile.Write(ref data, bclContainer.ContainsKey(keys[i]));
  472. }
  473. }
  474. struct ParallelHashMapIndexedRead : IBenchmarkContainerParallel
  475. {
  476. NativeParallelHashMap<int, int> nativeContainer;
  477. UnsafeParallelHashMap<int, int> unsafeContainer;
  478. UnsafeList<int> keys;
  479. public void AllocNativeContainer(int capacity)
  480. {
  481. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, false);
  482. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  483. for (int i = 0; i < capacity; i++)
  484. nativeContainer.TryAdd(keys[i], i);
  485. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  486. }
  487. public void AllocUnsafeContainer(int capacity)
  488. {
  489. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, false);
  490. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  491. for (int i = 0; i < capacity; i++)
  492. unsafeContainer.TryAdd(keys[i], i);
  493. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  494. }
  495. public object AllocBclContainer(int capacity)
  496. {
  497. object container = ParallelHashMapUtil.AllocBclContainer(capacity, false);
  498. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  499. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  500. for (int i = 0; i < capacity; i++)
  501. bclContainer.TryAdd(keys[i], i);
  502. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  503. return container;
  504. }
  505. public void MeasureNativeContainer(int worker, int threadIndex)
  506. {
  507. var reader = nativeContainer.AsReadOnly();
  508. int insertions = keys.Length;
  509. int value = 0;
  510. for (int i = 0; i < insertions; i++)
  511. Volatile.Write(ref value, reader[keys[i]]);
  512. }
  513. public void MeasureUnsafeContainer(int worker, int threadIndex)
  514. {
  515. int insertions = keys.Length;
  516. int value = 0;
  517. for (int i = 0; i < insertions; i++)
  518. Volatile.Write(ref value, unsafeContainer[keys[i]]);
  519. }
  520. public void MeasureBclContainer(object container, int worker)
  521. {
  522. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  523. int insertions = keys.Length;
  524. int value = 0;
  525. for (int i = 0; i < insertions; i++)
  526. Volatile.Write(ref value, bclContainer[keys[i]]);
  527. }
  528. }
  529. struct ParallelHashMapIndexedWrite : IBenchmarkContainerParallel
  530. {
  531. NativeParallelHashMap<int, int> nativeContainer;
  532. UnsafeParallelHashMap<int, int> unsafeContainer;
  533. UnsafeList<int> keys;
  534. public void AllocNativeContainer(int capacity)
  535. {
  536. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, false);
  537. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  538. for (int i = 0; i < capacity; i++)
  539. nativeContainer.TryAdd(keys[i], i);
  540. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  541. }
  542. public void AllocUnsafeContainer(int capacity)
  543. {
  544. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, false);
  545. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  546. for (int i = 0; i < capacity; i++)
  547. unsafeContainer.TryAdd(keys[i], i);
  548. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  549. }
  550. public object AllocBclContainer(int capacity)
  551. {
  552. object container = ParallelHashMapUtil.AllocBclContainer(capacity, false);
  553. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  554. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  555. for (int i = 0; i < capacity; i++)
  556. bclContainer.TryAdd(keys[i], i);
  557. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  558. return container;
  559. }
  560. public void MeasureNativeContainer(int worker, int threadIndex)
  561. {
  562. int insertions = keys.Length;
  563. for (int i = 0; i < insertions; i++)
  564. nativeContainer[keys[i]] = i;
  565. }
  566. public void MeasureUnsafeContainer(int worker, int threadIndex)
  567. {
  568. int insertions = keys.Length;
  569. for (int i = 0; i < insertions; i++)
  570. unsafeContainer[keys[i]] = i;
  571. }
  572. public void MeasureBclContainer(object container, int worker)
  573. {
  574. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  575. int insertions = keys.Length;
  576. for (int i = 0; i < insertions; i++)
  577. bclContainer[keys[i]] = i;
  578. }
  579. }
  580. struct ParallelHashMapTryGetValue : IBenchmarkContainerParallel
  581. {
  582. int workers;
  583. NativeParallelHashMap<int, int> nativeContainer;
  584. UnsafeParallelHashMap<int, int> unsafeContainer;
  585. UnsafeList<int> keys;
  586. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args) => workers = args[0];
  587. public void AllocNativeContainer(int capacity)
  588. {
  589. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, false);
  590. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  591. for (int i = 0; i < capacity; i++)
  592. nativeContainer.TryAdd(keys[i], i);
  593. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  594. }
  595. public void AllocUnsafeContainer(int capacity)
  596. {
  597. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, false);
  598. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  599. for (int i = 0; i < capacity; i++)
  600. unsafeContainer.TryAdd(keys[i], i);
  601. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  602. }
  603. public object AllocBclContainer(int capacity)
  604. {
  605. object container = ParallelHashMapUtil.AllocBclContainer(capacity, false);
  606. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  607. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  608. for (int i = 0; i < capacity; i++)
  609. bclContainer.TryAdd(keys[i], i);
  610. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  611. return container;
  612. }
  613. public void MeasureNativeContainer(int worker, int threadIndex)
  614. {
  615. var reader = nativeContainer.AsReadOnly();
  616. ParallelHashMapUtil.SplitForWorkers(keys.Length, worker, workers, out int start, out int end);
  617. for (int i = start; i < end; i++)
  618. {
  619. reader.TryGetValue(keys[i], out var value);
  620. Volatile.Read(ref value);
  621. }
  622. }
  623. public void MeasureUnsafeContainer(int worker, int threadIndex)
  624. {
  625. ParallelHashMapUtil.SplitForWorkers(keys.Length, worker, workers, out int start, out int end);
  626. for (int i = start; i < end; i++)
  627. {
  628. unsafeContainer.TryGetValue(keys[i], out var value);
  629. Volatile.Read(ref value);
  630. }
  631. }
  632. public void MeasureBclContainer(object container, int worker)
  633. {
  634. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  635. ParallelHashMapUtil.SplitForWorkers(keys.Length, worker, workers, out int start, out int end);
  636. for (int i = start; i < end; i++)
  637. {
  638. bclContainer.TryGetValue(keys[i], out var value);
  639. Volatile.Read(ref value);
  640. }
  641. }
  642. }
  643. struct ParallelHashMapRemove : IBenchmarkContainerParallel
  644. {
  645. NativeParallelHashMap<int, int> nativeContainer;
  646. UnsafeParallelHashMap<int, int> unsafeContainer;
  647. UnsafeList<int> keys;
  648. public void AllocNativeContainer(int capacity)
  649. {
  650. ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, false);
  651. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  652. for (int i = 0; i < capacity; i++)
  653. nativeContainer.TryAdd(keys[i], i);
  654. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  655. }
  656. public void AllocUnsafeContainer(int capacity)
  657. {
  658. ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, false);
  659. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  660. for (int i = 0; i < capacity; i++)
  661. unsafeContainer.TryAdd(keys[i], i);
  662. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  663. }
  664. public object AllocBclContainer(int capacity)
  665. {
  666. object container = ParallelHashMapUtil.AllocBclContainer(capacity, false);
  667. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  668. ParallelHashMapUtil.CreateRandomKeys(capacity, ref keys);
  669. for (int i = 0; i < capacity; i++)
  670. bclContainer.TryAdd(keys[i], i);
  671. ParallelHashMapUtil.RandomlyShuffleKeys(capacity, ref keys);
  672. return container;
  673. }
  674. public void MeasureNativeContainer(int worker, int threadIndex)
  675. {
  676. int insertions = keys.Length;
  677. for (int i = 0; i < insertions; i++)
  678. nativeContainer.Remove(keys[i]);
  679. }
  680. public void MeasureUnsafeContainer(int worker, int threadIndex)
  681. {
  682. int insertions = keys.Length;
  683. for (int i = 0; i < insertions; i++)
  684. unsafeContainer.Remove(keys[i]);
  685. }
  686. public void MeasureBclContainer(object container, int worker)
  687. {
  688. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  689. int insertions = keys.Length;
  690. for (int i = 0; i < insertions; i++)
  691. bclContainer.TryRemove(keys[i], out _);
  692. }
  693. }
  694. struct ParallelHashMapForEach : IBenchmarkContainerParallel
  695. {
  696. NativeParallelHashMap<int, int> nativeContainer;
  697. UnsafeParallelHashMap<int, int> unsafeContainer;
  698. public void AllocNativeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref nativeContainer, capacity, true);
  699. public void AllocUnsafeContainer(int capacity) => ParallelHashMapUtil.AllocInt(ref unsafeContainer, capacity, true);
  700. public object AllocBclContainer(int capacity) => ParallelHashMapUtil.AllocBclContainer(capacity, true);
  701. public void MeasureNativeContainer(int _, int __)
  702. {
  703. foreach (var pair in nativeContainer)
  704. Volatile.Read(ref pair.Value);
  705. }
  706. public void MeasureUnsafeContainer(int _, int __)
  707. {
  708. foreach (var pair in unsafeContainer)
  709. Volatile.Read(ref pair.Value);
  710. }
  711. public void MeasureBclContainer(object container, int _)
  712. {
  713. int value = 0;
  714. var bclContainer = (System.Collections.Concurrent.ConcurrentDictionary<int, int>)container;
  715. foreach (var pair in bclContainer)
  716. Volatile.Write(ref value, pair.Value);
  717. }
  718. }
  719. [Benchmark(typeof(BenchmarkContainerType))]
  720. [BenchmarkNameOverride(BenchmarkContainerConfig.BCL, "ConcurrentDictionary")]
  721. class ParallelHashMap
  722. {
  723. #if UNITY_EDITOR
  724. [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + nameof(ParallelHashMap))]
  725. static void RunIndividual()
  726. => BenchmarkContainerConfig.RunBenchmark(typeof(ParallelHashMap));
  727. #endif
  728. [Test, Performance]
  729. [Category("Performance")]
  730. public unsafe void IsEmpty_x_100k(
  731. [Values(1, 2, 4)] int workers,
  732. [Values(0, 100)] int capacity,
  733. [Values] BenchmarkContainerType type)
  734. {
  735. BenchmarkContainerRunnerParallel<ParallelHashMapIsEmpty100k>.Run(workers, capacity, type, workers);
  736. }
  737. [Test, Performance]
  738. [Category("Performance")]
  739. public unsafe void Count_x_100k(
  740. [Values(1, 2, 4)] int workers,
  741. [Values(0, 100)] int capacity,
  742. [Values] BenchmarkContainerType type)
  743. {
  744. BenchmarkContainerRunnerParallel<ParallelHashMapCount100k>.Run(workers, capacity, type, workers);
  745. }
  746. [Test, Performance]
  747. [Category("Performance")]
  748. public unsafe void ToNativeArrayKeys(
  749. [Values(1)] int workers,
  750. [Values(10000, 100000, 1000000)] int capacity,
  751. [Values] BenchmarkContainerType type)
  752. {
  753. BenchmarkContainerRunnerParallel<ParallelHashMapToNativeArrayKeys>.Run(workers, capacity, type);
  754. }
  755. [Test, Performance]
  756. [Category("Performance")]
  757. public unsafe void ToNativeArrayValues(
  758. [Values(1)] int workers,
  759. [Values(10000, 100000, 1000000)] int capacity,
  760. [Values] BenchmarkContainerType type)
  761. {
  762. BenchmarkContainerRunnerParallel<ParallelHashMapToNativeArrayValues>.Run(workers, capacity, type);
  763. }
  764. [Test, Performance]
  765. [Category("Performance")]
  766. public unsafe void Insert(
  767. [Values(1, 2, 4)] int workers,
  768. #if UNITY_STANDALONE || UNITY_EDITOR
  769. [Values(10000, 100000, 1000000)] int insertions,
  770. #else
  771. [Values(10000, 100000)] int insertions, // Observe potential lower memory requirement on non-desktop platforms
  772. #endif
  773. [Values] BenchmarkContainerType type)
  774. {
  775. BenchmarkContainerRunnerParallel<ParallelHashMapInsert>.Run(workers, insertions, type, workers);
  776. }
  777. [Test, Performance]
  778. [Category("Performance")]
  779. [BenchmarkTestFootnote("Incrementally grows from `capacity` until reaching size of `growTo`")]
  780. public unsafe void AddGrow(
  781. [Values(1)] int workers, // Can't grow capacity in parallel
  782. [Values(4, 65536)] int capacity,
  783. [Values(1024 * 1024)] int growTo,
  784. [Values] BenchmarkContainerType type)
  785. {
  786. BenchmarkContainerRunnerParallel<ParallelHashMapAddGrow>.Run(workers, capacity, type, growTo);
  787. }
  788. [Test, Performance]
  789. [Category("Performance")]
  790. public unsafe void Contains(
  791. [Values(1, 2, 4)] int workers,
  792. #if UNITY_STANDALONE || UNITY_EDITOR
  793. [Values(10000, 100000, 1000000)] int insertions,
  794. #else
  795. [Values(10000, 100000)] int insertions, // Observe potential lower memory requirement on non-desktop platforms
  796. #endif
  797. [Values] BenchmarkContainerType type)
  798. {
  799. BenchmarkContainerRunnerParallel<ParallelHashMapContains>.Run(workers, insertions, type, workers);
  800. }
  801. [Test, Performance]
  802. [Category("Performance")]
  803. public unsafe void IndexedRead(
  804. [Values(1, 2, 4)] int workers,
  805. #if UNITY_STANDALONE || UNITY_EDITOR
  806. [Values(10000, 100000, 1000000)] int insertions,
  807. #else
  808. [Values(10000, 100000)] int insertions, // Observe potential lower memory requirement on non-desktop platforms
  809. #endif
  810. [Values] BenchmarkContainerType type)
  811. {
  812. BenchmarkContainerRunnerParallel<ParallelHashMapIndexedRead>.Run(workers, insertions, type, workers);
  813. }
  814. [Test, Performance]
  815. [Category("Performance")]
  816. public unsafe void IndexedWrite(
  817. [Values(1)] int workers, // Indexed write only available in single thread
  818. [Values(10000, 100000, 1000000)] int insertions,
  819. [Values] BenchmarkContainerType type)
  820. {
  821. BenchmarkContainerRunnerParallel<ParallelHashMapIndexedWrite>.Run(workers, insertions, type, workers);
  822. }
  823. [Test, Performance]
  824. [Category("Performance")]
  825. public unsafe void TryGetValue(
  826. [Values(1, 2, 4)] int workers,
  827. #if UNITY_STANDALONE || UNITY_EDITOR
  828. [Values(10000, 100000, 1000000)] int insertions,
  829. #else
  830. [Values(10000, 100000)] int insertions, // Observe potential lower memory requirement on non-desktop platforms
  831. #endif
  832. [Values] BenchmarkContainerType type)
  833. {
  834. BenchmarkContainerRunnerParallel<ParallelHashMapTryGetValue>.Run(workers, insertions, type, workers);
  835. }
  836. [Test, Performance]
  837. [Category("Performance")]
  838. public unsafe void Remove(
  839. [Values(1)] int workers, // No API for ParallelWriter.TryRemove currently
  840. [Values(10000, 100000, 1000000)] int insertions,
  841. [Values] BenchmarkContainerType type)
  842. {
  843. BenchmarkContainerRunnerParallel<ParallelHashMapRemove>.Run(workers, insertions, type, workers);
  844. }
  845. [Test, Performance]
  846. [Category("Performance")]
  847. public unsafe void Foreach(
  848. [Values(1)] int workers, // This work can't be split
  849. [Values(10000, 100000, 1000000)] int insertions,
  850. [Values] BenchmarkContainerType type)
  851. {
  852. BenchmarkContainerRunnerParallel<ParallelHashMapForEach>.Run(workers, insertions, type, workers);
  853. }
  854. }
  855. }