Skip to content

Commit

Permalink
Add another benchmark + use explicit pre-alloc'd value types
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter committed Feb 4, 2025
1 parent b040b8a commit cee51a0
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/benchmarks/micro/runtime/System.Reflection/Invoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Invoke
private static MethodInvoker s_method_invoker;
private static MethodInvoker s_method_int_string_struct_class_invoker;
private static MethodInvoker s_method_byref_int_string_struct_class_invoker;
private static MethodInvoker s_method_byref_int_string_struct_class_bool_invoker;
private static ConstructorInvoker s_ctor_int_string_struct_class_invoker;
private static ConstructorInvoker s_ctor_NoParams_invoker;
#endif
Expand Down Expand Up @@ -100,6 +101,7 @@ public void Setup()
s_method_invoker = MethodInvoker.Create(s_method);
s_method_int_string_struct_class_invoker = MethodInvoker.Create(s_method_int_string_struct_class);
s_method_byref_int_string_struct_class_invoker = MethodInvoker.Create(s_method_byref_int_string_struct_class);
s_method_byref_int_string_struct_class_bool_invoker = MethodInvoker.Create(s_method_byref_int_string_struct_class_bool);
s_ctor_int_string_struct_class_invoker = ConstructorInvoker.Create(s_ctor_int_string_struct_class);
s_ctor_NoParams_invoker = ConstructorInvoker.Create(typeof(MyClass).GetConstructor(Array.Empty<Type>()));
#endif
Expand Down Expand Up @@ -187,23 +189,21 @@ public void StaticMethod4_int_string_struct_class()
[Benchmark(OperationsPerInvoke = Iterations)]
public void StaticMethod4_int_string_struct_class_MethodInvoker()
{
// To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
object boxedInt = 42;
object boxedStruct = default(MyBlittableStruct);
// To make the test more comparable to the MethodBase tests, set up the references and pre-boxed types.
object boxedInt = s_args4[0];
object stringRef = s_args4[1];
object boxedStruct = s_args4[2];
object myClassRef = s_args4[3];

for (int i = 0; i < Iterations; i++)
{
s_method_int_string_struct_class_invoker.Invoke(null, boxedInt, "Hello", boxedStruct, s_MyClass);
s_method_int_string_struct_class_invoker.Invoke(null, boxedInt, stringRef, boxedStruct, myClassRef);
}
}

[Benchmark(OperationsPerInvoke = Iterations)]
public void StaticMethod4_int_string_struct_class_MethodInvokerWithSpan()
{
// To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
object boxedInt = 42;
object boxedStruct = default(MyBlittableStruct);

for (int i = 0; i < Iterations; i++)
{
s_method_int_string_struct_class_invoker.Invoke(null, new Span<object>(s_args4));
Expand All @@ -224,13 +224,15 @@ public void StaticMethod4_ByRefParams_int_string_struct_class()
[Benchmark(OperationsPerInvoke = Iterations)]
public void StaticMethod4_ByRefParams_int_string_struct_class_MethodInvoker()
{
// To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
object boxedInt = 42;
object boxedStruct = default(MyBlittableStruct);
// To make the test more comparable to the MethodBase tests, set up the references and pre-boxed types.
object boxedInt = s_args4[0];
object stringRef = s_args4[1];
object boxedStruct = s_args4[2];
object myClassRef = s_args4[3];

for (int i = 0; i < Iterations; i++)
{
s_method_byref_int_string_struct_class_invoker.Invoke(null, boxedInt, "Hello", boxedStruct, s_MyClass);
s_method_byref_int_string_struct_class_invoker.Invoke(null, boxedInt, stringRef, boxedStruct, myClassRef);
}
}
#endif
Expand All @@ -245,6 +247,17 @@ public void StaticMethod5_ByRefParams_int_string_struct_class_bool()
}
}

#if NET8_0_OR_GREATER
[Benchmark(OperationsPerInvoke = Iterations)]
public void StaticMethod5_ByRefParams_int_string_struct_class_bool_MethodInvoker()
{
for (int i = 0; i < Iterations; i++)
{
s_method_byref_int_string_struct_class_bool_invoker.Invoke(null, new Span<object>(s_args5));
}
}
#endif

[Benchmark(OperationsPerInvoke = Iterations)]
public void Ctor0_NoParams()
{
Expand Down Expand Up @@ -301,13 +314,15 @@ public void Ctor4_int_string_struct_class()
[Benchmark(OperationsPerInvoke = Iterations)]
public void Ctor4_int_string_struct_class_ConstructorInvoker()
{
// To make the test more comparable to the MethodBase tests, we need to pre-box the value types.
object boxedInt = 42;
object boxedStruct = default(MyBlittableStruct);
// To make the test more comparable to the MethodBase tests, set up the references and pre-boxed types.
object boxedInt = s_args4[0];
object stringRef = s_args4[1];
object boxedStruct = s_args4[2];
object myClassRef = s_args4[3];

for (int i = 0; i < Iterations; i++)
{
s_ctor_int_string_struct_class_invoker.Invoke(boxedInt, "Hello", boxedStruct, s_MyClass);
s_ctor_int_string_struct_class_invoker.Invoke(boxedInt, stringRef, boxedStruct, myClassRef);
}
}
#endif
Expand Down

0 comments on commit cee51a0

Please sign in to comment.