Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,95 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;

// Performance test for virtual call dispatch with three
// possible target classes mixed in varying proportions.
//
// Note: for now we type the test input as B[] and not I[]
// so the simple guessing heuristic in the jit has a class
// to guess for.

namespace GuardedDevirtualizationThreeClassInterface
{

interface I
{
int F();
}

public class B : I
{
int I.F() => 33;
}

public class D : B, I
{
int I.F() => 44;
}

public class E : B, I
{
int I.F() => 55;
}

public class TestInput
{
public const int N = 1000;

public TestInput(double pB, double pD)
{
_pB = pB;
_pD = pD;
b = GetArray();
}

static Random r = new Random(42);

double _pB;
double _pD;
B[] b;

public B[] Array => b;
public override string ToString() => $"pB={_pB:F2} pD={_pD:F2}";

B[] GetArray()
namespace GuardedDevirtualization
{
[BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)]
public class ThreeClassInterface
{
B[] result = new B[N];

for (int i = 0; i < N; i++)
interface I
{
double p = r.NextDouble();

if (p <= _pB)
{
result[i] = new B();
}
else if (p <= _pB + _pD)
{
result[i] = new D();
}
else
int F();
}

public class B : I
{
int I.F() => 33;
}

public class D : B, I
{
int I.F() => 44;
}

public class E : B, I
{
int I.F() => 55;
}

[Benchmark(OperationsPerInvoke=TestInput.N)]
[ArgumentsSource(nameof(GetInput))]
public long Call(TestInput testInput)
{
long sum = 0;

// Note: for now we type the test input as B[] and not I[]
// so the simple guessing heuristic in the jit has a class
// to guess for.
B[] input = testInput.Array;
for (int i = 0; i < input.Length; i++)
{
result[i] = new E();
sum += ((I)input[i]).F();
}
return sum;
}
return result;
}
}

[BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)]
public class Interface
{
[Benchmark(OperationsPerInvoke=TestInput.N)]
[ArgumentsSource(nameof(GetInput))]
public long Call3(TestInput testInput)
{
long sum = 0;
B[] input = testInput.Array;
for (int i = 0; i < input.Length; i++)

public static IEnumerable<TestInput> GetInput()
{
sum += ((I)input[i]).F();
const int S = 3;
const double delta = 1.0 / (double) S;

for (int i = 0; i <= S; i++)
{
for (int j = 0; j <= S - i; j++)
{
yield return new TestInput(i * delta, j * delta);
}
}
}
return sum;
}

static int S = 3;
static double delta = 1.0 / (double) S;

public static IEnumerable<TestInput> GetInput()
{
double pB = 0;

for (int i = 0; i <= S; i++, pB += delta)

public class TestInput
{
double pD = 0;
public const int N = 1000;

for (int j = 0; j <= S - i; j++, pD += delta)
public B[] Array;
private double _pB;
private double _pD;

public TestInput(double pB, double pD)
{
yield return new TestInput(pB, pD);
_pB = pB;
_pD = pD;
Array = ValuesGenerator.Array<double>(N).Select(p =>
{
if (p <= _pB)
return new B();
if (p <= _pB + _pD)
return new D();
return new E();
}).ToArray();
}

public override string ToString() => $"pB={_pB:F2} pD={_pD:F2}";
}
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,86 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;

// Performance test for virtual call dispatch with three
// possible target classes mixed in varying proportions.

namespace GuardedDevirtualizationThreeClass
{

public class B
{
public virtual int F() => 33;
}

public class D : B
{
public override int F() => 44;
}

public class E : B
{
public override int F() => 55;
}

public class TestInput
{
public const int N = 1000;

public TestInput(double pB, double pD)
{
_pB = pB;
_pD = pD;
b = GetArray();
}

static Random r = new Random(42);

double _pB;
double _pD;
B[] b;

public B[] Array => b;
public override string ToString() => $"pB={_pB:F2} pD={_pD:F2}";

B[] GetArray()
namespace GuardedDevirtualization
{
[BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)]
public class ThreeClassVirtual
{
B[] result = new B[N];

for (int i = 0; i < N; i++)
public class B
{
double p = r.NextDouble();

if (p <= _pB)
{
result[i] = new B();
}
else if (p <= _pB + _pD)
{
result[i] = new D();
}
else
public virtual int F() => 33;
}

public class D : B
{
public override int F() => 44;
}

public class E : B
{
public override int F() => 55;
}

[Benchmark(OperationsPerInvoke=TestInput.N)]
[ArgumentsSource(nameof(GetInput))]
public long Call(TestInput testInput)
{
long sum = 0;
B[] input = testInput.Array;
for (int i = 0; i < input.Length; i++)
{
result[i] = new E();
sum += input[i].F();
}
return sum;
}
return result;
}
}

[BenchmarkCategory(Categories.CoreCLR, Categories.Virtual)]
public class Virtual
{
[Benchmark(OperationsPerInvoke=TestInput.N)]
[ArgumentsSource(nameof(GetInput))]
public long Call3(TestInput testInput)
{
long sum = 0;
B[] input = testInput.Array;
for (int i = 0; i < input.Length; i++)

public static IEnumerable<TestInput> GetInput()
{
sum += input[i].F();
const int S = 3;
const double delta = 1.0 / (double) S;

for (int i = 0; i <= S; i++)
{
for (int j = 0; j <= S - i; j++)
{
yield return new TestInput(i * delta, j * delta);
}
}
}
return sum;
}

static int S = 3;
static double delta = 1.0 / (double) S;

public static IEnumerable<TestInput> GetInput()
{
double pB = 0;

for (int i = 0; i <= S; i++, pB += delta)

public class TestInput
{
double pD = 0;
public const int N = 1000;

for (int j = 0; j <= S - i; j++, pD += delta)
public B[] Array;
private double _pB;
private double _pD;

public TestInput(double pB, double pD)
{
yield return new TestInput(pB, pD);
_pB = pB;
_pD = pD;
Array = ValuesGenerator.Array<double>(N).Select(p =>
{
if (p <= _pB)
return new B();
if (p <= _pB + _pD)
return new D();
return new E();
}).ToArray();
}
}

public override string ToString() => $"pB={_pB:F2} pD={_pD:F2}";
}
}
}

}


Loading