Skip to content

Faster strongly typed features for server FeatureCollection #31322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ private void ValidateNonOriginHostHeader(string hostText)

protected override void OnReset()
{
ResetHttp1Features();

_requestTimedOut = false;
_requestTargetForm = HttpRequestTarget.Unknown;
Expand All @@ -625,6 +624,10 @@ protected override void OnReset()
_requestCount++;

MinResponseDataRate = ServerOptions.Limits.MinResponseDataRate;

// Reset Http1 Features
_currentIHttpMinRequestBodyDataRateFeature = this;
_currentIHttpMinResponseDataRateFeature = this;
}

protected override void OnRequestProcessingEnding()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,6 @@ RouteValueDictionary IRouteValuesFeature.RouteValues

Stream IHttpResponseBodyFeature.Stream => ResponseBody;

protected void ResetHttp1Features()
{
_currentIHttpMinRequestBodyDataRateFeature = this;
_currentIHttpMinResponseDataRateFeature = this;
}

protected void ResetHttp2Features()
{
_currentIHttp2StreamIdFeature = this;
_currentIHttpResponseTrailersFeature = this;
_currentIHttpResetFeature = this;
}

protected void ResetHttp3Features()
{
_currentIHttpResponseTrailersFeature = this;
_currentIHttpResetFeature = this;
}

void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
{
OnStarting(callback, state);
Expand Down
169 changes: 84 additions & 85 deletions src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.Generated.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ protected override void OnReset()
_keepAlive = true;
_connectionAborted = false;

ResetHttp2Features();
// Reset Http2 Features
_currentIHttpMinRequestBodyDataRateFeature = this;
_currentIHttp2StreamIdFeature = this;
_currentIHttpResponseTrailersFeature = this;
_currentIHttpResetFeature = this;
}

protected override void OnRequestProcessingEnded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,10 @@ public void Tick(DateTimeOffset now)

protected override void OnReset()
{
ResetHttp3Features();
// Reset Http3 Features
_currentIHttpMinRequestBodyDataRateFeature = this;
_currentIHttpResponseTrailersFeature = this;
_currentIHttpResetFeature = this;
}

