Skip to content

Commit 74ead29

Browse files
committed
Additional invoke benchmarks
1 parent acac700 commit 74ead29

File tree

1 file changed

+112
-0
lines changed
  • src/benchmarks/micro/runtime/System.Reflection

1 file changed

+112
-0
lines changed

src/benchmarks/micro/runtime/System.Reflection/Invoke.cs

+112
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public class Invoke
2424
private static MethodInfo s_method_nullableInt;
2525
private static ConstructorInfo s_ctor_int_string_struct_class;
2626
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+
2736
private static PropertyInfo s_property_int;
2837
private static PropertyInfo s_property_class;
2938
private static FieldInfo s_field_int;
@@ -86,6 +95,14 @@ public void Setup()
8695

8796
s_staticField_struct = typeof(MyClass).
8897
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
89106
}
90107

91108
public static void Method_int_string_struct_class(int i, string s, MyBlittableStruct myStruct, MyClass myClass)
@@ -122,6 +139,18 @@ public void Method0_NoParms()
122139
}
123140
}
124141

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+
125154
[Benchmark(OperationsPerInvoke = Iterations)]
126155
// Include the array allocation and population for a typical scenario.
127156
public void StaticMethod4_arrayNotCached_int_string_struct_class()
@@ -154,6 +183,34 @@ public void StaticMethod4_int_string_struct_class()
154183
}
155184
}
156185

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+
157214
[Benchmark(OperationsPerInvoke = Iterations)]
158215
public void StaticMethod4_ByRefParams_int_string_struct_class()
159216
{
@@ -163,6 +220,21 @@ public void StaticMethod4_ByRefParams_int_string_struct_class()
163220
}
164221
}
165222

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+
166238
[Benchmark(OperationsPerInvoke = Iterations)]
167239
// Starting with 5 parameters, stack allocations are replaced with heap allocations.
168240
public void StaticMethod5_ByRefParams_int_string_struct_class_bool()
@@ -182,6 +254,31 @@ public void Ctor0_NoParams()
182254
}
183255
}
184256

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+
185282
[Benchmark(OperationsPerInvoke = Iterations)]
186283
public void Ctor0_ActivatorCreateInstance_NoParams()
187284
{
@@ -200,6 +297,21 @@ public void Ctor4_int_string_struct_class()
200297
}
201298
}
202299

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+
203315
[Benchmark(OperationsPerInvoke = Iterations)]
204316
public void Ctor4_ActivatorCreateInstance()
205317
{

0 commit comments

Comments
 (0)