-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvokeBenchmarks.cs
169 lines (141 loc) · 5.65 KB
/
InvokeBenchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using BenchmarkDotNet.Attributes;
using OpenRiaServices.Client.Benchmarks.Client.Cities;
using System.ComponentModel;
using System.Reflection;
namespace ClientBenchmarks
{
[LegacyJitX86Job, RyuJitX64Job]
[MemoryDiagnoser]
public class InvokeBenchmarks
{
City _city = new City() { CountyName = "Dummy" };
[Params(10, 50, 100, 500)]
public int NumInvocations { get; set; } = 1;
[Benchmark()]
public object Reflection()
{
var propertyInfo = typeof(City).GetProperty(nameof(City.CountyName));
object result = null;
for (int i = 0; i < NumInvocations; ++i)
{
result = propertyInfo.GetValue(_city, null);
}
return result;
}
[Benchmark(Baseline = true)]
public object Reflection_PropertyDescriptor()
{
var propertyInfo = TypeDescriptor.GetProperties(typeof(City))[nameof(City.CountyName)];
object result = null;
for (int i = 0; i < NumInvocations; ++i)
{
result = propertyInfo.GetValue(_city);
}
return result;
}
//[Benchmark] This is so slow so no reason to continue testing
public object ExpressionCompile()
{
var propertyInfo = typeof(City).GetProperty(nameof(City.CountyName));
Func<object, object> func = CreatePropertyGetter(propertyInfo);
object result = null;
for (int i = 0; i < NumInvocations; ++i)
{
result = func(_city);
}
return result;
}
//[Benchmark]
public object DelegateInvoke()
{
var propertyInfo = TypeDescriptor.GetProperties(typeof(City))[nameof(City.CountyName)];
Func<object, object> func = CreateDelegate(propertyInfo);
object result = null;
for (int i = 0; i < NumInvocations; ++i)
{
result = func(_city);
}
return result;
}
[Benchmark]
public object MetaMember_Method()
{
var propertyInfo = typeof(City).GetProperty(nameof(City.CountyName));
var member = new MetaMember(null, propertyInfo);
object result = null;
for (int i = 0; i < NumInvocations; ++i)
{
result = member.GetValue(_city);
}
return result;
}
[Benchmark]
public object MetaMember_VirtualMethod()
{
var propertyInfo = typeof(City).GetProperty(nameof(City.CountyName));
var member = MetaMember.CreateMember(null, propertyInfo);
object result = null;
for (int i = 0; i < NumInvocations; ++i)
{
result = member.GetValueVirtual(_city);
}
return result;
}
public static Func<object, object> CreatePropertyGetter(System.Reflection.PropertyInfo propertyInfo)
{
var getMethod = propertyInfo.GetGetMethod();
if (getMethod == null)
{
return x => propertyInfo.GetValue(x, null);
}
var type = propertyInfo.DeclaringType;
var instance = Expression.Parameter(typeof(object));
var typedInstance = Expression.Convert(instance, type);
var res = Expression.Call(typedInstance, getMethod);
var result = Expression.Convert(res, typeof(object));
var lambda = Expression.Lambda<Func<object, object>>(result, instance);
return lambda.Compile();
}
/// <summary>
/// Helper method which creates a delegate which can be used to invoke a specific getter
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
public static Func<object, object> CreateDelegate(PropertyDescriptor property)
{
PropertyInfo propertyInfo = property.ComponentType.GetProperty(property.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
if (propertyInfo == null)
return (object x) => property.GetValue(x);
return CreateDelegate(propertyInfo);
}
public static Func<object, object> CreateDelegate(PropertyInfo propertyInfo)
{
var helperMethod = typeof(InvokeBenchmarks).GetMethod(nameof(InvokeBenchmarks.CreateDelegateHelper), BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
var helper = helperMethod.MakeGenericMethod(propertyInfo.DeclaringType, propertyInfo.PropertyType);
return (Func<object, object>)helper.Invoke(null, new[] { propertyInfo });
}
private static Func<object, object> CreateDelegateHelper<T, Tres>(PropertyInfo propertyInfo)
{
var getMethod = propertyInfo.GetGetMethod();
if (getMethod == null)
{
return CreateFallbackGetter(propertyInfo);
}
var func = (Func<T, Tres>)Delegate.CreateDelegate(typeof(Func<T, Tres>), getMethod);
// Add a wrapper which performs boxing of the function
Func<object, object> res = (object x) => (object)func((T)x);
return res;
}
private static Func<object, object> CreateFallbackGetter(PropertyInfo propertyInfo)
{
return x => propertyInfo.GetValue(x, null);
}
}
}