Skip to content

Commit 2d46b79

Browse files
committed
Add where T : allows ref struct anti-constraints.
1 parent 882e625 commit 2d46b79

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/BenchmarkDotNet/Engines/DeadCodeEliminationHelper.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ public static class DeadCodeEliminationHelper
1111
/// </summary>
1212
[MethodImpl(MethodImplOptions.NoInlining)]
1313
[UsedImplicitly] // Used in generated benchmarks
14-
public static void KeepAliveWithoutBoxing<T>(T value) { }
14+
public static void KeepAliveWithoutBoxing<T>(T value)
15+
#if NET9_0_OR_GREATER
16+
where T : allows ref struct
17+
#endif
18+
{ }
1519

1620
/// <summary>
1721
/// This method can't get inlined, so any value send to it
1822
/// will not get eliminated by the dead code elimination
1923
/// </summary>
2024
[MethodImpl(MethodImplOptions.NoInlining)]
2125
[UsedImplicitly] // Used in generated benchmarks
22-
public static void KeepAliveWithoutBoxing<T>(ref T value) { }
26+
public static void KeepAliveWithoutBoxing<T>(ref T value)
27+
#if NET9_0_OR_GREATER
28+
where T : allows ref struct
29+
#endif
30+
{ }
2331

2432
/// <summary>
2533
/// This method can't get inlined, so any value send to it
@@ -28,6 +36,10 @@ public static void KeepAliveWithoutBoxing<T>(ref T value) { }
2836
/// </summary>
2937
[MethodImpl(MethodImplOptions.NoInlining)]
3038
[UsedImplicitly] // Used in generated benchmarks
31-
public static void KeepAliveWithoutBoxingReadonly<T>(in T value) { }
39+
public static void KeepAliveWithoutBoxingReadonly<T>(in T value)
40+
#if NET9_0_OR_GREATER
41+
where T : allows ref struct
42+
#endif
43+
{ }
3244
}
3345
}

src/BenchmarkDotNet/Toolchains/InProcess/NoEmit/BenchmarkActionImpl.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,18 +544,21 @@ public override void Cleanup()
544544
}
545545

546546
[AggressivelyOptimizeMethods]
547-
public class BenchmarkActionConfiguredCancelableAsyncEnumerable<T> : BenchmarkActionBase
547+
public class BenchmarkActionAsyncEnumerable<T> : BenchmarkActionBase
548+
#if NET9_0_OR_GREATER
549+
where T : allows ref struct
550+
#endif
548551
{
549-
private readonly Func<ConfiguredCancelableAsyncEnumerable<T>> callback;
552+
private readonly Func<IAsyncEnumerable<T>> callback;
550553
private readonly int unrollFactor;
551554
private WorkloadValueTaskSource workloadValueTaskSource = null!;
552555
private IClock? clock;
553556
private long invokeCount;
554557

555558
[SetsRequiredMembers]
556-
public BenchmarkActionConfiguredCancelableAsyncEnumerable(object? instance, MethodInfo method, int unrollFactor)
559+
public BenchmarkActionAsyncEnumerable(object? instance, MethodInfo method, int unrollFactor)
557560
{
558-
callback = CreateWorkload<Func<ConfiguredCancelableAsyncEnumerable<T>>>(instance, method);
561+
callback = CreateWorkload<Func<IAsyncEnumerable<T>>>(instance, method);
559562
this.unrollFactor = unrollFactor;
560563
InvokeSingle = InvokeOnce;
561564
InvokeUnroll = WorkloadActionUnroll;
@@ -623,18 +626,21 @@ public override void Cleanup()
623626
}
624627

625628
[AggressivelyOptimizeMethods]
626-
public class BenchmarkActionAsyncEnumerable<T> : BenchmarkActionBase
629+
public class BenchmarkActionConfiguredCancelableAsyncEnumerable<T> : BenchmarkActionBase
630+
#if NET10_0_OR_GREATER
631+
where T : allows ref struct
632+
#endif
627633
{
628-
private readonly Func<IAsyncEnumerable<T>> callback;
634+
private readonly Func<ConfiguredCancelableAsyncEnumerable<T>> callback;
629635
private readonly int unrollFactor;
630636
private WorkloadValueTaskSource workloadValueTaskSource = null!;
631637
private IClock? clock;
632638
private long invokeCount;
633639

634640
[SetsRequiredMembers]
635-
public BenchmarkActionAsyncEnumerable(object? instance, MethodInfo method, int unrollFactor)
641+
public BenchmarkActionConfiguredCancelableAsyncEnumerable(object? instance, MethodInfo method, int unrollFactor)
636642
{
637-
callback = CreateWorkload<Func<IAsyncEnumerable<T>>>(instance, method);
643+
callback = CreateWorkload<Func<ConfiguredCancelableAsyncEnumerable<T>>>(instance, method);
638644
this.unrollFactor = unrollFactor;
639645
InvokeSingle = InvokeOnce;
640646
InvokeUnroll = WorkloadActionUnroll;

0 commit comments

Comments
 (0)