summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndy Ayers <andya@microsoft.com>2017-09-27 07:15:04 -0700
committerGitHub <noreply@github.com>2017-09-27 07:15:04 -0700
commitccc5e17738a4b2f652059f31e8146513163dbee7 (patch)
treeeb7fe9d6b30b01d02a6b8bbe454333607899d507 /tests
parent9509fc86a50ad107ee4d601e917cf072f135aa5e (diff)
downloadcoreclr-ccc5e17738a4b2f652059f31e8146513163dbee7.tar.gz
coreclr-ccc5e17738a4b2f652059f31e8146513163dbee7.tar.bz2
coreclr-ccc5e17738a4b2f652059f31e8146513163dbee7.zip
JIT: devirtualization support for EqualityComparer<T>.Default (#14125)
Mark `EqualityComparer<T>.Default`'s getter as `[Intrinsic]` so the jit knows there is something special about it. Extend the jit's named intrinsic recognizer to recognize this method. Add a new jit interface method to determine the exact type returned by `EqualityComparer<T>.Default`, given `T`. Compute the return type by mirroring the logic used in the actual implementation. Bail out when `T` is not final as those cases won't simplify down much and lead to code bloat. Invoke this interface method when trying to devirtualize calls where the 'this' object in the call comes from `EqualityComparer<T>.Default`. The devirtualized methods can then be inlined. Since the specific comparer `Equal` and `GetHashCode` methods look more complicated in IL than they really are, mark them with AggressiveInlining attributes. If devirtualization and inlining happen, it is quite likely that the value of the comparer object itself is not used in the body of the comparer. This value comes from a static field cache on the comparer helper. When the comparer value is ignored, the jit removes the field access since it is non-faulting. It also removes the the class init helper that is there to ensure that the (no-longer accessed) field is properly initialized. This helper has relatively high overhead even in the fast case where the class has been initialized aready. Add a perf test. Closes #6688.
Diffstat (limited to 'tests')
-rw-r--r--tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.cs207
-rw-r--r--tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.csproj40
2 files changed, 247 insertions, 0 deletions
diff --git a/tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.cs b/tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.cs
new file mode 100644
index 0000000000..b9da9f17fa
--- /dev/null
+++ b/tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.cs
@@ -0,0 +1,207 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Xunit.Performance;
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using Xunit;
+
+[assembly: OptimizeForBenchmarks]
+
+// Performance tests for optimizations related to EqualityComparer<T>.Default
+
+namespace Devirtualization
+{
+ public class EqualityComparerFixture<T> where T : IEquatable<T>
+ {
+ IEqualityComparer<T> comparer;
+
+ public EqualityComparerFixture(IEqualityComparer<T> customComparer = null)
+ {
+ comparer = customComparer ?? EqualityComparer<T>.Default;
+ }
+
+ // Baseline method showing unoptimized performance
+ [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
+ public bool CompareNoOpt(ref T a, ref T b)
+ {
+ return EqualityComparer<T>.Default.Equals(a, b);
+ }
+
+ // The code this method invokes should be well-optimized
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public bool Compare(ref T a, ref T b)
+ {
+ return EqualityComparer<T>.Default.Equals(a, b);
+ }
+
+ // This models how Dictionary uses a comparer. We're not
+ // yet able to optimize such cases.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public bool CompareCached(ref T a, ref T b)
+ {
+ return comparer.Equals(a, b);
+ }
+
+ private static IEqualityComparer<T> Wrapped()
+ {
+ return EqualityComparer<T>.Default;
+ }
+
+ // We would need enhancements to late devirtualization
+ // to optimize this case.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public bool CompareWrapped(ref T x, ref T y)
+ {
+ return Wrapped().Equals(x, y);
+ }
+
+ public bool BenchCompareNoOpt(ref T t, long count)
+ {
+ bool result = true;
+ for (int i = 0; i < count; i++)
+ {
+ result &= CompareNoOpt(ref t, ref t);
+ }
+ return result;
+ }
+
+ public bool BenchCompare(ref T t, long count)
+ {
+ bool result = true;
+ for (int i = 0; i < count; i++)
+ {
+ result &= Compare(ref t, ref t);
+ }
+ return result;
+ }
+
+ public bool BenchCompareCached(ref T t, long count)
+ {
+ bool result = true;
+ for (int i = 0; i < count; i++)
+ {
+ result &= CompareCached(ref t, ref t);
+ }
+ return result;
+ }
+
+ public bool BenchCompareWrapped(ref T t, long count)
+ {
+ bool result = true;
+ for (int i = 0; i < count; i++)
+ {
+ result &= CompareWrapped(ref t, ref t);
+ }
+ return result;
+ }
+ }
+
+ public class EqualityComparer
+ {
+
+#if DEBUG
+ public const int Iterations = 1;
+#else
+ public const int Iterations = 150 * 1000 * 1000;
+#endif
+
+ public enum E
+ {
+ RED = 1,
+ BLUE = 2
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ static void Consume(bool b) { }
+
+ [Benchmark(InnerIterationCount = Iterations)]
+ public static void ValueTupleCompareNoOpt()
+ {
+ var valueTupleFixture = new EqualityComparerFixture<ValueTuple<byte, E, int>>();
+ var v0 = new ValueTuple<byte, E, int>(3, E.RED, 11);
+ var result = true;
+
+ foreach (var iteration in Benchmark.Iterations)
+ {
+ using (iteration.StartMeasurement())
+ {
+ result &= valueTupleFixture.BenchCompareNoOpt(ref v0, Benchmark.InnerIterationCount);
+ }
+ }
+
+ Consume(result);
+ }
+
+ [Benchmark(InnerIterationCount = Iterations)]
+ public static void ValueTupleCompare()
+ {
+ var valueTupleFixture = new EqualityComparerFixture<ValueTuple<byte, E, int>>();
+ var v0 = new ValueTuple<byte, E, int>(3, E.RED, 11);
+ var result = true;
+
+ foreach (var iteration in Benchmark.Iterations)
+ {
+ using (iteration.StartMeasurement())
+ {
+ result &= valueTupleFixture.BenchCompare(ref v0, Benchmark.InnerIterationCount);
+ }
+ }
+
+ Consume(result);
+ }
+
+ [Benchmark(InnerIterationCount = Iterations)]
+ public static void ValueTupleCompareCached()
+ {
+ var valueTupleFixture = new EqualityComparerFixture<ValueTuple<byte, E, int>>();
+ var v0 = new ValueTuple<byte, E, int>(3, E.RED, 11);
+ var result = true;
+
+ foreach (var iteration in Benchmark.Iterations)
+ {
+ using (iteration.StartMeasurement())
+ {
+ result &= valueTupleFixture.BenchCompareCached(ref v0, Benchmark.InnerIterationCount);
+ }
+ }
+
+ Consume(result);
+ }
+
+ [Benchmark(InnerIterationCount = Iterations)]
+ public static void ValueTupleCompareWrapped()
+ {
+ var valueTupleFixture = new EqualityComparerFixture<ValueTuple<byte, E, int>>();
+ var v0 = new ValueTuple<byte, E, int>(3, E.RED, 11);
+ var result = true;
+
+ foreach (var iteration in Benchmark.Iterations)
+ {
+ using (iteration.StartMeasurement())
+ {
+ result &= valueTupleFixture.BenchCompareWrapped(ref v0, Benchmark.InnerIterationCount);
+ }
+ }
+
+ Consume(result);
+ }
+
+ public static int Main()
+ {
+ var valueTupleFixture = new EqualityComparerFixture<ValueTuple<byte, E, int>>();
+ var v0 = new ValueTuple<byte, E, int>(3, E.RED, 11);
+
+ bool vtCompare = valueTupleFixture.Compare(ref v0, ref v0);
+ bool vtCompareNoOpt = valueTupleFixture.CompareNoOpt(ref v0, ref v0);
+ bool vtCompareCached = valueTupleFixture.CompareCached(ref v0, ref v0);
+ bool vtCompareWrapped = valueTupleFixture.CompareWrapped(ref v0, ref v0);
+
+ bool vtOk = vtCompare & vtCompareNoOpt & vtCompareCached & vtCompareWrapped;
+
+ return vtOk ? 100 : 0;
+ }
+ }
+}
diff --git a/tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.csproj b/tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.csproj
new file mode 100644
index 0000000000..7f06379d37
--- /dev/null
+++ b/tests/src/JIT/Performance/CodeQuality/Devirtualization/DefaultEqualityComparerPerf.csproj
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{C1BFD48A-A83F-4767-8EB2-3E2C50906681}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+ <NuGetTargetMoniker>.NETStandard,Version=v1.4</NuGetTargetMoniker>
+ <NuGetTargetMonikerShort>netstandard1.4</NuGetTargetMonikerShort>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ </PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="DefaultEqualityComparerPerf.cs" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+ <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
+ <PropertyGroup>
+ <ProjectAssetsFile>$(JitPackagesConfigFileDirectory)benchmark\obj\project.assets.json</ProjectAssetsFile>
+ </PropertyGroup>
+</Project>