Skip to content

Commit f81fa85

Browse files
Avoid boxing flush ValueTask if already complete (#30)
* Avoid boxing flush ValueTask if already complete * Bump version to 0.6.2 * Update src/RazorSlices/ValueTaskExtensions.cs Co-authored-by: Brennan <[email protected]>
1 parent 46c11c2 commit f81fa85

5 files changed

+45
-15
lines changed

src/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionPrefix>0.6.1</VersionPrefix>
4+
<VersionPrefix>0.6.2</VersionPrefix>
55
<!-- VersionSuffix used for local builds -->
66
<VersionSuffix>dev</VersionSuffix>
77
<!-- VersionSuffix to be used for CI builds -->

src/RazorSlices/RazorSlice.PipeWriterExtensions.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,5 @@ public static async ValueTask RenderAsync(this RazorSlice razorSlice, Stream str
3737
}
3838

3939
private static Func<CancellationToken, ValueTask> GetFlushWrapper(PipeWriter pipeWriter)
40-
=> ct => AsValueTask(pipeWriter.FlushAsync(ct));
41-
42-
private static ValueTask AsValueTask<TResult>(ValueTask<TResult> valueTask)
43-
{
44-
if (valueTask.IsCompletedSuccessfully)
45-
{
46-
var _ = valueTask.GetAwaiter().GetResult();
47-
return default;
48-
}
49-
50-
return new ValueTask(valueTask.AsTask());
51-
}
40+
=> ct => pipeWriter.FlushAsync(ct).GetAsValueTask();
5241
}

src/RazorSlices/RazorSliceHttpResult.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Task IResult.ExecuteAsync(HttpContext httpContext)
5353

5454
if (renderTask.HandleSynchronousCompletion())
5555
{
56-
return httpContext.Response.BodyWriter.FlushAsync(httpContext.RequestAborted).AsTask();
56+
return httpContext.Response.BodyWriter.FlushAsync(httpContext.RequestAborted).GetAsTask();
5757
}
5858

5959
return AwaitRenderTaskAndFlushResponse(renderTask, httpContext.Response.BodyWriter, httpContext.RequestAborted);

src/RazorSlices/RazorSliceHttpResultOfTModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Task IResult.ExecuteAsync(HttpContext httpContext)
5151

5252
if (renderTask.HandleSynchronousCompletion())
5353
{
54-
return httpContext.Response.BodyWriter.FlushAsync(httpContext.RequestAborted).AsTask();
54+
return httpContext.Response.BodyWriter.FlushAsync(httpContext.RequestAborted).GetAsTask();
5555
}
5656

5757
return AwaitRenderTaskAndFlushResponse(renderTask, httpContext.Response.BodyWriter, httpContext.RequestAborted);
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Taken from https://github.com/dotnet/aspnetcore/blob/4eae56d8f7315cbd49fcbd760341940e3d087aa5/src/Shared/ValueTaskExtensions/ValueTaskExtensions.cs
2+
3+
using System.IO.Pipelines;
4+
using System.Runtime.CompilerServices;
5+
6+
namespace RazorSlices;
7+
8+
internal static class ValueTaskExtensions
9+
{
10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11+
public static Task GetAsTask(this in ValueTask<FlushResult> valueTask)
12+
{
13+
// Try to avoid the allocation from AsTask
14+
if (valueTask.IsCompletedSuccessfully)
15+
{
16+
// Signal consumption to the IValueTaskSource
17+
var _ = valueTask.GetAwaiter().GetResult();
18+
return Task.CompletedTask;
19+
}
20+
else
21+
{
22+
return valueTask.AsTask();
23+
}
24+
}
25+
26+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27+
public static ValueTask GetAsValueTask(this in ValueTask<FlushResult> valueTask)
28+
{
29+
// Try to avoid the allocation from AsTask
30+
if (valueTask.IsCompletedSuccessfully)
31+
{
32+
// Signal consumption to the IValueTaskSource
33+
var _ = valueTask.GetAwaiter().GetResult();
34+
return default;
35+
}
36+
else
37+
{
38+
return new ValueTask(valueTask.AsTask());
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)