protected override void ApplicationAbort() => ApplicationAbort(new ConnectionAbortedException(CoreStrings.ConnectionAbortedByApplication), Http3ErrorCode.InternalError);
Expand Down
30 changes: 15 additions & 15 deletions src/Servers/Kestrel/shared/TransportConnection.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace Microsoft.AspNetCore.Connections
{
internal partial class TransportConnection : IFeatureCollection
{
private object? _currentIConnectionIdFeature;
private object? _currentIConnectionTransportFeature;
private object? _currentIConnectionItemsFeature;
private object? _currentIMemoryPoolFeature;
private object? _currentIConnectionLifetimeFeature;
internal protected IConnectionIdFeature? _currentIConnectionIdFeature;
internal protected IConnectionTransportFeature? _currentIConnectionTransportFeature;
internal protected IConnectionItemsFeature? _currentIConnectionItemsFeature;
internal protected IMemoryPoolFeature? _currentIMemoryPoolFeature;
internal protected IConnectionLifetimeFeature? _currentIConnectionLifetimeFeature;

private int _featureRevision;

Expand Down Expand Up @@ -137,23 +137,23 @@ private void ExtraFeatureSet(Type key, object? value)

if (key == typeof(IConnectionIdFeature))
{
_currentIConnectionIdFeature = value;
_currentIConnectionIdFeature = (IConnectionIdFeature?)value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benaadams shouldn't the unsafe cast be here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the this[Type key] = object version; so can't guarantee value is the correct type so Unsafe cast here would be bad

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that we had this bug today where you could basically store anything in here and it would explode on get

}
else if (key == typeof(IConnectionTransportFeature))
{
_currentIConnectionTransportFeature = value;
_currentIConnectionTransportFeature = (IConnectionTransportFeature?)value;
}
else if (key == typeof(IConnectionItemsFeature))
{
_currentIConnectionItemsFeature = value;
_currentIConnectionItemsFeature = (IConnectionItemsFeature?)value;
}
else if (key == typeof(IMemoryPoolFeature))
{
_currentIMemoryPoolFeature = value;
_currentIMemoryPoolFeature = (IMemoryPoolFeature?)value;
}
else if (key == typeof(IConnectionLifetimeFeature))
{
_currentIConnectionLifetimeFeature = value;
_currentIConnectionLifetimeFeature = (IConnectionLifetimeFeature?)value;
}
else
{
Expand Down Expand Up @@ -198,23 +198,23 @@ private void ExtraFeatureSet(Type key, object? value)
_featureRevision++;
if (typeof(TFeature) == typeof(IConnectionIdFeature))
{
_currentIConnectionIdFeature = feature;
_currentIConnectionIdFeature = (IConnectionIdFeature?)feature;
}
else if (typeof(TFeature) == typeof(IConnectionTransportFeature))
{
_currentIConnectionTransportFeature = feature;
_currentIConnectionTransportFeature = (IConnectionTransportFeature?)feature;
}
else if (typeof(TFeature) == typeof(IConnectionItemsFeature))
{
_currentIConnectionItemsFeature = feature;
_currentIConnectionItemsFeature = (IConnectionItemsFeature?)feature;
}
else if (typeof(TFeature) == typeof(IMemoryPoolFeature))
{
_currentIMemoryPoolFeature = feature;
_currentIMemoryPoolFeature = (IMemoryPoolFeature?)feature;
}
else if (typeof(TFeature) == typeof(IConnectionLifetimeFeature))
{
_currentIConnectionLifetimeFeature = feature;
_currentIConnectionLifetimeFeature = (IConnectionLifetimeFeature?)feature;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace Microsoft.AspNetCore.Connections
{
internal partial class TransportMultiplexedConnection : IFeatureCollection
{
private object? _currentIConnectionIdFeature;
private object? _currentIConnectionTransportFeature;
private object? _currentIConnectionItemsFeature;
private object? _currentIMemoryPoolFeature;
private object? _currentIConnectionLifetimeFeature;
internal protected IConnectionIdFeature? _currentIConnectionIdFeature;
internal protected IConnectionTransportFeature? _currentIConnectionTransportFeature;
internal protected IConnectionItemsFeature? _currentIConnectionItemsFeature;
internal protected IMemoryPoolFeature? _currentIMemoryPoolFeature;
internal protected IConnectionLifetimeFeature? _currentIConnectionLifetimeFeature;

private int _featureRevision;

Expand All @@ -27,11 +27,11 @@ internal partial class TransportMultiplexedConnection : IFeatureCollection
private void FastReset()
{
_currentIConnectionIdFeature = this;
_currentIConnectionTransportFeature = this;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an impossible cast; however since they were object? fields the compiler didn't detect it.

_currentIConnectionItemsFeature = this;
_currentIMemoryPoolFeature = this;
_currentIConnectionLifetimeFeature = this;

_currentIConnectionTransportFeature = null;
}

// Internal for testing
Expand Down Expand Up @@ -137,23 +137,23 @@ private void ExtraFeatureSet(Type key, object? value)

if (key == typeof(IConnectionIdFeature))
{
_currentIConnectionIdFeature = value;
_currentIConnectionIdFeature = (IConnectionIdFeature?)value;
}
else if (key == typeof(IConnectionTransportFeature))
{
_currentIConnectionTransportFeature = value;
_currentIConnectionTransportFeature = (IConnectionTransportFeature?)value;
}
else if (key == typeof(IConnectionItemsFeature))
{
_currentIConnectionItemsFeature = value;
_currentIConnectionItemsFeature = (IConnectionItemsFeature?)value;
}
else if (key == typeof(IMemoryPoolFeature))
{
_currentIMemoryPoolFeature = value;
_currentIMemoryPoolFeature = (IMemoryPoolFeature?)value;
}
else if (key == typeof(IConnectionLifetimeFeature))
{
_currentIConnectionLifetimeFeature = value;
_currentIConnectionLifetimeFeature = (IConnectionLifetimeFeature?)value;
}
else
{
Expand Down Expand Up @@ -198,23 +198,23 @@ private void ExtraFeatureSet(Type key, object? value)
_featureRevision++;
if (typeof(TFeature) == typeof(IConnectionIdFeature))
{
_currentIConnectionIdFeature = feature;
_currentIConnectionIdFeature = (IConnectionIdFeature?)feature;
}
else if (typeof(TFeature) == typeof(IConnectionTransportFeature))
{
_currentIConnectionTransportFeature = feature;
_currentIConnectionTransportFeature = (IConnectionTransportFeature?)feature;
}
else if (typeof(TFeature) == typeof(IConnectionItemsFeature))
{
_currentIConnectionItemsFeature = feature;
_currentIConnectionItemsFeature = (IConnectionItemsFeature?)feature;
}
else if (typeof(TFeature) == typeof(IMemoryPoolFeature))
{
_currentIMemoryPoolFeature = feature;
_currentIMemoryPoolFeature = (IMemoryPoolFeature?)feature;
}
else if (typeof(TFeature) == typeof(IConnectionLifetimeFeature))
{
_currentIConnectionLifetimeFeature = feature;
_currentIConnectionLifetimeFeature = (IConnectionLifetimeFeature?)feature;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CodeGenerator
{
public static class FeatureCollectionGenerator
{
public static string GenerateFile(string namespaceName, string className, string[] allFeatures, string[] implementedFeatures, string extraUsings, string fallbackFeatures)
public static string GenerateFile(string namespaceName, string className, string[] allFeatures, string[] implementedFeatures, string[] skipResetFeatures, string extraUsings, string fallbackFeatures)
{
// NOTE: This list MUST always match the set of feature interfaces implemented by TransportConnection.
// See also: src/Kestrel/Http/TransportConnection.FeatureCollection.cs
Expand All @@ -33,14 +33,14 @@ namespace {namespaceName}
{{
internal partial class {className} : IFeatureCollection
{{{Each(features, feature => $@"
private object? _current{feature.Name};")}
internal protected {feature.Name}? _current{feature.Name};")}

private int _featureRevision;

private List<KeyValuePair<Type, object>>? MaybeExtra;

private void FastReset()
{{{Each(implementedFeatures, feature => $@"
{{{Each(implementedFeatures.Where(f => !skipResetFeatures.Contains(f)), feature => $@"
_current{feature} = this;")}
{Each(allFeatures.Where(f => !implementedFeatures.Contains(f)), feature => $@"
_current{feature} = null;")}
Expand Down Expand Up @@ -133,7 +133,7 @@ private void ExtraFeatureSet(Type key, object? value)
{Each(features, feature => $@"
{(feature.Index != 0 ? "else " : "")}if (key == typeof({feature.Name}))
{{
_current{feature.Name} = value;
_current{feature.Name} = ({feature.Name}?)value;
}}")}
else
{{
Expand Down Expand Up @@ -167,7 +167,7 @@ private void ExtraFeatureSet(Type key, object? value)
_featureRevision++;{Each(features, feature => $@"
{(feature.Index != 0 ? "else " : "")}if (typeof(TFeature) == typeof({feature.Name}))
{{
_current{feature.Name} = feature;
_current{feature.Name} = ({feature.Name}?)feature;
}}")}
else
{{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ public static string GenerateFile()
"IRouteValuesFeature",
"IEndpointFeature"
};


// NOTE: Each item in this list MUST always be reset by each protocol in their OnReset() method
var skipResetFeatures = new[]
{
"IHttpMinRequestBodyDataRateFeature"
};

var usings = $@"
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Features.Authentication;
Expand All @@ -85,6 +91,7 @@ public static string GenerateFile()
className: "HttpProtocol",
allFeatures: allFeatures,
implementedFeatures: implementedFeatures,
skipResetFeatures: skipResetFeatures,
extraUsings: usings,
fallbackFeatures: "ConnectionFeatures");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace CodeGenerator
{
public class TransportConnectionFeatureCollection
Expand All @@ -27,6 +29,7 @@ public static string GenerateFile()
className: "TransportConnection",
allFeatures: features,
implementedFeatures: features,
skipResetFeatures: Array.Empty<string>(),
extraUsings: usings,
fallbackFeatures: null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace CodeGenerator
{
public class TransportMultiplexedConnectionFeatureCollection
Expand All @@ -9,14 +11,21 @@ public static string GenerateFile()
{
// NOTE: This list MUST always match the set of feature interfaces implemented by TransportConnectionBase.
// See also: shared/TransportConnectionBase.FeatureCollection.cs
var features = new[]
var allFeatures = new[]
{
"IConnectionIdFeature",
"IConnectionTransportFeature",
"IConnectionItemsFeature",
"IMemoryPoolFeature",
"IConnectionLifetimeFeature"
};
var implementedFeatures = new[]
{
"IConnectionIdFeature",
"IConnectionItemsFeature",
"IMemoryPoolFeature",
"IConnectionLifetimeFeature"
};

var usings = $@"
using Microsoft.AspNetCore.Connections.Features;
Expand All @@ -25,8 +34,9 @@ public static string GenerateFile()
return FeatureCollectionGenerator.GenerateFile(
namespaceName: "Microsoft.AspNetCore.Connections",
className: "TransportMultiplexedConnection",
allFeatures: features,
implementedFeatures: features,
allFeatures: allFeatures,
implementedFeatures: implementedFeatures,
skipResetFeatures: Array.Empty<string>(),
extraUsings: usings,
fallbackFeatures: null);
}
Expand Down