Skip to content

Commit 07fccb7

Browse files
committed
Merge branch 'valuetask-nongeneric-inprocess' into temp
2 parents 79ffa8d + b713a94 commit 07fccb7

File tree

5 files changed

+267
-25
lines changed

5 files changed

+267
-25
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ private static BenchmarkAction CreateCore(
3232
if (resultType == typeof(Task))
3333
return new BenchmarkActionTask(resultInstance, targetMethod, unrollFactor);
3434

35+
if (resultType == typeof(ValueTask))
36+
return new BenchmarkActionValueTask(resultInstance, targetMethod, unrollFactor);
37+
3538
if (resultType.GetTypeInfo().IsGenericType)
3639
{
3740
var genericType = resultType.GetGenericTypeDefinition();

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

+40
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,46 @@ private void InvokeMultipleHardcoded(long repeatCount)
150150
public override object LastRunResult => result;
151151
}
152152

153+
internal class BenchmarkActionValueTask : BenchmarkActionBase
154+
{
155+
private readonly Helpers.AwaitHelper awaitHelper = new Helpers.AwaitHelper();
156+
private readonly Func<ValueTask> startTaskCallback;
157+
private readonly Action callback;
158+
private readonly Action unrolledCallback;
159+
160+
public BenchmarkActionValueTask(object instance, MethodInfo method, int unrollFactor)
161+
{
162+
bool isIdle = method == null;
163+
if (!isIdle)
164+
{
165+
startTaskCallback = CreateWorkload<Func<ValueTask>>(instance, method);
166+
callback = ExecuteBlocking;
167+
}
168+
else
169+
{
170+
callback = Overhead;
171+
}
172+
173+
InvokeSingle = callback;
174+
175+
unrolledCallback = Unroll(callback, unrollFactor);
176+
InvokeMultiple = InvokeMultipleHardcoded;
177+
178+
}
179+
180+
// must be kept in sync with VoidDeclarationsProvider.IdleImplementation
181+
private void Overhead() { }
182+
183+
// must be kept in sync with TaskDeclarationsProvider.TargetMethodDelegate
184+
private void ExecuteBlocking() => awaitHelper.GetResult(startTaskCallback.Invoke());
185+
186+
private void InvokeMultipleHardcoded(long repeatCount)
187+
{
188+
for (long i = 0; i < repeatCount; i++)
189+
unrolledCallback();
190+
}
191+
}
192+
153193
internal class BenchmarkActionValueTask<T> : BenchmarkActionBase
154194
{
155195
private readonly Helpers.AwaitHelper awaitHelper = new Helpers.AwaitHelper();

src/BenchmarkDotNet/Toolchains/InProcess/BenchmarkActionFactory.cs

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ private static BenchmarkAction CreateCore(
3434
if (resultType == typeof(Task))
3535
return new BenchmarkActionTask(resultInstance, targetMethod, codegenMode, unrollFactor);
3636

37+
if (resultType == typeof(ValueTask))
38+
return new BenchmarkActionValueTask(resultInstance, targetMethod, codegenMode, unrollFactor);
39+
3740
if (resultType.GetTypeInfo().IsGenericType)
3841
{
3942
var genericType = resultType.GetGenericTypeDefinition();

src/BenchmarkDotNet/Toolchains/InProcess/BenchmarkActionFactory_Implementations.cs

+46
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,52 @@ private void InvokeMultipleHardcoded(long repeatCount)
177177
public override object LastRunResult => result;
178178
}
179179

180+
internal class BenchmarkActionValueTask : BenchmarkActionBase
181+
{
182+
private readonly Helpers.AwaitHelper awaitHelper = new Helpers.AwaitHelper();
183+
private readonly Func<ValueTask> startTaskCallback;
184+
private readonly Action callback;
185+
private readonly Action unrolledCallback;
186+
187+
public BenchmarkActionValueTask(object instance, MethodInfo method, BenchmarkActionCodegen codegenMode, int unrollFactor)
188+
{
189+
bool isIdle = method == null;
190+
if (!isIdle)
191+
{
192+
startTaskCallback = CreateWorkload<Func<ValueTask>>(instance, method);
193+
callback = ExecuteBlocking;
194+
}
195+
else
196+
{
197+
callback = Overhead;
198+
}
199+
200+
InvokeSingle = callback;
201+
202+
if (UseFallbackCode(codegenMode, unrollFactor))
203+
{
204+
unrolledCallback = Unroll(callback, unrollFactor);
205+
InvokeMultiple = InvokeMultipleHardcoded;
206+
}
207+
else
208+
{
209+
InvokeMultiple = EmitInvokeMultiple(this, nameof(callback), null, unrollFactor);
210+
}
211+
}
212+
213+
// must be kept in sync with VoidDeclarationsProvider.IdleImplementation
214+
private void Overhead() { }
215+
216+
// must be kept in sync with TaskDeclarationsProvider.TargetMethodDelegate
217+
private void ExecuteBlocking() => awaitHelper.GetResult(startTaskCallback.Invoke());
218+
219+
private void InvokeMultipleHardcoded(long repeatCount)
220+
{
221+
for (long i = 0; i < repeatCount; i++)
222+
unrolledCallback();
223+
}
224+
}
225+
180226
internal class BenchmarkActionValueTask<T> : BenchmarkActionBase
181227
{
182228
private readonly Helpers.AwaitHelper awaitHelper = new Helpers.AwaitHelper();

0 commit comments

Comments
 (0)