暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ParallelHashSetPerformanceTests.cs 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 ParallelHashSetUtil
  11. {
  12. static public void AllocInt(ref NativeParallelHashSet<int> container, int capacity, bool addValues)
  13. {
  14. if (capacity >= 0)
  15. {
  16. Random.InitState(0);
  17. container = new NativeParallelHashSet<int>(capacity, Allocator.Persistent);
  18. if (addValues)
  19. {
  20. for (int i = 0; i < capacity; i++)
  21. container.Add(i);
  22. }
  23. }
  24. else
  25. container.Dispose();
  26. }
  27. static public void AllocInt(ref NativeParallelHashSet<int> containerA, ref NativeParallelHashSet<int> containerB, int capacity, bool addValues)
  28. {
  29. AllocInt(ref containerA, capacity, false);
  30. AllocInt(ref containerB, capacity, false);
  31. if (!addValues)
  32. return;
  33. for (int i = 0; i < capacity; i++)
  34. {
  35. containerA.Add(Random.Range(0, capacity * 2));
  36. containerB.Add(Random.Range(0, capacity * 2));
  37. }
  38. }
  39. static public void AllocInt(ref UnsafeParallelHashSet<int> container, int capacity, bool addValues)
  40. {
  41. if (capacity >= 0)
  42. {
  43. Random.InitState(0);
  44. container = new UnsafeParallelHashSet<int>(capacity, Allocator.Persistent);
  45. if (addValues)
  46. {
  47. for (int i = 0; i < capacity; i++)
  48. container.Add(i);
  49. }
  50. }
  51. else
  52. container.Dispose();
  53. }
  54. static public void AllocInt(ref UnsafeParallelHashSet<int> containerA, ref UnsafeParallelHashSet<int> containerB, int capacity, bool addValues)
  55. {
  56. AllocInt(ref containerA, capacity, false);
  57. AllocInt(ref containerB, capacity, false);
  58. if (!addValues)
  59. return;
  60. for (int i = 0; i < capacity; i++)
  61. {
  62. containerA.Add(Random.Range(0, capacity * 2));
  63. containerB.Add(Random.Range(0, capacity * 2));
  64. }
  65. }
  66. static public object AllocBclContainer(int capacity, bool addValues)
  67. {
  68. if (capacity < 0)
  69. return null;
  70. Random.InitState(0);
  71. var bclContainer = new FakeConcurrentHashSet<int>();
  72. if (addValues)
  73. {
  74. for (int i = 0; i < capacity; i++)
  75. bclContainer.Add(i);
  76. }
  77. return bclContainer;
  78. }
  79. static public object AllocBclContainerTuple(int capacity, bool addValues)
  80. {
  81. var tuple = new System.Tuple<FakeConcurrentHashSet<int>, FakeConcurrentHashSet<int>>(
  82. (FakeConcurrentHashSet<int>)AllocBclContainer(capacity, false),
  83. (FakeConcurrentHashSet<int>)AllocBclContainer(capacity, false));
  84. if (addValues)
  85. {
  86. for (int i = 0; i < capacity; i++)
  87. {
  88. tuple.Item1.Add(Random.Range(0, capacity * 2));
  89. tuple.Item2.Add(Random.Range(0, capacity * 2));
  90. }
  91. }
  92. return tuple;
  93. }
  94. static public void CreateRandomKeys(int capacity, ref UnsafeList<int> keys)
  95. {
  96. if (!keys.IsCreated)
  97. {
  98. keys = new UnsafeList<int>(capacity, Allocator.Persistent);
  99. Random.InitState(0);
  100. for (int i = 0; i < capacity; i++)
  101. {
  102. int randKey = Random.Range(0, capacity);
  103. keys.Add(randKey);
  104. }
  105. }
  106. else
  107. keys.Dispose();
  108. }
  109. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  110. static public void SplitForWorkers(int count, int worker, int workers, out int startInclusive, out int endExclusive)
  111. {
  112. startInclusive = count * worker / workers;
  113. endExclusive = count * (worker + 1) / workers;
  114. }
  115. }
  116. // A generic HashSet with a lock is generally recommended as most performant way to obtain a thread safe HashSet in C#.
  117. internal class FakeConcurrentHashSet<T> : System.IDisposable
  118. {
  119. private readonly ReaderWriterLockSlim m_Lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
  120. private readonly System.Collections.Generic.HashSet<T> m_HashSet = new System.Collections.Generic.HashSet<T>();
  121. ~FakeConcurrentHashSet() => Dispose(false);
  122. public bool Add(T item)
  123. {
  124. m_Lock.EnterWriteLock();
  125. try
  126. {
  127. return m_HashSet.Add(item);
  128. }
  129. finally
  130. {
  131. if (m_Lock.IsWriteLockHeld)
  132. m_Lock.ExitWriteLock();
  133. }
  134. }
  135. public void Clear()
  136. {
  137. m_Lock.EnterWriteLock();
  138. try
  139. {
  140. m_HashSet.Clear();
  141. }
  142. finally
  143. {
  144. if (m_Lock.IsWriteLockHeld)
  145. m_Lock.ExitWriteLock();
  146. }
  147. }
  148. public bool Contains(T item)
  149. {
  150. m_Lock.EnterReadLock();
  151. try
  152. {
  153. return m_HashSet.Contains(item);
  154. }
  155. finally
  156. {
  157. if (m_Lock.IsReadLockHeld)
  158. m_Lock.ExitReadLock();
  159. }
  160. }
  161. public bool Remove(T item)
  162. {
  163. m_Lock.EnterWriteLock();
  164. try
  165. {
  166. return m_HashSet.Remove(item);
  167. }
  168. finally
  169. {
  170. if (m_Lock.IsWriteLockHeld)
  171. m_Lock.ExitWriteLock();
  172. }
  173. }
  174. public int Count
  175. {
  176. get
  177. {
  178. m_Lock.EnterReadLock();
  179. try
  180. {
  181. return m_HashSet.Count;
  182. }
  183. finally
  184. {
  185. if (m_Lock.IsReadLockHeld)
  186. m_Lock.ExitReadLock();
  187. }
  188. }
  189. }
  190. public void CopyTo(T[] array)
  191. {
  192. m_Lock.EnterReadLock();
  193. try
  194. {
  195. m_HashSet.CopyTo(array);
  196. }
  197. finally
  198. {
  199. if (m_Lock.IsReadLockHeld)
  200. m_Lock.ExitReadLock();
  201. }
  202. }
  203. public System.Collections.Generic.HashSet<T>.Enumerator GetEnumerator()
  204. {
  205. m_Lock.EnterReadLock();
  206. try
  207. {
  208. return m_HashSet.GetEnumerator();
  209. }
  210. finally
  211. {
  212. if (m_Lock.IsReadLockHeld)
  213. m_Lock.ExitReadLock();
  214. }
  215. }
  216. public void UnionWith(FakeConcurrentHashSet<T> other)
  217. {
  218. m_Lock.EnterReadLock();
  219. try
  220. {
  221. m_HashSet.UnionWith(other.m_HashSet);
  222. }
  223. finally
  224. {
  225. if (m_Lock.IsReadLockHeld)
  226. m_Lock.ExitReadLock();
  227. }
  228. }
  229. public void IntersectWith(FakeConcurrentHashSet<T> other)
  230. {
  231. m_Lock.EnterReadLock();
  232. try
  233. {
  234. m_HashSet.IntersectWith(other.m_HashSet);
  235. }
  236. finally
  237. {
  238. if (m_Lock.IsReadLockHeld)
  239. m_Lock.ExitReadLock();
  240. }
  241. }
  242. public void ExceptWith(FakeConcurrentHashSet<T> other)
  243. {
  244. m_Lock.EnterReadLock();
  245. try
  246. {
  247. m_HashSet.ExceptWith(other.m_HashSet);
  248. }
  249. finally
  250. {
  251. if (m_Lock.IsReadLockHeld)
  252. m_Lock.ExitReadLock();
  253. }
  254. }
  255. public void Dispose()
  256. {
  257. Dispose(true);
  258. System.GC.SuppressFinalize(this);
  259. }
  260. protected virtual void Dispose(bool disposing)
  261. {
  262. if (disposing && m_Lock != null)
  263. m_Lock.Dispose();
  264. }
  265. }
  266. struct ParallelHashSetIsEmpty100k : IBenchmarkContainerParallel
  267. {
  268. const int kIterations = 100_000;
  269. int workers;
  270. NativeParallelHashSet<int> nativeContainer;
  271. UnsafeParallelHashSet<int> unsafeContainer;
  272. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args) => workers = args[0];
  273. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, true);
  274. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, true);
  275. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainer(capacity, true);
  276. [MethodImpl(MethodImplOptions.NoOptimization)]
  277. public void MeasureNativeContainer(int worker, int threadIndex)
  278. {
  279. var reader = nativeContainer.AsReadOnly();
  280. ParallelHashSetUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  281. for (int i = start; i < end; i++)
  282. _ = reader.IsEmpty;
  283. }
  284. [MethodImpl(MethodImplOptions.NoOptimization)]
  285. public void MeasureUnsafeContainer(int worker, int threadIndex)
  286. {
  287. ParallelHashSetUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  288. for (int i = start; i < end; i++)
  289. _ = unsafeContainer.IsEmpty;
  290. }
  291. [MethodImpl(MethodImplOptions.NoOptimization)]
  292. public void MeasureBclContainer(object container, int worker)
  293. {
  294. var bclContainer = (FakeConcurrentHashSet<int>)container;
  295. ParallelHashSetUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  296. for (int i = start; i < end; i++)
  297. _ = bclContainer.Count == 0;
  298. }
  299. }
  300. struct ParallelHashSetCount100k : IBenchmarkContainerParallel
  301. {
  302. const int kIterations = 100_000;
  303. int workers;
  304. NativeParallelHashSet<int> nativeContainer;
  305. UnsafeParallelHashSet<int> unsafeContainer;
  306. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args) => workers = args[0];
  307. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, true);
  308. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, true);
  309. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainer(capacity, true);
  310. [MethodImpl(MethodImplOptions.NoOptimization)]
  311. public void MeasureNativeContainer(int worker, int threadIndex)
  312. {
  313. var reader = nativeContainer.AsReadOnly();
  314. ParallelHashSetUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  315. for (int i = start; i < end; i++)
  316. _ = reader.Count();
  317. }
  318. [MethodImpl(MethodImplOptions.NoOptimization)]
  319. public void MeasureUnsafeContainer(int worker, int threadIndex)
  320. {
  321. ParallelHashSetUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  322. for (int i = start; i < end; i++)
  323. _ = unsafeContainer.Count();
  324. }
  325. [MethodImpl(MethodImplOptions.NoOptimization)]
  326. public void MeasureBclContainer(object container, int worker)
  327. {
  328. var bclContainer = (FakeConcurrentHashSet<int>)container;
  329. ParallelHashSetUtil.SplitForWorkers(kIterations, worker, workers, out int start, out int end);
  330. for (int i = start; i < end; i++)
  331. _ = bclContainer.Count;
  332. }
  333. }
  334. struct ParallelHashSetToNativeArray : IBenchmarkContainerParallel
  335. {
  336. NativeParallelHashSet<int> nativeContainer;
  337. UnsafeParallelHashSet<int> unsafeContainer;
  338. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, true);
  339. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, true);
  340. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainer(capacity, true);
  341. public void MeasureNativeContainer(int worker, int threadIndex)
  342. {
  343. var asArray = nativeContainer.ToNativeArray(Allocator.Temp);
  344. asArray.Dispose();
  345. }
  346. public void MeasureUnsafeContainer(int worker, int threadIndex)
  347. {
  348. var asArray = unsafeContainer.ToNativeArray(Allocator.Temp);
  349. asArray.Dispose();
  350. }
  351. public void MeasureBclContainer(object container, int worker)
  352. {
  353. var bclContainer = (FakeConcurrentHashSet<int>)container;
  354. int[] asArray = new int[bclContainer.Count];
  355. bclContainer.CopyTo(asArray);
  356. }
  357. }
  358. struct ParallelHashSetInsert : IBenchmarkContainerParallel
  359. {
  360. int capacity;
  361. int workers;
  362. NativeParallelHashSet<int> nativeContainer;
  363. UnsafeParallelHashSet<int> unsafeContainer;
  364. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  365. {
  366. this.capacity = capacity;
  367. workers = args[0];
  368. }
  369. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, false);
  370. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, false);
  371. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainer(capacity, false);
  372. public void MeasureNativeContainer(int worker, int threadIndex)
  373. {
  374. var writer = nativeContainer.AsParallelWriter();
  375. ParallelHashSetUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  376. for (int i = start; i < end; i++)
  377. writer.Add(i, threadIndex);
  378. }
  379. public void MeasureUnsafeContainer(int worker, int threadIndex)
  380. {
  381. var writer = unsafeContainer.AsParallelWriter();
  382. ParallelHashSetUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  383. for (int i = start; i < end; i++)
  384. writer.Add(i, threadIndex);
  385. }
  386. public void MeasureBclContainer(object container, int worker)
  387. {
  388. var bclContainer = (FakeConcurrentHashSet<int>)container;
  389. ParallelHashSetUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  390. for (int i = start; i < end; i++)
  391. bclContainer.Add(i);
  392. }
  393. }
  394. struct ParallelHashSetAddGrow : IBenchmarkContainerParallel
  395. {
  396. int capacity;
  397. int toAdd;
  398. NativeParallelHashSet<int> nativeContainer;
  399. UnsafeParallelHashSet<int> unsafeContainer;
  400. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  401. {
  402. this.capacity = capacity;
  403. toAdd = args[0] - capacity;
  404. }
  405. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, true);
  406. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, true);
  407. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainer(capacity, true);
  408. public void MeasureNativeContainer(int _, int __)
  409. {
  410. // Intentionally setting capacity small and growing by adding more items
  411. for (int i = capacity; i < capacity + toAdd; i++)
  412. nativeContainer.Add(i);
  413. }
  414. public void MeasureUnsafeContainer(int _, int __)
  415. {
  416. // Intentionally setting capacity small and growing by adding more items
  417. for (int i = capacity; i < capacity + toAdd; i++)
  418. unsafeContainer.Add(i);
  419. }
  420. public void MeasureBclContainer(object container, int _)
  421. {
  422. var bclContainer = (FakeConcurrentHashSet<int>)container;
  423. // Intentionally setting capacity small and growing by adding more items
  424. for (int i = capacity; i < capacity + toAdd; i++)
  425. bclContainer.Add(i);
  426. }
  427. }
  428. struct ParallelHashSetContains : IBenchmarkContainerParallel
  429. {
  430. int capacity;
  431. int workers;
  432. NativeParallelHashSet<int> nativeContainer;
  433. UnsafeParallelHashSet<int> unsafeContainer;
  434. UnsafeList<int> keys;
  435. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  436. {
  437. this.capacity = capacity;
  438. workers = args[0];
  439. }
  440. public void AllocNativeContainer(int capacity)
  441. {
  442. ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, false);
  443. ParallelHashSetUtil.CreateRandomKeys(capacity, ref keys);
  444. for (int i = 0; i < capacity; i++)
  445. nativeContainer.Add(keys[i]);
  446. }
  447. public void AllocUnsafeContainer(int capacity)
  448. {
  449. ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, false);
  450. ParallelHashSetUtil.CreateRandomKeys(capacity, ref keys);
  451. for (int i = 0; i < capacity; i++)
  452. unsafeContainer.Add(keys[i]);
  453. }
  454. public object AllocBclContainer(int capacity)
  455. {
  456. object container = ParallelHashSetUtil.AllocBclContainer(capacity, false);
  457. var bclContainer = (FakeConcurrentHashSet<int>)container;
  458. ParallelHashSetUtil.CreateRandomKeys(capacity, ref keys);
  459. for (int i = 0; i < capacity; i++)
  460. bclContainer.Add(keys[i]);
  461. return container;
  462. }
  463. public void MeasureNativeContainer(int worker, int threadIndex)
  464. {
  465. var reader = nativeContainer.AsReadOnly();
  466. ParallelHashSetUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  467. bool data = false;
  468. for (int i = start; i < end; i++)
  469. Volatile.Write(ref data, reader.Contains(keys[i]));
  470. }
  471. public void MeasureUnsafeContainer(int worker, int threadIndex)
  472. {
  473. ParallelHashSetUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  474. bool data = false;
  475. for (int i = start; i < end; i++)
  476. Volatile.Write(ref data, unsafeContainer.Contains(keys[i]));
  477. }
  478. public void MeasureBclContainer(object container, int worker)
  479. {
  480. var bclContainer = (FakeConcurrentHashSet<int>)container;
  481. ParallelHashSetUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  482. bool data = false;
  483. for (int i = start; i < end; i++)
  484. Volatile.Write(ref data, bclContainer.Contains(keys[i]));
  485. }
  486. }
  487. struct ParallelHashSetRemove : IBenchmarkContainerParallel
  488. {
  489. NativeParallelHashSet<int> nativeContainer;
  490. UnsafeParallelHashSet<int> unsafeContainer;
  491. UnsafeList<int> keys;
  492. public void AllocNativeContainer(int capacity)
  493. {
  494. ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, false);
  495. ParallelHashSetUtil.CreateRandomKeys(capacity, ref keys);
  496. for (int i = 0; i < capacity; i++)
  497. nativeContainer.Add(keys[i]);
  498. }
  499. public void AllocUnsafeContainer(int capacity)
  500. {
  501. ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, false);
  502. ParallelHashSetUtil.CreateRandomKeys(capacity, ref keys);
  503. for (int i = 0; i < capacity; i++)
  504. unsafeContainer.Add(keys[i]);
  505. }
  506. public object AllocBclContainer(int capacity)
  507. {
  508. object container = ParallelHashSetUtil.AllocBclContainer(capacity, false);
  509. var bclContainer = (FakeConcurrentHashSet<int>)container;
  510. ParallelHashSetUtil.CreateRandomKeys(capacity, ref keys);
  511. for (int i = 0; i < capacity; i++)
  512. bclContainer.Add(keys[i]);
  513. return container;
  514. }
  515. public void MeasureNativeContainer(int worker, int threadIndex)
  516. {
  517. int insertions = keys.Length;
  518. for (int i = 0; i < insertions; i++)
  519. nativeContainer.Remove(keys[i]);
  520. }
  521. public void MeasureUnsafeContainer(int worker, int threadIndex)
  522. {
  523. int insertions = keys.Length;
  524. for (int i = 0; i < insertions; i++)
  525. unsafeContainer.Remove(keys[i]);
  526. }
  527. public void MeasureBclContainer(object container, int worker)
  528. {
  529. var bclContainer = (FakeConcurrentHashSet<int>)container;
  530. int insertions = keys.Length;
  531. for (int i = 0; i < insertions; i++)
  532. bclContainer.Remove(keys[i]);
  533. }
  534. }
  535. struct ParallelHashSetForEach : IBenchmarkContainerParallel
  536. {
  537. NativeParallelHashSet<int> nativeContainer;
  538. UnsafeParallelHashSet<int> unsafeContainer;
  539. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, capacity, true);
  540. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, capacity, true);
  541. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainer(capacity, true);
  542. public void MeasureNativeContainer(int _, int __)
  543. {
  544. int keep = 0;
  545. foreach (var value in nativeContainer)
  546. Volatile.Write(ref keep, value);
  547. }
  548. public void MeasureUnsafeContainer(int _, int __)
  549. {
  550. int keep = 0;
  551. foreach (var value in unsafeContainer)
  552. Volatile.Write(ref keep, value);
  553. }
  554. public void MeasureBclContainer(object container, int _)
  555. {
  556. int keep = 0;
  557. var bclContainer = (FakeConcurrentHashSet<int>)container;
  558. foreach (var value in bclContainer)
  559. Volatile.Write(ref keep, value);
  560. }
  561. }
  562. struct ParallelHashSetUnionWith : IBenchmarkContainerParallel
  563. {
  564. NativeParallelHashSet<int> nativeContainer;
  565. NativeParallelHashSet<int> nativeContainerOther;
  566. UnsafeParallelHashSet<int> unsafeContainer;
  567. UnsafeParallelHashSet<int> unsafeContainerOther;
  568. public int total;
  569. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, ref nativeContainerOther, capacity, true);
  570. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, ref unsafeContainerOther, capacity, true);
  571. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainerTuple(capacity, true);
  572. public void MeasureNativeContainer(int _, int __) => nativeContainer.UnionWith(nativeContainerOther);
  573. public void MeasureUnsafeContainer(int _, int __) => unsafeContainer.UnionWith(unsafeContainerOther);
  574. public void MeasureBclContainer(object container, int _)
  575. {
  576. var dotnetContainer = (System.Tuple<FakeConcurrentHashSet<int>, FakeConcurrentHashSet<int>>)container;
  577. dotnetContainer.Item1.UnionWith(dotnetContainer.Item2);
  578. }
  579. }
  580. struct ParallelHashSetIntersectWith : IBenchmarkContainerParallel
  581. {
  582. NativeParallelHashSet<int> nativeContainer;
  583. NativeParallelHashSet<int> nativeContainerOther;
  584. UnsafeParallelHashSet<int> unsafeContainer;
  585. UnsafeParallelHashSet<int> unsafeContainerOther;
  586. public int total;
  587. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, ref nativeContainerOther, capacity, true);
  588. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, ref unsafeContainerOther, capacity, true);
  589. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainerTuple(capacity, true);
  590. public void MeasureNativeContainer(int _, int __) => nativeContainer.IntersectWith(nativeContainerOther);
  591. public void MeasureUnsafeContainer(int _, int __) => unsafeContainer.IntersectWith(unsafeContainerOther);
  592. public void MeasureBclContainer(object container, int _)
  593. {
  594. var dotnetContainer = (System.Tuple<FakeConcurrentHashSet<int>, FakeConcurrentHashSet<int>>)container;
  595. dotnetContainer.Item1.IntersectWith(dotnetContainer.Item2);
  596. }
  597. }
  598. struct ParallelHashSetExceptWith : IBenchmarkContainerParallel
  599. {
  600. NativeParallelHashSet<int> nativeContainer;
  601. NativeParallelHashSet<int> nativeContainerOther;
  602. UnsafeParallelHashSet<int> unsafeContainer;
  603. UnsafeParallelHashSet<int> unsafeContainerOther;
  604. public int total;
  605. public void AllocNativeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref nativeContainer, ref nativeContainerOther, capacity, true);
  606. public void AllocUnsafeContainer(int capacity) => ParallelHashSetUtil.AllocInt(ref unsafeContainer, ref unsafeContainerOther, capacity, true);
  607. public object AllocBclContainer(int capacity) => ParallelHashSetUtil.AllocBclContainerTuple(capacity, true);
  608. public void MeasureNativeContainer(int _, int __) => nativeContainer.ExceptWith(nativeContainerOther);
  609. public void MeasureUnsafeContainer(int _, int __) => unsafeContainer.ExceptWith(unsafeContainerOther);
  610. public void MeasureBclContainer(object container, int _)
  611. {
  612. var dotnetContainer = (System.Tuple<FakeConcurrentHashSet<int>, FakeConcurrentHashSet<int>>)container;
  613. dotnetContainer.Item1.ExceptWith(dotnetContainer.Item2);
  614. }
  615. }
  616. [Benchmark(typeof(BenchmarkContainerType))]
  617. [BenchmarkNameOverride(BenchmarkContainerConfig.BCL, "HashSet w/lock")]
  618. class ParallelHashSet
  619. {
  620. #if UNITY_EDITOR
  621. [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + nameof(ParallelHashSet))]
  622. static void RunIndividual()
  623. => BenchmarkContainerConfig.RunBenchmark(typeof(ParallelHashSet));
  624. #endif
  625. [Test, Performance]
  626. [Category("Performance")]
  627. public unsafe void IsEmpty_x_100k(
  628. [Values(1, 2, 4)] int workers,
  629. [Values(0, 100)] int capacity,
  630. [Values] BenchmarkContainerType type)
  631. {
  632. BenchmarkContainerRunnerParallel<ParallelHashSetIsEmpty100k>.Run(workers, capacity, type, workers);
  633. }
  634. [Test, Performance]
  635. [Category("Performance")]
  636. public unsafe void Count_x_100k(
  637. [Values(1, 2, 4)] int workers,
  638. [Values(0, 100)] int capacity,
  639. [Values] BenchmarkContainerType type)
  640. {
  641. BenchmarkContainerRunnerParallel<ParallelHashSetCount100k>.Run(workers, capacity, type, workers);
  642. }
  643. [Test, Performance]
  644. [Category("Performance")]
  645. public unsafe void ToNativeArray(
  646. [Values(1)] int workers,
  647. [Values(10000, 100000, 1000000)] int capacity,
  648. [Values] BenchmarkContainerType type)
  649. {
  650. BenchmarkContainerRunnerParallel<ParallelHashSetToNativeArray>.Run(workers, capacity, type);
  651. }
  652. [Test, Performance]
  653. [Category("Performance")]
  654. public unsafe void Insert(
  655. [Values(1, 2, 4)] int workers,
  656. #if UNITY_STANDALONE || UNITY_EDITOR
  657. [Values(10000, 100000, 1000000)] int insertions,
  658. #else
  659. [Values(10000, 100000)] int insertions, // Observe potential lower memory requirement on non-desktop platforms
  660. #endif
  661. [Values] BenchmarkContainerType type)
  662. {
  663. BenchmarkContainerRunnerParallel<ParallelHashSetInsert>.Run(workers, insertions, type, workers);
  664. }
  665. [Test, Performance]
  666. [Category("Performance")]
  667. [BenchmarkTestFootnote("Incrementally grows from `capacity` until reaching size of `growTo`")]
  668. public unsafe void AddGrow(
  669. [Values(1)] int workers, // Can't grow capacity in parallel
  670. [Values(4, 65536)] int capacity,
  671. [Values(1024 * 1024)] int growTo,
  672. [Values] BenchmarkContainerType type)
  673. {
  674. BenchmarkContainerRunnerParallel<ParallelHashSetAddGrow>.Run(workers, capacity, type, growTo);
  675. }
  676. [Test, Performance]
  677. [Category("Performance")]
  678. public unsafe void Contains(
  679. [Values(1, 2, 4)] int workers,
  680. #if UNITY_STANDALONE || UNITY_EDITOR
  681. [Values(10000, 100000, 1000000)] int insertions,
  682. #else
  683. [Values(10000, 100000)] int insertions, // Observe potential lower memory requirement on non-desktop platforms
  684. #endif
  685. [Values] BenchmarkContainerType type)
  686. {
  687. BenchmarkContainerRunnerParallel<ParallelHashSetContains>.Run(workers, insertions, type, workers);
  688. }
  689. [Test, Performance]
  690. [Category("Performance")]
  691. public unsafe void Remove(
  692. [Values(1)] int workers, // No API for ParallelWriter.TryRemove currently
  693. [Values(10000, 100000, 1000000)] int insertions,
  694. [Values] BenchmarkContainerType type)
  695. {
  696. BenchmarkContainerRunnerParallel<ParallelHashSetRemove>.Run(workers, insertions, type, workers);
  697. }
  698. [Test, Performance]
  699. [Category("Performance")]
  700. public unsafe void Foreach(
  701. [Values(1)] int workers, // This work can't be split
  702. [Values(10000, 100000, 1000000)] int insertions,
  703. [Values] BenchmarkContainerType type)
  704. {
  705. BenchmarkContainerRunnerParallel<ParallelHashSetForEach>.Run(workers, insertions, type, workers);
  706. }
  707. [Test, Performance]
  708. [Category("Performance")]
  709. public unsafe void UnionWith(
  710. [Values(1)] int workers, // This work is already split and unrelated to the parallelism of the container
  711. [Values(10000, 100000, 1000000)] int insertions,
  712. [Values] BenchmarkContainerType type)
  713. {
  714. BenchmarkContainerRunnerParallel<ParallelHashSetUnionWith>.Run(workers, insertions, type);
  715. }
  716. [Test, Performance]
  717. [Category("Performance")]
  718. public unsafe void IntersectWith(
  719. [Values(1)] int workers, // This work is already split and unrelated to the parallelism of the container
  720. [Values(10000, 100000, 1000000)] int insertions,
  721. [Values] BenchmarkContainerType type)
  722. {
  723. BenchmarkContainerRunnerParallel<ParallelHashSetIntersectWith>.Run(workers, insertions, type);
  724. }
  725. [Test, Performance]
  726. [Category("Performance")]
  727. public unsafe void ExceptWith(
  728. [Values(1)] int workers, // This work is already split and unrelated to the parallelism of the container
  729. [Values(10000, 100000, 1000000)] int insertions,
  730. [Values] BenchmarkContainerType type)
  731. {
  732. BenchmarkContainerRunnerParallel<ParallelHashSetExceptWith>.Run(workers, insertions, type);
  733. }
  734. }
  735. }