Skip to content
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

Remove some Linq usages from Kestrel.Core #47012

Merged
merged 2 commits into from
Mar 8, 2023

Conversation

eerhardt
Copy link
Member

@eerhardt eerhardt commented Mar 2, 2023

Similar to #47004, remove some Linq usages to reduce NativeAOT app size.

Unfortunately, these changes aren't yielding as big of a size impact. But since I made them, I figured I'd send a PR for them. If we don't think this is worth it, we can reject this PR.

On Windows x64, the BasicMinimalApi using EnableRequestDelegateGenerator=true, the app size goes from:

#47004: 9.56 MB (10,024,960 bytes)
PR: 9.54 MB (10,009,088 bytes)

@@ -179,7 +178,14 @@ public override CancellationToken ConnectionClosed

public Task StopEndpointsAsync(List<EndpointConfig> endpointsToStop, CancellationToken cancellationToken)
{
var transportsToStop = _transports.Where(t => t.EndpointConfig != null && endpointsToStop.Contains(t.EndpointConfig)).ToList();
var transportsToStop = new List<ActiveTransport>();
Copy link
Member

Choose a reason for hiding this comment

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

How many do we expect? Almost none? Almost all? I'm thinking "list initial size" reasons

Copy link
Member Author

Choose a reason for hiding this comment

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

At most it is _transports.Count. At minimum, it is 0. I'm not sure what the typical case would be.

This is only called on RebindAsync, which happens when the configuration changes and there are endpoints to stop.

Copy link
Member

Choose a reason for hiding this comment

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

I'd leave it. There are much bigger optimizations we could make to endpoint rebinding if we cared more about this scenario. For example, we could start bringing new endpoints online before completely stopping all the removed ones if they don't conflict.

@@ -179,7 +178,14 @@ public override CancellationToken ConnectionClosed

public Task StopEndpointsAsync(List<EndpointConfig> endpointsToStop, CancellationToken cancellationToken)
{
var transportsToStop = _transports.Where(t => t.EndpointConfig != null && endpointsToStop.Contains(t.EndpointConfig)).ToList();
var transportsToStop = new List<ActiveTransport>();
Copy link
Member

Choose a reason for hiding this comment

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

I'd leave it. There are much bigger optimizations we could make to endpoint rebinding if we cared more about this scenario. For example, we could start bringing new endpoints online before completely stopping all the removed ones if they don't conflict.

@@ -38,7 +38,20 @@ public class KestrelServerOptions
// The following two lists configure the endpoints that Kestrel should listen to. If both lists are empty, the "urls" config setting (e.g. UseUrls) is used.
internal List<ListenOptions> CodeBackedListenOptions { get; } = new List<ListenOptions>();
internal List<ListenOptions> ConfigurationBackedListenOptions { get; } = new List<ListenOptions>();
internal IEnumerable<ListenOptions> ListenOptions => CodeBackedListenOptions.Concat(ConfigurationBackedListenOptions);

internal ListenOptions[] GetListenOptions()
Copy link
Member

Choose a reason for hiding this comment

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

Would it really be that bad to leave it as a property? It's not that expensive, and might be easier than tracking down all the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can leave it as a property, but I really dislike allocating in a property getter.

Copy link
Member

@mgravell mgravell Mar 3, 2023

Choose a reason for hiding this comment

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

Question: are the two lists mutated much at the moment? Thery could be stored in a single list or array, with an int that marks the split between the two chunks. Then the combined list is "ignore the split", and the other two lists are just separate pieces of the single chunk - ideally returning either RwadOnlyMemory-ListenOptions, ReadOnlySpan-ListenOptions, or ArraySegment-ListenOptions. If it is easiest to use a list when building, theres a CollectionsMarshal API to sneak inside when needed

Copy link
Member Author

Choose a reason for hiding this comment

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

From a quick investigation, for a dotnet new api template app, neither list is mutated.

The way CodeBackedListenOptions gets mutated is if someone adds endpoints in code, ex: builder.WebHost.ConfigureKestrel(o => o.ListenLocalhost(5556));.

The way ConfigurationBackedListenOptions get mutated is if someone adds endpoints through configuration, ex: builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>() { { "Kestrel:EndPoints:Http:Url", "http://localhost:5555" } });.

They are kept separate in order to correctly "reload" on configuration changes. Kestrel knows which ones were added by the previous configuration, and can remove any endpoints that are now removed.

I think your idea might be a good idea, but I think it is out of scope for this PR. Feel free to open a separate PR for it.

@eerhardt eerhardt merged commit 34a16dc into dotnet:main Mar 8, 2023
@ghost ghost added this to the 8.0-preview3 milestone Mar 8, 2023
@eerhardt eerhardt deleted the RemoveLinqKestrel branch March 8, 2023 22:39
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
@eerhardt eerhardt added the Perf label Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Perf
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants