123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- using NUnit.Framework;
- using UnityEngine;
- using Unity.Collections.LowLevel.Unsafe;
- using Unity.PerformanceTesting;
- using Unity.PerformanceTesting.Benchmark;
- using System.Runtime.CompilerServices;
- using System.Threading;
-
- namespace Unity.Collections.PerformanceTests
- {
- static class ListUtil
- {
- static public void AllocInt(ref NativeList<int> container, int capacity, bool addValues)
- {
- if (capacity >= 0)
- {
- Random.InitState(0);
- container = new NativeList<int>(capacity, Allocator.Persistent);
- if (addValues)
- {
- for (int i = 0; i < capacity; i++)
- container.Add(i);
- }
- }
- else
- container.Dispose();
- }
- static public void AllocInt(ref UnsafeList<int> container, int capacity, bool addValues)
- {
- if (capacity >= 0)
- {
- Random.InitState(0);
- container = new UnsafeList<int>(capacity, Allocator.Persistent);
- if (addValues)
- {
- for (int i = 0; i < capacity; i++)
- container.Add(i);
- }
- }
- else
- container.Dispose();
- }
- static public object AllocBclContainer(int capacity, bool addValues)
- {
- if (capacity < 0)
- return null;
-
- Random.InitState(0);
- var bclContainer = new System.Collections.Generic.List<int>(capacity);
- if (addValues)
- {
- for (int i = 0; i < capacity; i++)
- bclContainer.Add(i);
- }
- return bclContainer;
- }
-
- static public void CreateRandomValues(int capacity, ref UnsafeList<int> values)
- {
- if (capacity >= 0)
- {
- values = new UnsafeList<int>(capacity, Allocator.Persistent);
- Random.InitState(0);
- for (int i = 0; i < capacity; i++)
- {
- int randKey = Random.Range(0, capacity);
- values.Add(randKey);
- }
- }
- else
- values.Dispose();
- }
- }
-
- struct ListIsEmpty100k : IBenchmarkContainer
- {
- const int kIterations = 100_000;
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
-
- public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
- public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
-
- [MethodImpl(MethodImplOptions.NoOptimization)]
- public void MeasureNativeContainer()
- {
- for (int i = 0; i < kIterations; i++)
- _ = nativeContainer.IsEmpty;
- }
- [MethodImpl(MethodImplOptions.NoOptimization)]
- public void MeasureUnsafeContainer()
- {
- for (int i = 0; i < kIterations; i++)
- _ = unsafeContainer.IsEmpty;
- }
- [MethodImpl(MethodImplOptions.NoOptimization)]
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- for (int i = 0; i < kIterations; i++)
- _ = bclContainer.Count == 0;
- }
- }
-
- struct ListCount100k : IBenchmarkContainer
- {
- const int kIterations = 100_000;
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
-
- public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
- public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
-
- [MethodImpl(MethodImplOptions.NoOptimization)]
- public void MeasureNativeContainer()
- {
- var reader = nativeContainer.AsReadOnly();
- for (int i = 0; i < kIterations; i++)
- _ = reader.Length;
- }
- [MethodImpl(MethodImplOptions.NoOptimization)]
- public void MeasureUnsafeContainer()
- {
- var reader = unsafeContainer.AsReadOnly();
- for (int i = 0; i < kIterations; i++)
- _ = reader.Length;
- }
- [MethodImpl(MethodImplOptions.NoOptimization)]
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- for (int i = 0; i < kIterations; i++)
- _ = bclContainer.Count;
- }
- }
-
- struct ListToNativeArray : IBenchmarkContainer
- {
- NativeList<int> nativeContainer;
-
- public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
- public void AllocUnsafeContainer(int capacity) { }
- public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
-
- public void MeasureNativeContainer()
- {
- var asArray = nativeContainer.ToArray(Allocator.Temp);
- asArray.Dispose();
- }
- public void MeasureUnsafeContainer() { }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- int[] asArray = new int[bclContainer.Count];
- bclContainer.CopyTo(asArray, 0);
- }
- }
-
- struct ListAdd : IBenchmarkContainer
- {
- int capacity;
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
-
- void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
-
- public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, false);
- public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, false);
- public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, false);
-
- public void MeasureNativeContainer()
- {
- for (int i = 0; i < capacity; i++)
- nativeContainer.Add(i);
- }
- public void MeasureUnsafeContainer()
- {
- for (int i = 0; i < capacity; i++)
- unsafeContainer.Add(i);
- }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- for (int i = 0; i < capacity; i++)
- bclContainer.Add(i);
- }
- }
-
- struct ListAddGrow : IBenchmarkContainer
- {
- int capacity;
- int toAdd;
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
-
- void IBenchmarkContainer.SetParams(int capacity, params int[] args)
- {
- this.capacity = capacity;
- toAdd = args[0];
- }
-
- public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
- public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
-
- public void MeasureNativeContainer()
- {
- // Intentionally setting capacity small and growing by adding more items
- for (int i = capacity; i < capacity + toAdd; i++)
- nativeContainer.Add(i);
- }
- public void MeasureUnsafeContainer()
- {
- // Intentionally setting capacity small and growing by adding more items
- for (int i = capacity; i < capacity + toAdd; i++)
- unsafeContainer.Add(i);
- }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- // Intentionally setting capacity small and growing by adding more items
- for (int i = capacity; i < capacity + toAdd; i++)
- bclContainer.Add(i);
- }
- }
-
- struct ListContains : IBenchmarkContainer
- {
- int capacity;
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
- UnsafeList<int> values;
-
- void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
-
- public void AllocNativeContainer(int capacity)
- {
- ListUtil.AllocInt(ref nativeContainer, capacity, false);
- ListUtil.CreateRandomValues(capacity, ref values);
- for (int i = 0; i < capacity; i++)
- nativeContainer.Add(values[i]);
- }
- public void AllocUnsafeContainer(int capacity)
- {
- ListUtil.AllocInt(ref unsafeContainer, capacity, false);
- ListUtil.CreateRandomValues(capacity, ref values);
- for (int i = 0; i < capacity; i++)
- unsafeContainer.Add(values[i]);
- }
- public object AllocBclContainer(int capacity)
- {
- object container = ListUtil.AllocBclContainer(capacity, false);
- var bclContainer = (System.Collections.Generic.List<int>)container;
- ListUtil.CreateRandomValues(capacity, ref values);
- for (int i = 0; i < capacity; i++)
- bclContainer.Add(values[i]);
- return container;
- }
-
- public void MeasureNativeContainer()
- {
- var reader = nativeContainer.AsReadOnly();
- bool data = false;
- for (int i = 0; i < capacity; i++)
- Volatile.Write(ref data, reader.Contains(values[i]));
- }
- public void MeasureUnsafeContainer()
- {
- var reader = unsafeContainer.AsReadOnly();
- bool data = false;
- for (int i = 0; i < capacity; i++)
- Volatile.Write(ref data, reader.Contains(values[i]));
- }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- bool data = false;
- for (int i = 0; i < capacity; i++)
- Volatile.Write(ref data, bclContainer.Contains(values[i]));
- }
- }
-
- struct ListIndexedRead : IBenchmarkContainer
- {
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
- UnsafeList<int> values;
-
- public void AllocNativeContainer(int capacity)
- {
- ListUtil.AllocInt(ref nativeContainer, capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- }
- public void AllocUnsafeContainer(int capacity)
- {
- ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- }
- public object AllocBclContainer(int capacity)
- {
- object container = ListUtil.AllocBclContainer(capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- return container;
- }
-
- public void MeasureNativeContainer()
- {
- var reader = nativeContainer.AsReadOnly();
- int insertions = values.Length;
- int value = 0;
- for (int i = 0; i < insertions; i++)
- Volatile.Write(ref value, reader[values[i]]);
- }
- public void MeasureUnsafeContainer()
- {
- int insertions = values.Length;
- int value = 0;
- for (int i = 0; i < insertions; i++)
- Volatile.Write(ref value, unsafeContainer[values[i]]);
- }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- int insertions = values.Length;
- int value = 0;
- for (int i = 0; i < insertions; i++)
- Volatile.Write(ref value, bclContainer[values[i]]);
- }
- }
-
- struct ListIndexedWrite : IBenchmarkContainer
- {
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
- UnsafeList<int> values;
-
- public void AllocNativeContainer(int capacity)
- {
- ListUtil.AllocInt(ref nativeContainer, capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- }
- public void AllocUnsafeContainer(int capacity)
- {
- ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- }
- public object AllocBclContainer(int capacity)
- {
- object container = ListUtil.AllocBclContainer(capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- return container;
- }
-
- public void MeasureNativeContainer()
- {
- int insertions = values.Length;
- for (int i = 0; i < insertions; i++)
- nativeContainer[values[i]] = i;
- }
- public void MeasureUnsafeContainer()
- {
- int insertions = values.Length;
- for (int i = 0; i < insertions; i++)
- unsafeContainer[values[i]] = i;
- }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- int insertions = values.Length;
- for (int i = 0; i < insertions; i++)
- bclContainer[values[i]] = i;
- }
- }
-
- struct ListRemove : IBenchmarkContainer
- {
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
- UnsafeList<int> values;
-
- void FixValues()
- {
- // Ensure if we iterate this list and remove a random index, it will always be a valid index given how many elements still remain.
- int max = values.Length;
- while (--max >= 0)
- {
- int reverseIndex = values.Length - 1 - max;
- int value = values[reverseIndex];
- if (value > max)
- values[reverseIndex] = max;
- }
- }
- public void AllocNativeContainer(int capacity)
- {
- ListUtil.AllocInt(ref nativeContainer, capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- FixValues();
- }
- public void AllocUnsafeContainer(int capacity)
- {
- ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- FixValues();
- }
- public object AllocBclContainer(int capacity)
- {
- object container = ListUtil.AllocBclContainer(capacity, true);
- ListUtil.CreateRandomValues(capacity, ref values);
- FixValues();
- return container;
- }
-
- public void MeasureNativeContainer()
- {
- int insertions = values.Length;
- for (int i = 0; i < insertions; i++)
- nativeContainer.RemoveAt(values[i]);
- }
- public void MeasureUnsafeContainer()
- {
- int insertions = values.Length;
- for (int i = 0; i < insertions; i++)
- unsafeContainer.RemoveAt(values[i]);
- }
- public void MeasureBclContainer(object container)
- {
- var bclContainer = (System.Collections.Generic.List<int>)container;
- int insertions = values.Length;
- for (int i = 0; i < insertions; i++)
- bclContainer.RemoveAt(values[i]);
- }
- }
-
- struct ListForEach : IBenchmarkContainer
- {
- NativeList<int> nativeContainer;
- UnsafeList<int> unsafeContainer;
- public int total;
-
- public void AllocNativeContainer(int capacity) => ListUtil.AllocInt(ref nativeContainer, capacity, true);
- public void AllocUnsafeContainer(int capacity) => ListUtil.AllocInt(ref unsafeContainer, capacity, true);
- public object AllocBclContainer(int capacity) => ListUtil.AllocBclContainer(capacity, true);
-
- public void MeasureNativeContainer()
- {
- int value = 0;
- foreach (var element in nativeContainer)
- Volatile.Write(ref value, element);
- }
- public void MeasureUnsafeContainer()
- {
- int value = 0;
- foreach (var element in unsafeContainer)
- Volatile.Write(ref value, element);
- }
- public void MeasureBclContainer(object container)
- {
- int value = 0;
- var bclContainer = (System.Collections.Generic.List<int>)container;
- foreach (var element in bclContainer)
- Volatile.Write(ref value, element);
- }
- }
-
-
- [Benchmark(typeof(BenchmarkContainerType))]
- class List
- {
- #if UNITY_EDITOR
- [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + nameof(List))]
- static void RunIndividual()
- => BenchmarkContainerConfig.RunBenchmark(typeof(List));
- #endif
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void IsEmpty_x_100k(
- [Values(0, 100)] int capacity,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListIsEmpty100k>.Run(capacity, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void Count_x_100k(
- [Values(0, 100)] int capacity,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListCount100k>.Run(capacity, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void ToNativeArray(
- [Values(10000, 100000, 1000000)] int capacity,
- [Values(BenchmarkContainerType.Native, BenchmarkContainerType.NativeBurstSafety,
- BenchmarkContainerType.NativeBurstNoSafety)] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListToNativeArray>.Run(capacity, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void Add(
- [Values(10000, 100000, 1000000)] int insertions,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListAdd>.Run(insertions, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- [BenchmarkTestFootnote("Incrementally grows from `capacity` until reaching size of `growTo`")]
- public unsafe void AddGrow(
- [Values(4, 65536)] int capacity,
- [Values(1024 * 1024)] int growTo,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListAddGrow>.Run(capacity, type, growTo);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void Contains(
- [Values(1000, 10000)] int insertions,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListContains>.Run(insertions, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void IndexedRead(
- [Values(10000, 100000, 1000000)] int insertions,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListIndexedRead>.Run(insertions, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void IndexedWrite(
- [Values(10000, 100000, 1000000)] int insertions,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListIndexedWrite>.Run(insertions, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void Remove(
- [Values(1000, 10000)] int insertions,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListRemove>.Run(insertions, type);
- }
-
- [Test, Performance]
- [Category("Performance")]
- public unsafe void Foreach(
- [Values(10000, 100000, 1000000)] int insertions,
- [Values] BenchmarkContainerType type)
- {
- BenchmarkContainerRunner<ListForEach>.Run(insertions, type);
- }
- }
- }
|