Skip to content

Commit

Permalink
Added pointers to SampleBenchmark test, and fixed the test failure.
Browse files Browse the repository at this point in the history
Added comment about `Type.IsClass` returns true for pointers.
  • Loading branch information
timcassell committed May 17, 2023
1 parent 7abb119 commit fbdb74c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/BenchmarkDotNet/Engines/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void Consume<T>(in T value)
=> byteHolder = Unsafe.As<T, byte>(ref Unsafe.AsRef(in value));

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

internal static bool HasConsumableField(Type type, out FieldInfo consumableField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public static void EmitReturnDefault(this ILGenerator ilBuilder, Type resultType
{
case Type t when t == typeof(void):
break;
case Type t when t.IsPointer: // Type.IsClass returns true for pointers, so we have to check for pointer type first.
/*
IL_0000: ldc.i4.0
IL_0001: conv.u
*/
ilBuilder.Emit(OpCodes.Ldc_I4_0);
ilBuilder.Emit(OpCodes.Conv_U);
break;
case Type t when t.IsClass || t.IsInterface:
ilBuilder.Emit(OpCodes.Ldnull);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ public EmptyStruct ReturnManyArgsCase(ref double i, int j, string k, object l)
return ref refValueHolder;
}

[Benchmark]
public unsafe int* ReturnsIntPointer()
{
Thread.Sleep(100);
return default;
}

[Benchmark]
public unsafe void* ReturnsVoidPointer()
{
Thread.Sleep(100);
return default;
}

[Benchmark]
public unsafe EmptyStruct* ReturnsStructPointer()
{
Thread.Sleep(100);
return default;
}

[Benchmark, Arguments(12)]
public Task<int> TaskSample(long arg)
{
Expand Down

0 comments on commit fbdb74c

Please sign in to comment.