Skip to content

Commit 13a4608

Browse files
authored
HTTP/3: Remove AsTask allocations (#38559)
1 parent d4d4c4b commit 13a4608

File tree

5 files changed

+14
-29
lines changed

5 files changed

+14
-29
lines changed

src/Hosting/Hosting/src/Internal/WebHost.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,14 @@ public WebHost(
7979
_hostingServiceProvider = hostingServiceProvider;
8080
_applicationServiceCollection.AddSingleton<ApplicationLifetime>();
8181
// There's no way to to register multiple service types per definition. See https://github.com/aspnet/DependencyInjection/issues/360
82-
#pragma warning disable CS8634 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'class' constraint.
83-
_applicationServiceCollection.AddSingleton(services
84-
=> services.GetService<ApplicationLifetime>() as IHostApplicationLifetime);
82+
_applicationServiceCollection.AddSingleton<IHostApplicationLifetime>(services
83+
=> services.GetService<ApplicationLifetime>()!);
8584
#pragma warning disable CS0618 // Type or member is obsolete
86-
_applicationServiceCollection.AddSingleton(services
87-
=> services.GetService<ApplicationLifetime>() as AspNetCore.Hosting.IApplicationLifetime);
88-
_applicationServiceCollection.AddSingleton(services
89-
=> services.GetService<ApplicationLifetime>() as Extensions.Hosting.IApplicationLifetime);
85+
_applicationServiceCollection.AddSingleton<AspNetCore.Hosting.IApplicationLifetime>(services
86+
=> services.GetService<ApplicationLifetime>()!);
87+
_applicationServiceCollection.AddSingleton<Extensions.Hosting.IApplicationLifetime>(services
88+
=> services.GetService<ApplicationLifetime>()!);
9089
#pragma warning restore CS0618 // Type or member is obsolete
91-
#pragma warning restore CS8634 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'class' constraint.
9290
_applicationServiceCollection.AddSingleton<HostedServiceExecutor>();
9391
}
9492

src/Http/Http.Abstractions/src/Extensions/HttpResponseWritingExtensions.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Diagnostics.CodeAnalysis;
65
using System.IO.Pipelines;
76
using System.Text;
8-
using System.Threading;
9-
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Internal;
108

119
namespace Microsoft.AspNetCore.Http;
1210

@@ -78,15 +76,7 @@ public static class HttpResponseWritingExtensions
7876

7977
Write(response, text, encoding);
8078

81-
var flushAsyncTask = response.BodyWriter.FlushAsync(cancellationToken);
82-
if (flushAsyncTask.IsCompletedSuccessfully)
83-
{
84-
// Most implementations of ValueTask reset state in GetResult, so call it before returning a completed task.
85-
flushAsyncTask.GetAwaiter().GetResult();
86-
return Task.CompletedTask;
87-
}
88-
89-
return flushAsyncTask.AsTask();
79+
return response.BodyWriter.FlushAsync(cancellationToken).GetAsTask();
9080
}
9181

9282
private static async Task StartAndWriteAsyncAwaited(this HttpResponse response, string text, Encoding encoding, CancellationToken cancellationToken, Task startAsyncTask)

src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
2525
<Compile Include="$(SharedSourceRoot)ParameterDefaultValue\*.cs" />
2626
<Compile Include="$(SharedSourceRoot)PropertyHelper\**\*.cs" />
2727
<Compile Include="$(SharedSourceRoot)\UrlDecoder\UrlDecoder.cs" Link="UrlDecoder.cs" />
28+
<Compile Include="$(SharedSourceRoot)ValueTaskExtensions\**\*.cs" />
2829
</ItemGroup>
2930

3031
<ItemGroup>

src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Buffers;
6-
using System.Collections.Generic;
75
using System.Diagnostics;
86
using System.IO.Pipelines;
97
using System.Net.Http;
108
using System.Net.Http.QPack;
11-
using System.Text;
12-
using System.Threading;
13-
using System.Threading.Tasks;
149
using Microsoft.AspNetCore.Connections;
1510
using Microsoft.AspNetCore.Connections.Features;
1611
using Microsoft.AspNetCore.Http;
12+
using Microsoft.AspNetCore.Internal;
1713
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
1814
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
1915
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.PipeWriterHelpers;
@@ -129,7 +125,7 @@ internal Task WriteSettingsAsync(List<Http3PeerSetting> settings)
129125
_outgoingFrame.Length = totalLength;
130126
_outputWriter.Advance(totalLength);
131127

132-
return _outputWriter.FlushAsync().AsTask();
128+
return _outputWriter.FlushAsync().GetAsTask();
133129
}
134130

135131
internal static int CalculateSettingsSize(List<Http3PeerSetting> settings)
@@ -159,7 +155,7 @@ internal Task WriteStreamIdAsync(long id)
159155
{
160156
var buffer = _outputWriter.GetSpan(8);
161157
_outputWriter.Advance(VariableLengthIntegerHelper.WriteInteger(buffer, id));
162-
return _outputWriter.FlushAsync().AsTask();
158+
return _outputWriter.FlushAsync().GetAsTask();
163159
}
164160

165161
public ValueTask<FlushResult> WriteDataAsync(in ReadOnlySequence<byte> data)

src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.Connections;
1111
using Microsoft.AspNetCore.Connections.Features;
1212
using Microsoft.AspNetCore.Hosting.Server;
13+
using Microsoft.AspNetCore.Internal;
1314
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
1415
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
1516
using Microsoft.Extensions.Logging;
@@ -718,8 +719,7 @@ private Task ProcessDataFrameAsync(in ReadOnlySequence<byte> payload)
718719
RequestBodyPipe.Writer.Write(segment.Span);
719720
}
720721

721-
// TODO this can be better.
722-
return RequestBodyPipe.Writer.FlushAsync().AsTask();
722+
return RequestBodyPipe.Writer.FlushAsync().GetAsTask();
723723
}
724724

725725
protected override void OnReset()

0 commit comments

Comments
 (0)