Files

133 lines
2.9 KiB
C#
Raw Permalink Normal View History

2025-08-16 12:14:03 +03:00
using BenchmarkDotNet.Attributes;
using mROA.Abstract;
using mROA.Codegen;
using mROA.Implementation;
namespace mROA.Benchmark;
[MemoryDiagnoser]
2025-09-24 00:11:10 +03:00
// [DisassemblyDiagnoser]
2025-08-16 12:14:03 +03:00
public class MethodAccess
{
2025-09-24 00:11:10 +03:00
private CollectableMethodRepository _current = new();
2025-08-16 12:14:03 +03:00
private FastMethodRepository _fast = new();
private readonly int _count = new GeneratedInvokersCollection().Count;
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
[GlobalSetup]
public void SetupMethodAccess()
{
_current.AppendInvokers(new GeneratedInvokersCollection());
_fast.AppendInvokers(new GeneratedInvokersCollection());
}
// [Benchmark(Baseline = true)]
// public IMethodInvoker CurrentSingle()
// {
// return _current.GetMethod(0);
// }
//
// [Benchmark]
// public IMethodInvoker CurrentDispose()
// {
// return _current.GetMethod(-1);
// }
//
// [Benchmark]
// public IMethodInvoker FastSingle()
// {
// return _fast.GetMethod(0);
// }
//
// [Benchmark]
// public IMethodInvoker FastDispose()
// {
// return _fast.GetMethod(-1);
// }
// [Benchmark]
// public IMethodInvoker FastSingleBaked()
// {
// return _fast.GetMethodBaked(0);
// }
//
// [Benchmark]
// public IMethodInvoker FastDisposeBaked()
// {
// return _fast.GetMethodBaked(-1);
// }
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
[Benchmark(Baseline = true)]
public IMethodInvoker CurrentAll()
{
IMethodInvoker inv = null;
for (int i = -1; i < _count; i++)
{
inv = _current.GetMethod(i);
}
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
return inv;
}
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
[Benchmark]
public IMethodInvoker FastAll()
{
IMethodInvoker inv = null;
for (int i = -1; i < _count; i++)
{
inv = _fast.GetMethod(i);
}
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
return inv;
}
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
[Benchmark]
public IMethodInvoker FastAllBaked()
{
IMethodInvoker inv = null;
for (int i = -1; i < _count; i++)
{
inv = _fast.GetMethodBaked(i);
}
2025-09-24 00:11:10 +03:00
return inv;
}
[Benchmark]
public IMethodInvoker FastAllBakedPreicrement()
{
IMethodInvoker inv = null;
for (int i = -1; i < _count; i++)
{
inv = _fast.GetMethodBakedPreicrement(i);
}
2025-08-16 12:14:03 +03:00
return inv;
}
}
2025-09-24 00:11:10 +03:00
internal class FastMethodRepository : IMethodRepository
2025-08-16 12:14:03 +03:00
{
private readonly List<IMethodInvoker> _methods = [MethodInvoker.Dispose];
private IMethodInvoker[] _baked = [];
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
public void AppendInvokers(IEnumerable<IMethodInvoker> methodInvokers)
{
_methods.AddRange(methodInvokers);
_baked = _methods.ToArray();
}
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
public IMethodInvoker GetMethod(int id)
{
return _methods[id + 1];
}
2025-09-24 00:11:10 +03:00
2025-08-16 12:14:03 +03:00
public IMethodInvoker GetMethodBaked(int id)
{
return _baked[id + 1];
}
2025-09-24 00:11:10 +03:00
public IMethodInvoker GetMethodBakedPreicrement(int id)
{
return _baked[++id];
}
2025-08-16 12:14:03 +03:00
}