Skip to content

Commit

Permalink
added: "close to real" RentReturnRent ObjectPoolComparison benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Jun 18, 2018
1 parent 75b8dd3 commit 56b7f53
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 17 deletions.
121 changes: 105 additions & 16 deletions src/Playground/ObjectPoolComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Playground
{
[MemoryDiagnoser, DisassemblyDiagnoser]
public class ObjectPoolComparison_AccessPrefilledPools
public class ObjectPoolComparison_RentReturnAndRentPrefilledPool
{
private ScanPool<X> _scanPool;
private StackPool<X> _stackPool;
Expand All @@ -15,8 +15,91 @@ public void CreateAndPopulatePools()
{
_scanPool = new ScanPool<X>();
_scanPool.Return(new X(false, 0, null));
_scanPool.ScanReturn(new X(false, 0, null));
_scanPool.ScanReturn(new X(false, 0, null));

_stackPool = new StackPool<X>();
_stackPool.Return(new X(false, 0, null));
_stackPool.Return(new X(false, 0, null));
}

[Benchmark(Baseline = true)]
public int New_4Times()
{
var x1 = new X(true, 1, "1");
var x2 = new X(true, 2, "2");

x1 = new X(false, 3, "3");
x2 = new X(false, 4, "4");

return x1.I + x2.I;
}

[Benchmark]
public int StackPool_2Rents2Returns2Rents()
{
var p = _stackPool;

var x1 = p.RentOrNew(true, 1, "1");
var x2 = p.RentOrNew(true, 2, "2");

p.Return(x1);
p.Return(x2);

x1 = p.RentOrNew(false, 3, "3");
x2 = p.RentOrNew(false, 4, "4");

return x1.I + x2.I;
}

[Benchmark]
public int ScanPool_2Rents2Returns2Rents()
{
var p = _scanPool;

var x1 = p.RentOrNew(true, 1, "1");
var x2 = p.RentOrNew(true, 2, "2");

p.Return(x1);
p.Return(x2);

x1 = p.RentOrNew(false, 3, "3");
x2 = p.RentOrNew(false, 4, "4");

return x1.I + x2.I;
}

[Benchmark]
public int ScanPool_ScanOnly_2Rents2Returns2Rents()
{
var p = _scanPool;

var x1 = p.ScanRentOrNew(true, 1, "1");
var x2 = p.ScanRentOrNew(true, 2, "2");

p.ScanReturn(x1);
p.ScanReturn(x2);

x1 = p.ScanRentOrNew(false, 3, "3");
x2 = p.ScanRentOrNew(false, 4, "4");

return x1.I + x2.I;
}
}

[MemoryDiagnoser, DisassemblyDiagnoser]
public class ObjectPoolComparison_RentPrefilledPool
{
private ScanPool<X> _scanPool;
private StackPool<X> _stackPool;

[IterationSetup]
public void CreateAndPopulatePools()
{
_scanPool = new ScanPool<X>();
_scanPool.Return(new X(false, 0, null));
_scanPool.Return(new X(false, 0, null));
_scanPool.ScanReturn(new X(false, 0, null));
_scanPool.ScanReturn(new X(false, 0, null));

_stackPool = new StackPool<X>();
_stackPool.Return(new X(false, 0, null));
Expand All @@ -26,37 +109,43 @@ public void CreateAndPopulatePools()
[Benchmark(Baseline = true)]
public int New_2Times()
{
var x3 = new X(true, 1, "1");
var x4 = new X(true, 2, "2");
var x1 = new X(true, 1, "1");
var x2 = new X(true, 2, "2");

return x3.I + x4.I;
return x1.I + x2.I;
}

[Benchmark]
public int StackPool_2Rents()
{
var x3 = _stackPool.RentOrNew(true, 1, "1");
var x4 = _stackPool.RentOrNew(true, 2, "2");
var p = _stackPool;

return x3.I + x4.I;
var x1 = p.RentOrNew(true, 1, "1");
var x2 = p.RentOrNew(true, 2, "2");

return x1.I + x2.I;
}

[Benchmark]
public int ScanPool_2Rents()
{
var x3 = _scanPool.RentOrNew(true, 1, "1");
var x4 = _scanPool.RentOrNew(true, 2, "2");
var p = _scanPool;

return x3.I + x4.I;
var x1 = p.RentOrNew(true, 1, "1");
var x2 = p.RentOrNew(true, 2, "2");

return x1.I + x2.I;
}

[Benchmark]
public int ScanPool_ScanOnly_2Rents()
{
var x3 = _scanPool.ScanRentOrNew(true, 1, "1");
var x4 = _scanPool.ScanRentOrNew(true, 2, "2");
var p = _scanPool;

return x3.I + x4.I;
var x1 = p.ScanRentOrNew(true, 1, "1");
var x2 = p.ScanRentOrNew(true, 2, "2");

return x1.I + x2.I;
}
}

Expand Down Expand Up @@ -215,11 +304,11 @@ public T ScanRent()
public void Return(T x)
{
if (Interlocked.CompareExchange(ref _x, x, null) != null)
ScanSet(x);
ScanReturn(x);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ScanSet(T x)
public void ScanReturn(T x)
{
var xs = _xs;
for (var i = 0; i < xs.Length &&
Expand Down
3 changes: 2 additions & 1 deletion src/Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Program
{
static void Main()
{
BenchmarkRunner.Run<ObjectPoolComparison_AccessPrefilledPools>();
BenchmarkRunner.Run<ObjectPoolComparison_RentReturnAndRentPrefilledPool>();
//BenchmarkRunner.Run<ObjectPoolComparison_RentPrefilledPool>();

//BenchmarkRunner.Run<ObjectPoolComparison>();
//BenchmarkRunner.Run<DelegateVsInterfaceStruct.MapArray>();
Expand Down

0 comments on commit 56b7f53

Please sign in to comment.