-
Notifications
You must be signed in to change notification settings - Fork 272
/
Invoke.cs
383 lines (327 loc) · 12.8 KB
/
Invoke.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// 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 BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Reflection
{
[BenchmarkCategory(Categories.Runtime, Categories.Reflection)]
public class Invoke
{
private const int Iterations = 1_000; // Reduce the randomness of these short-lived calls.
private static MyClass s_MyClass = new MyClass();
private static object[] s_args4 = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass };
private static object[] s_args5 = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass, true };
private static int s_dummy;
private static MethodInfo s_method;
private static MethodInfo s_method_int_string_struct_class;
private static MethodInfo s_method_int_string_struct_class_bool;
private static MethodInfo s_method_byref_int_string_struct_class;
private static MethodInfo s_method_byref_int_string_struct_class_bool;
private static MethodInfo s_method_nullableInt;
private static ConstructorInfo s_ctor_int_string_struct_class;
private static ConstructorInfo s_ctor_NoParams;
private static PropertyInfo s_property_int;
private static PropertyInfo s_property_class;
private static FieldInfo s_field_int;
private static FieldInfo s_field_class;
private static FieldInfo s_field_struct;
private static FieldInfo s_staticField_int;
private static FieldInfo s_staticField_class;
private static FieldInfo s_staticField_struct;
public static int s_int;
public static object s_class;
public static MyBlittableStruct s_struct;
[GlobalSetup]
public void Setup()
{
s_method = typeof(MyClass).
GetMethod(nameof(MyClass.MyMethod), BindingFlags.Public | BindingFlags.Instance)!;
s_method_int_string_struct_class = typeof(Invoke).
GetMethod(nameof(Method_int_string_struct_class), BindingFlags.Public | BindingFlags.Static)!;
s_method_int_string_struct_class_bool = typeof(Invoke).
GetMethod(nameof(Method_int_string_struct_class_bool), BindingFlags.Public | BindingFlags.Static)!;
s_method_byref_int_string_struct_class = typeof(Invoke).
GetMethod(nameof(Method_byref_int_string_struct_class), BindingFlags.Public | BindingFlags.Static)!;
s_method_byref_int_string_struct_class_bool = typeof(Invoke).
GetMethod(nameof(Method_byref_int_string_struct_class_bool), BindingFlags.Public | BindingFlags.Static)!;
s_method_nullableInt = typeof(Invoke).
GetMethod(nameof(Method_nullableInt), BindingFlags.Public | BindingFlags.Static)!;
s_ctor_int_string_struct_class = typeof(MyClass).
GetConstructor(new Type[] { typeof(int), typeof(string), typeof(MyBlittableStruct), typeof(MyClass) })!;
s_ctor_NoParams = typeof(MyClass).
GetConstructor(Array.Empty<Type>())!;
s_property_int = typeof(MyClass).
GetProperty(nameof(MyClass.I));
s_property_class = typeof(MyClass).
GetProperty(nameof(MyClass.O));
s_field_int = typeof(MyClass).
GetField(nameof(MyClass.i));
s_staticField_int = typeof(MyClass).
GetField(nameof(MyClass.s_i));
s_field_class = typeof(MyClass).
GetField(nameof(MyClass.o));
s_staticField_class = typeof(MyClass).
GetField(nameof(MyClass.s_o));
s_field_struct = typeof(MyClass).
GetField(nameof(MyClass.blittableStruct));
s_staticField_struct = typeof(MyClass).
GetField(nameof(MyClass.s_blittableStruct));
}
public static void Method_int_string_struct_class(int i, string s, MyBlittableStruct myStruct, MyClass myClass)
{
s_dummy++;
}
public static void Method_int_string_struct_class_bool(int i, string s, MyBlittableStruct myStruct, MyClass myClass, bool b)
{
s_dummy++;
}
public static void Method_byref_int_string_struct_class(ref int i, ref string s, ref MyBlittableStruct myStruct, ref MyClass myClass)
{
s_dummy++;
}
public static void Method_byref_int_string_struct_class_bool(ref int i, ref string s, ref MyBlittableStruct myStruct, ref MyClass myClass, ref bool b)
{
s_dummy++;
}
public static void Method_nullableInt(int? i)
{
s_dummy++;
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Method0_NoParms()
{
for (int i = 0; i < Iterations; i++)
{
s_method.Invoke(s_MyClass, null);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
// Include the array allocation and population for a typical scenario.
public void StaticMethod4_arrayNotCached_int_string_struct_class()
{
for (int i = 0; i < Iterations; i++)
{
object[] args = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass };
s_method_int_string_struct_class.Invoke(null, args);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
// Include the array allocation and population for a typical scenario.
// Starting with 5 parameters, stack allocations are replaced with heap allocations.
public void StaticMethod5_arrayNotCached_int_string_struct_class_bool()
{
for (int i = 0; i < Iterations; i++)
{
object[] args = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass, true };
s_method_int_string_struct_class_bool.Invoke(null, args);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void StaticMethod4_int_string_struct_class()
{
for (int i = 0; i < Iterations; i++)
{
s_method_int_string_struct_class.Invoke(null, s_args4);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void StaticMethod4_ByRefParams_int_string_struct_class()
{
for (int i = 0; i < Iterations; i++)
{
s_method_byref_int_string_struct_class.Invoke(null, s_args4);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
// Starting with 5 parameters, stack allocations are replaced with heap allocations.
public void StaticMethod5_ByRefParams_int_string_struct_class_bool()
{
for (int i = 0; i < Iterations; i++)
{
s_method_byref_int_string_struct_class_bool.Invoke(null, s_args5);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Ctor0_NoParams()
{
for (int i = 0; i < Iterations; i++)
{
s_ctor_NoParams.Invoke(null);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Ctor0_ActivatorCreateInstance_NoParams()
{
for (int i = 0; i < Iterations; i++)
{
Activator.CreateInstance(typeof(MyClass));
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Ctor4_int_string_struct_class()
{
for (int i = 0; i < Iterations; i++)
{
s_ctor_int_string_struct_class.Invoke(s_args4);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Ctor4_ActivatorCreateInstance()
{
for (int i = 0; i < Iterations; i++)
{
Activator.CreateInstance(typeof(MyClass), s_args4);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Property_Get_int()
{
for (int i = 0; i < Iterations; i++)
{
s_int = (int)s_property_int.GetValue(s_MyClass);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Property_Get_class()
{
for (int i = 0; i < Iterations; i++)
{
s_class = s_property_class.GetValue(s_MyClass);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Property_Set_int()
{
for (int i = 0; i < 1000; i++)
{
s_property_int.SetValue(s_MyClass, 42);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Property_Set_class()
{
for (int i = 0; i < Iterations; i++)
{
s_property_class.SetValue(s_MyClass, null);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_Get_int()
{
for (int i = 0; i < Iterations; i++)
{
s_int = (int)s_field_int.GetValue(s_MyClass);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_GetStatic_int()
{
for (int i = 0; i < Iterations; i++)
{
s_int = (int)s_staticField_int.GetValue(null);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_Get_class()
{
for (int i = 0; i < Iterations; i++)
{
s_class = s_field_class.GetValue(s_MyClass);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_GetStatic_class()
{
for (int i = 0; i < Iterations; i++)
{
s_class = s_staticField_class.GetValue(null);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_Get_struct()
{
for (int i = 0; i < Iterations; i++)
{
s_struct = (MyBlittableStruct)s_field_struct.GetValue(s_MyClass);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_GetStatic_struct()
{
for (int i = 0; i < Iterations; i++)
{
s_struct = (MyBlittableStruct)s_staticField_struct.GetValue(null);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_Set_int()
{
for (int i = 0; i < Iterations; i++)
{
s_field_int.SetValue(s_MyClass, 42);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_SetStatic_int()
{
for (int i = 0; i < Iterations; i++)
{
s_staticField_int.SetValue(null, 42);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_Set_class()
{
for (int i = 0; i < Iterations; i++)
{
s_field_class.SetValue(s_MyClass, 42);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_SetStatic_class()
{
for (int i = 0; i < Iterations; i++)
{
s_staticField_class.SetValue(null, 42);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_Set_struct()
{
for (int i = 0; i < Iterations; i++)
{
s_field_struct.SetValue(s_MyClass, default(MyBlittableStruct));
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public void Field_SetStatic_struct()
{
for (int i = 0; i < Iterations; i++)
{
s_staticField_struct.SetValue(null, default(MyBlittableStruct));
}
}
public struct MyBlittableStruct
{
public int i;
public bool b;
}
public class MyClass
{
public MyClass() { }
public MyClass(int i, string s, MyBlittableStruct myStruct, MyClass myClass) { }
public void MyMethod() { }
public int i = 0;
public static int s_i = 0;
public bool b = false;
public string s = null;
public object o = null;
public static object s_o = null;
public MyBlittableStruct blittableStruct = default;
public static MyBlittableStruct s_blittableStruct = default;
public int I { get; set; } = 0;
public object O { get; set; } = null;
}
}
}