Last active
February 10, 2018 08:56
-
-
Save iwillspeak/2f97766aef7a1f44f5c4feb89f65ffd8 to your computer and use it in GitHub Desktop.
Demonstration of caching of derived type information using the curiously recurring template pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root = true | |
[*.cs] | |
indent_style = space | |
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bin/ | |
obj/ | |
BenchmarkDotNet.Artifacts/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.Linq; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
namespace crtp | |
{ | |
using foo; | |
using bar; | |
using baz; | |
namespace foo | |
{ | |
public class FooBase | |
{ | |
public FooBase() | |
{ | |
_props = CrtpBench.GetProperties(GetType()); | |
} | |
private IDictionary<string, PropertyInfo> _props; | |
} | |
public class Foo : FooBase | |
{ | |
public string F { get; } | |
public int B { get; } | |
} | |
} | |
namespace bar | |
{ | |
public class BarBase<T> | |
{ | |
private static IDictionary<string, PropertyInfo> Props = CrtpBench.GetProperties(typeof(T)); | |
} | |
public class Bar : BarBase<Bar> | |
{ | |
public string F { get; } | |
public int B { get; } | |
} | |
} | |
namespace baz | |
{ | |
public class BazBase | |
{ | |
public BazBase() | |
{ | |
_props = TypeProps.GetOrAdd(GetType(), CrtpBench.GetProperties); | |
} | |
private IDictionary<string, PropertyInfo> _props; | |
private static ConcurrentDictionary<Type, IDictionary<string, PropertyInfo>> TypeProps = | |
new ConcurrentDictionary<Type, IDictionary<string, PropertyInfo>>(); | |
} | |
public class Baz : BazBase | |
{ | |
public string F { get; } | |
public int B { get; } | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<CrtpBench>(); | |
} | |
} | |
[MemoryDiagnoser] | |
public class CrtpBench | |
{ | |
public static IDictionary<string, PropertyInfo> GetProperties(Type t) => | |
t.GetMembers() | |
.Where(mi => mi.MemberType==MemberTypes.Property) | |
.Cast<PropertyInfo>() | |
.ToDictionary(prop => prop.Name); | |
[Benchmark] | |
public Foo Standard() => new Foo(); | |
[Benchmark] | |
public Baz StaticCache() => new Baz(); | |
[Benchmark] | |
public Bar Crtp() => new Bar(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="BenchmarkDotNet" Version="0.10.12" /> | |
</ItemGroup> | |
</Project> |
Author
iwillspeak
commented
Feb 9, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment