Add benchmark for archetype indexing by handle vs array access vs field direct access (#3801)

This commit is contained in:
DrSmugleaf
2023-03-11 11:44:16 -08:00
committed by GitHub
parent 357755a65e
commit 52ba22f645
2 changed files with 54 additions and 1 deletions

View File

@@ -482,7 +482,7 @@ public class ArchetypeComponentAccessBenchmark
public struct Struct9{}
public struct Struct10{}
private sealed class Archetype<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>
public sealed class Archetype<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>
{
private int _nextId;
private readonly Dictionary<int, int> _ids;

View File

@@ -0,0 +1,53 @@
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
using static Robust.Benchmarks.EntityManager.ArchetypeComponentAccessBenchmark;
namespace Robust.Benchmarks.EntityManager;
[MemoryDiagnoser]
[Virtual]
public class ArrayAccessBenchmark
{
[Params(new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})]
public int[] Array = default!;
[Params(5)]
public int Element;
[Params(500)]
public int Handle;
public Archetype<int, int, int, int, int, int, int, int, int, int> Archetype = default!;
[GlobalSetup]
public void GlobalSetup()
{
Archetype = new Archetype<int, int, int, int, int, int, int, int, int, int>(1000);
}
[Benchmark]
public int? GetArrayElement()
{
return Consume();
}
[Benchmark]
public int? GetExisting()
{
return Consume(Element);
}
[Benchmark]
public int? GetArchetype()
{
return Consume(Archetype.GetComponentUnsafeHandle<int>(Handle));
}
private int? Consume(int? value = null)
{
if (value == null)
value = Array[5];
return value;
}
}