Skip to content

Commit

Permalink
Updated: Legacy Benchmark of FEF
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Apr 28, 2024
1 parent 3a59385 commit 35760ae
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions FileEmulationFramework.Benchmarks/DelegateCallVsDoubleDictLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,32 @@ namespace FileEmulationFramework.Benchmarks;
public class DelegateCallVsDoubleDictLookup
{
private static Swag _staticSwag = new();
// Simulates scenario with two lookups, separate for RegisterCustomFile and Emulated Files.
private Dictionary<nint, ISwag> _dictToInterface = new();
private Dictionary<nint, ISwag> _dictToInterface2 = new();
private Dictionary<nint, Action<IntPtr, IntPtr>> _dictToFunctionPointer = new();

private Dictionary<nint, ISwag> _dictToInterface;
private Dictionary<nint, ISwag> _dictToInterface2;
private Dictionary<nint, Action<IntPtr, IntPtr>> _dictToFunctionPointer;

[Params(2, 4, 8, 16, 32, 64, 128)] public int N;

[GlobalSetup]
public void Setup()
{
_dictToInterface[0] = new Swag();
_dictToInterface2[0] = new Swag(); // We can assume calling emu interface and custom registered file to have same overhead.
_dictToFunctionPointer[0] = _staticSwag.Invoke; // Delegates are slower, but one lookup might be faster
_dictToInterface = new Dictionary<nint, ISwag>(N);
_dictToInterface2 = new Dictionary<nint, ISwag>(N);
_dictToFunctionPointer = new Dictionary<nint, Action<IntPtr, IntPtr>>(N);

for (int i = 0; i < N; i++)
{
_dictToInterface[i] = new Swag();
_dictToInterface2[i] = new Swag();
_dictToFunctionPointer[i] = _staticSwag.Invoke;
}
}

// In these benchmarks we simulate a 1/10 hit rate to give an estimate of emulated to non-emulated files.

[Benchmark]
public void SeparateDicts()
{
for (int x = 0; x < 10; x++)
for (int x = 0; x < N; x++)
{
if (_dictToInterface.TryGetValue(x, out var iFace))
iFace.Invoke(IntPtr.Zero, IntPtr.Zero);
Expand All @@ -39,35 +44,30 @@ public void SeparateDicts()
iFace.Invoke(IntPtr.Zero, IntPtr.Zero);
}
}

[Benchmark]
public void UnifiedLookup()
{
for (int x = 0; x < 10; x++)
for (int x = 0; x < N; x++)
{
if (_dictToFunctionPointer.TryGetValue(x, out var iFace))
iFace(IntPtr.Zero, IntPtr.Zero);
}
}

[Benchmark]
public void SingleInterfaceCall()
{
if (_dictToInterface.TryGetValue(0, out var iFace))
iFace.Invoke(IntPtr.Zero, IntPtr.Zero);
}

[Benchmark]
public void SingleDelegateCall()
{
if (_dictToInterface.TryGetValue(0, out var iFace))
iFace.Invoke(IntPtr.Zero, IntPtr.Zero);
}
}

public class Swag : ISwag
{
public void Invoke(IntPtr a, IntPtr b) { }
public void Invoke(IntPtr a, IntPtr b)
{
}
}

public interface ISwag
Expand Down

0 comments on commit 35760ae

Please sign in to comment.