@@ -24,6 +24,15 @@ public class Invoke
24
24
private static MethodInfo s_method_nullableInt ;
25
25
private static ConstructorInfo s_ctor_int_string_struct_class ;
26
26
private static ConstructorInfo s_ctor_NoParams ;
27
+
28
+ #if NET8_0_OR_GREATER
29
+ private static MethodInvoker s_method_invoker ;
30
+ private static MethodInvoker s_method_int_string_struct_class_invoker ;
31
+ private static MethodInvoker s_method_byref_int_string_struct_class_invoker ;
32
+ private static ConstructorInvoker s_ctor_int_string_struct_class_invoker ;
33
+ private static ConstructorInvoker s_ctor_NoParams_invoker ;
34
+ #endif
35
+
27
36
private static PropertyInfo s_property_int ;
28
37
private static PropertyInfo s_property_class ;
29
38
private static FieldInfo s_field_int ;
@@ -86,6 +95,14 @@ public void Setup()
86
95
87
96
s_staticField_struct = typeof ( MyClass ) .
88
97
GetField ( nameof ( MyClass . s_blittableStruct ) ) ;
98
+
99
+ #if NET8_0_OR_GREATER
100
+ s_method_invoker = MethodInvoker . Create ( s_method ) ;
101
+ s_method_int_string_struct_class_invoker = MethodInvoker . Create ( s_method_int_string_struct_class ) ;
102
+ s_method_byref_int_string_struct_class_invoker = MethodInvoker . Create ( s_method_byref_int_string_struct_class ) ;
103
+ s_ctor_int_string_struct_class_invoker = ConstructorInvoker . Create ( s_ctor_int_string_struct_class ) ;
104
+ s_ctor_NoParams_invoker = ConstructorInvoker . Create ( typeof ( MyClass ) . GetConstructor ( Array . Empty < Type > ( ) ) ) ;
105
+ #endif
89
106
}
90
107
91
108
public static void Method_int_string_struct_class ( int i , string s , MyBlittableStruct myStruct , MyClass myClass )
@@ -122,6 +139,18 @@ public void Method0_NoParms()
122
139
}
123
140
}
124
141
142
+
143
+ #if NET8_0_OR_GREATER
144
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
145
+ public void Method0_NoParms_MethodInvoker ( )
146
+ {
147
+ for ( int i = 0 ; i < Iterations ; i ++ )
148
+ {
149
+ s_method_invoker . Invoke ( s_MyClass ) ;
150
+ }
151
+ }
152
+ #endif
153
+
125
154
[ Benchmark ( OperationsPerInvoke = Iterations ) ]
126
155
// Include the array allocation and population for a typical scenario.
127
156
public void StaticMethod4_arrayNotCached_int_string_struct_class ( )
@@ -154,6 +183,34 @@ public void StaticMethod4_int_string_struct_class()
154
183
}
155
184
}
156
185
186
+ #if NET8_0_OR_GREATER
187
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
188
+ public void StaticMethod4_int_string_struct_class_MethodInvoker ( )
189
+ {
190
+ // To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
191
+ object boxedInt = 42 ;
192
+ object boxedStruct = default ( MyBlittableStruct ) ;
193
+
194
+ for ( int i = 0 ; i < Iterations ; i ++ )
195
+ {
196
+ s_method_int_string_struct_class_invoker . Invoke ( null , boxedInt , "Hello" , boxedStruct , s_MyClass ) ;
197
+ }
198
+ }
199
+
200
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
201
+ public void StaticMethod4_int_string_struct_class_MethodInvokerWithSpan ( )
202
+ {
203
+ // To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
204
+ object boxedInt = 42 ;
205
+ object boxedStruct = default ( MyBlittableStruct ) ;
206
+
207
+ for ( int i = 0 ; i < Iterations ; i ++ )
208
+ {
209
+ s_method_int_string_struct_class_invoker . Invoke ( null , new Span < object > ( s_args4 ) ) ;
210
+ }
211
+ }
212
+ #endif
213
+
157
214
[ Benchmark ( OperationsPerInvoke = Iterations ) ]
158
215
public void StaticMethod4_ByRefParams_int_string_struct_class ( )
159
216
{
@@ -163,6 +220,21 @@ public void StaticMethod4_ByRefParams_int_string_struct_class()
163
220
}
164
221
}
165
222
223
+ #if NET8_0_OR_GREATER
224
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
225
+ public void StaticMethod4_ByRefParams_int_string_struct_class_MethodInvoker ( )
226
+ {
227
+ // To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
228
+ object boxedInt = 42 ;
229
+ object boxedStruct = default ( MyBlittableStruct ) ;
230
+
231
+ for ( int i = 0 ; i < Iterations ; i ++ )
232
+ {
233
+ s_method_byref_int_string_struct_class_invoker . Invoke ( null , boxedInt , "Hello" , boxedStruct , s_MyClass ) ;
234
+ }
235
+ }
236
+ #endif
237
+
166
238
[ Benchmark ( OperationsPerInvoke = Iterations ) ]
167
239
// Starting with 5 parameters, stack allocations are replaced with heap allocations.
168
240
public void StaticMethod5_ByRefParams_int_string_struct_class_bool ( )
@@ -182,6 +254,31 @@ public void Ctor0_NoParams()
182
254
}
183
255
}
184
256
257
+ #if NET8_0_OR_GREATER
258
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
259
+ public void Ctor0_NoParams_ConstructorInvoker ( )
260
+ {
261
+ for ( int i = 0 ; i < Iterations ; i ++ )
262
+ {
263
+ s_ctor_NoParams_invoker . Invoke ( ) ;
264
+ }
265
+ }
266
+ #endif
267
+
268
+ /// <summary>
269
+ /// Reinvoke the constructor on the same object. Used by some serializers.
270
+ /// </summary>
271
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
272
+ public void Ctor0_NoParams_Reinvoke ( )
273
+ {
274
+ MyClass obj = new MyClass ( ) ;
275
+
276
+ for ( int i = 0 ; i < Iterations ; i ++ )
277
+ {
278
+ s_ctor_NoParams . Invoke ( obj , null ) ;
279
+ }
280
+ }
281
+
185
282
[ Benchmark ( OperationsPerInvoke = Iterations ) ]
186
283
public void Ctor0_ActivatorCreateInstance_NoParams ( )
187
284
{
@@ -200,6 +297,21 @@ public void Ctor4_int_string_struct_class()
200
297
}
201
298
}
202
299
300
+ #if NET8_0_OR_GREATER
301
+ [ Benchmark ( OperationsPerInvoke = Iterations ) ]
302
+ public void Ctor4_int_string_struct_class_ConstructorInvoker ( )
303
+ {
304
+ // To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
305
+ object boxedInt = 42 ;
306
+ object boxedStruct = default ( MyBlittableStruct ) ;
307
+
308
+ for ( int i = 0 ; i < Iterations ; i ++ )
309
+ {
310
+ s_ctor_int_string_struct_class_invoker . Invoke ( boxedInt , "Hello" , boxedStruct , s_MyClass ) ;
311
+ }
312
+ }
313
+ #endif
314
+
203
315
[ Benchmark ( OperationsPerInvoke = Iterations ) ]
204
316
public void Ctor4_ActivatorCreateInstance ( )
205
317
{
0 commit comments