From fbdb74cfbf3053206f69b8c6277ace9fa7c9eab9 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 17 May 2023 17:51:34 -0400 Subject: [PATCH] Added pointers to SampleBenchmark test, and fixed the test failure. Added comment about `Type.IsClass` returns true for pointers. --- src/BenchmarkDotNet/Engines/Consumer.cs | 1 + .../IlGeneratorDefaultValueExtensions.cs | 8 +++++++ .../InProcess.EmitTests/SampleBenchmark.cs | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/BenchmarkDotNet/Engines/Consumer.cs b/src/BenchmarkDotNet/Engines/Consumer.cs index a98f7928aa..c8c130fc17 100644 --- a/src/BenchmarkDotNet/Engines/Consumer.cs +++ b/src/BenchmarkDotNet/Engines/Consumer.cs @@ -131,6 +131,7 @@ public void Consume(in T value) => byteHolder = Unsafe.As(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) diff --git a/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs b/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs index 4b23db0141..1be780632d 100644 --- a/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs +++ b/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorDefaultValueExtensions.cs @@ -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; diff --git a/tests/BenchmarkDotNet.IntegrationTests/InProcess.EmitTests/SampleBenchmark.cs b/tests/BenchmarkDotNet.IntegrationTests/InProcess.EmitTests/SampleBenchmark.cs index 5ad35b0779..186f4b6b2d 100644 --- a/tests/BenchmarkDotNet.IntegrationTests/InProcess.EmitTests/SampleBenchmark.cs +++ b/tests/BenchmarkDotNet.IntegrationTests/InProcess.EmitTests/SampleBenchmark.cs @@ -43,6 +43,27 @@ public ref int RefReturnManyArgsCase(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 TaskSample(long arg) {