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.

ListPerformanceTests.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 ListUtil
  11. {
  12. static public void AllocInt(ref NativeList<int> container, int capacity, bool addValues)
  13. {
  14. if (capacity >= 0)
  15. {
  16. Random.InitState(0);
  17. container = new NativeList<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 UnsafeList<int> container, int capacity, bool addValues)
  28. {
  29. if (capacity >= 0)
  30. {
  31. Random.InitState(0);
  32. container = new UnsafeList<int>(capacity, Allocator.Persistent);
  33. if (addValues)
  34. {
  35. for (int i = 0; i < capacity; i++)
  36. container.Add(i);
  37. }
  38. }
  39. else
  40. container.Dispose();
  41. }
  42. static public object AllocBclContainer(int capacity, bool addValues)
  43. {
  44. if (capacity < 0)
  45. return null;
  46. Random.InitState(0);
  47. var bclContainer = new System.Collections.Generic.List<int>(capacity);
  48. if (addValues)
  49. {
  50. for (int i = 0; i < capacity; i++)
  51. bclContainer.Add(i);
  52. }
  53. return bclContainer;
  54. }
  55. static public void CreateRandomValues(int capacity, ref UnsafeList<int> values)
  56. {
  57. if (capacity >= 0)
  58. {
  59. values = new UnsafeList<int>(capacity, Allocator.Persistent);
  60. Random.InitState(0);
  61. for (int i = 0; i < capacity; i++)
  62. {
  63. int randKey = Random.Range(0, capacity);
  64. values.Add(randKey);
  65. }
  66. }
  67. else
  68. values.Dispose();
  69. }
  70. }
  71. struct ListIsEmpty100k : IBenchmarkContainer
  72. {
  73. const int kIterations = 100_000;
  74. NativeList<int> nativeContainer;
  75. UnsafeList<int> unsafeContainer;
  76. public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
  77. public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  78. public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
  79. [MethodImpl(MethodImplOptions.NoOptimization)]
  80. public void MeasureNativeContainer()
  81. {
  82. for (int i = 0; i < kIterations; i++)
  83. _ = nativeContainer.IsEmpty;
  84. }
  85. [MethodImpl(MethodImplOptions.NoOptimization)]
  86. public void MeasureUnsafeContainer()
  87. {
  88. for (int i = 0; i < kIterations; i++)
  89. _ = unsafeContainer.IsEmpty;
  90. }
  91. [MethodImpl(MethodImplOptions.NoOptimization)]
  92. public void MeasureBclContainer(object container)
  93. {
  94. var bclContainer = (System.Collections.Generic.List<int>)container;
  95. for (int i = 0; i < kIterations; i++)
  96. _ = bclContainer.Count == 0;
  97. }
  98. }
  99. struct ListCount100k : IBenchmarkContainer
  100. {
  101. const int kIterations = 100_000;
  102. NativeList<int> nativeContainer;
  103. UnsafeList<int> unsafeContainer;
  104. public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
  105. public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  106. public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
  107. [MethodImpl(MethodImplOptions.NoOptimization)]
  108. public void MeasureNativeContainer()
  109. {
  110. var reader = nativeContainer.AsReadOnly();
  111. for (int i = 0; i < kIterations; i++)
  112. _ = reader.Length;
  113. }
  114. [MethodImpl(MethodImplOptions.NoOptimization)]
  115. public void MeasureUnsafeContainer()
  116. {
  117. var reader = unsafeContainer.AsReadOnly();
  118. for (int i = 0; i < kIterations; i++)
  119. _ = reader.Length;
  120. }
  121. [MethodImpl(MethodImplOptions.NoOptimization)]
  122. public void MeasureBclContainer(object container)
  123. {
  124. var bclContainer = (System.Collections.Generic.List<int>)container;
  125. for (int i = 0; i < kIterations; i++)
  126. _ = bclContainer.Count;
  127. }
  128. }
  129. struct ListToNativeArray : IBenchmarkContainer
  130. {
  131. NativeList<int> nativeContainer;
  132. public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
  133. public void AllocUnsafeContainer(int capacity) { }
  134. public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
  135. public void MeasureNativeContainer()
  136. {
  137. var asArray = nativeContainer.ToArray(Allocator.Temp);
  138. asArray.Dispose();
  139. }
  140. public void MeasureUnsafeContainer() { }
  141. public void MeasureBclContainer(object container)
  142. {
  143. var bclContainer = (System.Collections.Generic.List<int>)container;
  144. int[] asArray = new int[bclContainer.Count];
  145. bclContainer.CopyTo(asArray, 0);
  146. }
  147. }
  148. struct ListAdd : IBenchmarkContainer
  149. {
  150. int capacity;
  151. NativeList<int> nativeContainer;
  152. UnsafeList<int> unsafeContainer;
  153. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  154. public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, false);
  155. public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, false);
  156. public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, false);
  157. public void MeasureNativeContainer()
  158. {
  159. for (int i = 0; i < capacity; i++)
  160. nativeContainer.Add(i);
  161. }
  162. public void MeasureUnsafeContainer()
  163. {
  164. for (int i = 0; i < capacity; i++)
  165. unsafeContainer.Add(i);
  166. }
  167. public void MeasureBclContainer(object container)
  168. {
  169. var bclContainer = (System.Collections.Generic.List<int>)container;
  170. for (int i = 0; i < capacity; i++)
  171. bclContainer.Add(i);
  172. }
  173. }
  174. struct ListAddGrow : IBenchmarkContainer
  175. {
  176. int capacity;
  177. int toAdd;
  178. NativeList<int> nativeContainer;
  179. UnsafeList<int> unsafeContainer;
  180. void IBenchmarkContainer.SetParams(int capacity, params int[] args)
  181. {
  182. this.capacity = capacity;
  183. toAdd = args[0];
  184. }
  185. public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
  186. public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  187. public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
  188. public void MeasureNativeContainer()
  189. {
  190. // Intentionally setting capacity small and growing by adding more items
  191. for (int i = capacity; i < capacity + toAdd; i++)
  192. nativeContainer.Add(i);
  193. }
  194. public void MeasureUnsafeContainer()
  195. {
  196. // Intentionally setting capacity small and growing by adding more items
  197. for (int i = capacity; i < capacity + toAdd; i++)
  198. unsafeContainer.Add(i);
  199. }
  200. public void MeasureBclContainer(object container)
  201. {
  202. var bclContainer = (System.Collections.Generic.List<int>)container;
  203. // Intentionally setting capacity small and growing by adding more items
  204. for (int i = capacity; i < capacity + toAdd; i++)
  205. bclContainer.Add(i);
  206. }
  207. }
  208. struct ListContains : IBenchmarkContainer
  209. {
  210. int capacity;
  211. NativeList<int> nativeContainer;
  212. UnsafeList<int> unsafeContainer;
  213. UnsafeList<int> values;
  214. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  215. public void AllocNativeContainer(int capacity)
  216. {
  217. ListUtil.AllocInt(ref nativeContainer, capacity, false);
  218. ListUtil.CreateRandomValues(capacity, ref values);
  219. for (int i = 0; i < capacity; i++)
  220. nativeContainer.Add(values[i]);
  221. }
  222. public void AllocUnsafeContainer(int capacity)
  223. {
  224. ListUtil.AllocInt(ref unsafeContainer, capacity, false);
  225. ListUtil.CreateRandomValues(capacity, ref values);
  226. for (int i = 0; i < capacity; i++)
  227. unsafeContainer.Add(values[i]);
  228. }
  229. public object AllocBclContainer(int capacity)
  230. {
  231. object container = ListUtil.AllocBclContainer(capacity, false);
  232. var bclContainer = (System.Collections.Generic.List<int>)container;
  233. ListUtil.CreateRandomValues(capacity, ref values);
  234. for (int i = 0; i < capacity; i++)
  235. bclContainer.Add(values[i]);
  236. return container;
  237. }
  238. public void MeasureNativeContainer()
  239. {
  240. var reader = nativeContainer.AsReadOnly();
  241. bool data = false;
  242. for (int i = 0; i < capacity; i++)
  243. Volatile.Write(ref data, reader.Contains(values[i]));
  244. }
  245. public void MeasureUnsafeContainer()
  246. {
  247. var reader = unsafeContainer.AsReadOnly();
  248. bool data = false;
  249. for (int i = 0; i < capacity; i++)
  250. Volatile.Write(ref data, reader.Contains(values[i]));
  251. }
  252. public void MeasureBclContainer(object container)
  253. {
  254. var bclContainer = (System.Collections.Generic.List<int>)container;
  255. bool data = false;
  256. for (int i = 0; i < capacity; i++)
  257. Volatile.Write(ref data, bclContainer.Contains(values[i]));
  258. }
  259. }
  260. struct ListIndexedRead : IBenchmarkContainer
  261. {
  262. NativeList<int> nativeContainer;
  263. UnsafeList<int> unsafeContainer;
  264. UnsafeList<int> values;
  265. public void AllocNativeContainer(int capacity)
  266. {
  267. ListUtil.AllocInt(ref nativeContainer, capacity, true);
  268. ListUtil.CreateRandomValues(capacity, ref values);
  269. }
  270. public void AllocUnsafeContainer(int capacity)
  271. {
  272. ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  273. ListUtil.CreateRandomValues(capacity, ref values);
  274. }
  275. public object AllocBclContainer(int capacity)
  276. {
  277. object container = ListUtil.AllocBclContainer(capacity, true);
  278. ListUtil.CreateRandomValues(capacity, ref values);
  279. return container;
  280. }
  281. public void MeasureNativeContainer()
  282. {
  283. var reader = nativeContainer.AsReadOnly();
  284. int insertions = values.Length;
  285. int value = 0;
  286. for (int i = 0; i < insertions; i++)
  287. Volatile.Write(ref value, reader[values[i]]);
  288. }
  289. public void MeasureUnsafeContainer()
  290. {
  291. int insertions = values.Length;
  292. int value = 0;
  293. for (int i = 0; i < insertions; i++)
  294. Volatile.Write(ref value, unsafeContainer[values[i]]);
  295. }
  296. public void MeasureBclContainer(object container)
  297. {
  298. var bclContainer = (System.Collections.Generic.List<int>)container;
  299. int insertions = values.Length;
  300. int value = 0;
  301. for (int i = 0; i < insertions; i++)
  302. Volatile.Write(ref value, bclContainer[values[i]]);
  303. }
  304. }
  305. struct ListIndexedWrite : IBenchmarkContainer
  306. {
  307. NativeList<int> nativeContainer;
  308. UnsafeList<int> unsafeContainer;
  309. UnsafeList<int> values;
  310. public void AllocNativeContainer(int capacity)
  311. {
  312. ListUtil.AllocInt(ref nativeContainer, capacity, true);
  313. ListUtil.CreateRandomValues(capacity, ref values);
  314. }
  315. public void AllocUnsafeContainer(int capacity)
  316. {
  317. ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  318. ListUtil.CreateRandomValues(capacity, ref values);
  319. }
  320. public object AllocBclContainer(int capacity)
  321. {
  322. object container = ListUtil.AllocBclContainer(capacity, true);
  323. ListUtil.CreateRandomValues(capacity, ref values);
  324. return container;
  325. }
  326. public void MeasureNativeContainer()
  327. {
  328. int insertions = values.Length;
  329. for (int i = 0; i < insertions; i++)
  330. nativeContainer[values[i]] = i;
  331. }
  332. public void MeasureUnsafeContainer()
  333. {
  334. int insertions = values.Length;
  335. for (int i = 0; i < insertions; i++)
  336. unsafeContainer[values[i]] = i;
  337. }
  338. public void MeasureBclContainer(object container)
  339. {
  340. var bclContainer = (System.Collections.Generic.List<int>)container;
  341. int insertions = values.Length;
  342. for (int i = 0; i < insertions; i++)
  343. bclContainer[values[i]] = i;
  344. }
  345. }
  346. struct ListRemove : IBenchmarkContainer
  347. {
  348. NativeList<int> nativeContainer;
  349. UnsafeList<int> unsafeContainer;
  350. UnsafeList<int> values;
  351. void FixValues()
  352. {
  353. // Ensure if we iterate this list and remove a random index, it will always be a valid index given how many elements still remain.
  354. int max = values.Length;
  355. while (--max >= 0)
  356. {
  357. int reverseIndex = values.Length - 1 - max;
  358. int value = values[reverseIndex];
  359. if (value > max)
  360. values[reverseIndex] = max;
  361. }
  362. }
  363. public void AllocNativeContainer(int capacity)
  364. {
  365. ListUtil.AllocInt(ref nativeContainer, capacity, true);
  366. ListUtil.CreateRandomValues(capacity, ref values);
  367. FixValues();
  368. }
  369. public void AllocUnsafeContainer(int capacity)
  370. {
  371. ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  372. ListUtil.CreateRandomValues(capacity, ref values);
  373. FixValues();
  374. }
  375. public object AllocBclContainer(int capacity)
  376. {
  377. object container = ListUtil.AllocBclContainer(capacity, true);
  378. ListUtil.CreateRandomValues(capacity, ref values);
  379. FixValues();
  380. return container;
  381. }
  382. public void MeasureNativeContainer()
  383. {
  384. int insertions = values.Length;
  385. for (int i = 0; i < insertions; i++)
  386. nativeContainer.RemoveAt(values[i]);
  387. }
  388. public void MeasureUnsafeContainer()
  389. {
  390. int insertions = values.Length;
  391. for (int i = 0; i < insertions; i++)
  392. unsafeContainer.RemoveAt(values[i]);
  393. }
  394. public void MeasureBclContainer(object container)
  395. {
  396. var bclContainer = (System.Collections.Generic.List<int>)container;
  397. int insertions = values.Length;
  398. for (int i = 0; i < insertions; i++)
  399. bclContainer.RemoveAt(values[i]);
  400. }
  401. }
  402. struct ListForEach : IBenchmarkContainer
  403. {
  404. NativeList<int> nativeContainer;
  405. UnsafeList<int> unsafeContainer;
  406. public int total;
  407. public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
  408. public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
  409. public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
  410. public void MeasureNativeContainer()
  411. {
  412. int value = 0;
  413. foreach (var element in nativeContainer)
  414. Volatile.Write(ref value, element);
  415. }
  416. public void MeasureUnsafeContainer()
  417. {
  418. int value = 0;
  419. foreach (var element in unsafeContainer)
  420. Volatile.Write(ref value, element);
  421. }
  422. public void MeasureBclContainer(object container)
  423. {
  424. int value = 0;
  425. var bclContainer = (System.Collections.Generic.List<int>)container;
  426. foreach (var element in bclContainer)
  427. Volatile.Write(ref value, element);
  428. }
  429. }
  430. [Benchmark(typeof(BenchmarkContainerType))]
  431. class List
  432. {
  433. #if UNITY_EDITOR
  434. [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + nameof(List))]
  435. static void RunIndividual()
  436. => BenchmarkContainerConfig.RunBenchmark(typeof(List));
  437. #endif
  438. [Test, Performance]
  439. [Category("Performance")]
  440. public unsafe void IsEmpty_x_100k(
  441. [Values(0, 100)] int capacity,
  442. [Values] BenchmarkContainerType type)
  443. {
  444. BenchmarkContainerRunner<ListIsEmpty100k>.Run(capacity, type);
  445. }
  446. [Test, Performance]
  447. [Category("Performance")]
  448. public unsafe void Count_x_100k(
  449. [Values(0, 100)] int capacity,
  450. [Values] BenchmarkContainerType type)
  451. {
  452. BenchmarkContainerRunner<ListCount100k>.Run(capacity, type);
  453. }
  454. [Test, Performance]
  455. [Category("Performance")]
  456. public unsafe void ToNativeArray(
  457. [Values(10000, 100000, 1000000)] int capacity,
  458. [Values(BenchmarkContainerType.Native, BenchmarkContainerType.NativeBurstSafety,
  459. BenchmarkContainerType.NativeBurstNoSafety)] BenchmarkContainerType type)
  460. {
  461. BenchmarkContainerRunner<ListToNativeArray>.Run(capacity, type);
  462. }
  463. [Test, Performance]
  464. [Category("Performance")]
  465. public unsafe void Add(
  466. [Values(10000, 100000, 1000000)] int insertions,
  467. [Values] BenchmarkContainerType type)
  468. {
  469. BenchmarkContainerRunner<ListAdd>.Run(insertions, type);
  470. }
  471. [Test, Performance]
  472. [Category("Performance")]
  473. [BenchmarkTestFootnote("Incrementally grows from `capacity` until reaching size of `growTo`")]
  474. public unsafe void AddGrow(
  475. [Values(4, 65536)] int capacity,
  476. [Values(1024 * 1024)] int growTo,
  477. [Values] BenchmarkContainerType type)
  478. {
  479. BenchmarkContainerRunner<ListAddGrow>.Run(capacity, type, growTo);
  480. }
  481. [Test, Performance]
  482. [Category("Performance")]
  483. public unsafe void Contains(
  484. [Values(1000, 10000)] int insertions,
  485. [Values] BenchmarkContainerType type)
  486. {
  487. BenchmarkContainerRunner<ListContains>.Run(insertions, type);
  488. }
  489. [Test, Performance]
  490. [Category("Performance")]
  491. public unsafe void IndexedRead(
  492. [Values(10000, 100000, 1000000)] int insertions,
  493. [Values] BenchmarkContainerType type)
  494. {
  495. BenchmarkContainerRunner<ListIndexedRead>.Run(insertions, type);
  496. }
  497. [Test, Performance]
  498. [Category("Performance")]
  499. public unsafe void IndexedWrite(
  500. [Values(10000, 100000, 1000000)] int insertions,
  501. [Values] BenchmarkContainerType type)
  502. {
  503. BenchmarkContainerRunner<ListIndexedWrite>.Run(insertions, type);
  504. }
  505. [Test, Performance]
  506. [Category("Performance")]
  507. public unsafe void Remove(
  508. [Values(1000, 10000)] int insertions,
  509. [Values] BenchmarkContainerType type)
  510. {
  511. BenchmarkContainerRunner<ListRemove>.Run(insertions, type);
  512. }
  513. [Test, Performance]
  514. [Category("Performance")]
  515. public unsafe void Foreach(
  516. [Values(10000, 100000, 1000000)] int insertions,
  517. [Values] BenchmarkContainerType type)
  518. {
  519. BenchmarkContainerRunner<ListForEach>.Run(insertions, type);
  520. }
  521. }
  522. }