Skip to content

Commit fbdb74c

Browse files
committed
Added pointers to SampleBenchmark test, and fixed the test failure.
Added comment about `Type.IsClass` returns true for pointers.
1 parent 7abb119 commit fbdb74c

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/BenchmarkDotNet/Engines/Consumer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public void Consume<T>(in T value)
131131
=> byteHolder = Unsafe.As<T, byte>(ref Unsafe.AsRef(in value));
132132

133133
internal static bool IsConsumable(Type type)
134+
// IsClass returns true for pointers.
134135
=> SupportedTypes.Contains(type) || type.GetTypeInfo().IsClass || type.GetTypeInfo().IsInterface || !type.IsByRefLike();
135136

136137
internal static bool HasConsumableField(Type type, out FieldInfo consumableField)

src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public static void EmitReturnDefault(this ILGenerator ilBuilder, Type resultType
3939
{
4040
case Type t when t == typeof(void):
4141
break;
42+
case Type t when t.IsPointer: // Type.IsClass returns true for pointers, so we have to check for pointer type first.
43+
/*
44+
IL_0000: ldc.i4.0
45+
IL_0001: conv.u
46+
*/
47+
ilBuilder.Emit(OpCodes.Ldc_I4_0);
48+
ilBuilder.Emit(OpCodes.Conv_U);
49+
break;
4250
case Type t when t.IsClass || t.IsInterface:
4351
ilBuilder.Emit(OpCodes.Ldnull);
4452
break;

tests/BenchmarkDotNet.IntegrationTests/InProcess.EmitTests/SampleBenchmark.cs

+21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ public ref int RefReturnManyArgsCase(ref double i, int j, string k, object l)
4343
return ref refValueHolder;
4444
}
4545

46+
[Benchmark]
47+
public unsafe int* ReturnsIntPointer()
48+
{
49+
Thread.Sleep(100);
50+
return default;
51+
}
52+
53+
[Benchmark]
54+
public unsafe void* ReturnsVoidPointer()
55+
{
56+
Thread.Sleep(100);
57+
return default;
58+
}
59+
60+
[Benchmark]
61+
public unsafe EmptyStruct* ReturnsStructPointer()
62+
{
63+
Thread.Sleep(100);
64+
return default;
65+
}
66+
4667
[Benchmark, Arguments(12)]
4768
public Task<int> TaskSample(long arg)
4869
{

0 commit comments

Comments
 (0)