Skip to content

perf: improve allocations in OwinEnvironment #58917

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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions AspNetCore.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
<Project Path="src/Http/Metadata/src/Microsoft.AspNetCore.Metadata.csproj" />
</Folder>
<Folder Name="/src/Http/Owin/" Id="c6dae135-6509-c765-458b-3693a7b28e8c">
<Project Path="src/Http/Owin/benchmarks/Microsoft.AspNetCore.Owin.Microbenchmarks/Microsoft.AspNetCore.Owin.Microbenchmarks.csproj" />
<Project Path="src/Http/Owin/src/Microsoft.AspNetCore.Owin.csproj" />
<Project Path="src/Http/Owin/test/Microsoft.AspNetCore.Owin.Tests.csproj" />
</Folder>
Expand All @@ -357,6 +358,7 @@
<Folder Name="/src/Http/samples/" Id="0e46e96b-2613-2f61-4250-fc4a97d94f4c">
<Project Path="src/Http/samples/MinimalSample/MinimalSample.csproj" />
<Project Path="src/Http/samples/MinimalSampleFSharp/MinimalSampleFSharp.fsproj" />
<Project Path="src/Http/samples/MinimalSampleOwin/MinimalSampleOwin.csproj" />
<Project Path="src/Http/samples/SampleApp/HttpAbstractions.SampleApp.csproj" />
</Folder>
<Folder Name="/src/Http/WebUtilities/" Id="1aadc95f-e3b5-447b-ddcf-108db21eb9ed">
Expand Down
10 changes: 6 additions & 4 deletions src/Http/HttpAbstractions.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@
"src\\Http\\Http\\src\\Microsoft.AspNetCore.Http.csproj",
"src\\Http\\Http\\test\\Microsoft.AspNetCore.Http.Tests.csproj",
"src\\Http\\Metadata\\src\\Microsoft.AspNetCore.Metadata.csproj",
"src\\Http\\Owin\\benchmarks\\Microsoft.AspNetCore.Owin.Microbenchmarks\\Microsoft.AspNetCore.Owin.Microbenchmarks.csproj",
"src\\Http\\Owin\\src\\Microsoft.AspNetCore.Owin.csproj",
"src\\Http\\Owin\\test\\Microsoft.AspNetCore.Owin.Tests.csproj",
"src\\Http\\Routing.Abstractions\\src\\Microsoft.AspNetCore.Routing.Abstractions.csproj",
"src\\Http\\Routing.Abstractions\\test\\Microsoft.AspNetCore.Mvc.Routing.Abstractions.Tests.csproj",
"src\\Http\\Routing\\perf\\Microbenchmarks\\Microsoft.AspNetCore.Routing.Microbenchmarks.csproj",
"src\\Http\\Routing\\src\\Microsoft.AspNetCore.Routing.csproj",
"src\\Http\\Routing\\test\\FunctionalTests\\Microsoft.AspNetCore.Routing.FunctionalTests.csproj",
"src\\Http\\Routing\\test\\UnitTests\\Microsoft.AspNetCore.Routing.Tests.csproj",
"src\\Http\\Routing\\test\\testassets\\Benchmarks\\Benchmarks.csproj",
"src\\Http\\Routing\\test\\testassets\\RoutingSandbox\\RoutingSandbox.csproj",
"src\\Http\\Routing\\test\\testassets\\RoutingWebSite\\RoutingWebSite.csproj",
"src\\Http\\Routing\\test\\UnitTests\\Microsoft.AspNetCore.Routing.Tests.csproj",
"src\\Http\\samples\\MinimalSample\\MinimalSample.csproj",
"src\\Http\\samples\\SampleApp\\HttpAbstractions.SampleApp.csproj",
"src\\Http\\WebUtilities\\perf\\Microbenchmarks\\Microsoft.AspNetCore.WebUtilities.Microbenchmarks.csproj",
"src\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj",
"src\\Http\\WebUtilities\\test\\Microsoft.AspNetCore.WebUtilities.Tests.csproj",
"src\\Http\\samples\\MinimalSampleOwin\\MinimalSampleOwin.csproj",
"src\\Http\\samples\\MinimalSample\\MinimalSample.csproj",
"src\\Http\\samples\\SampleApp\\HttpAbstractions.SampleApp.csproj",
"src\\Middleware\\CORS\\src\\Microsoft.AspNetCore.Cors.csproj",
"src\\Middleware\\Diagnostics.Abstractions\\src\\Microsoft.AspNetCore.Diagnostics.Abstractions.csproj",
"src\\Middleware\\Diagnostics\\src\\Microsoft.AspNetCore.Diagnostics.csproj",
Expand All @@ -72,4 +74,4 @@
"src\\WebEncoders\\src\\Microsoft.Extensions.WebEncoders.csproj"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

[assembly: BenchmarkDotNet.Attributes.AspNetCoreBenchmark]
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Owin.Microbenchmarks.Benchmarks;

[MemoryDiagnoser]
public class OwinEnvironmentBenchmark
{
const int RequestCount = 10000;

RequestDelegate _noOperationRequestDelegate;
RequestDelegate _accessPortsRequestDelegate;
RequestDelegate _accessHeadersRequestDelegate;

HttpContext _defaultHttpContext;
HttpContext _httpContextWithHeaders;

[GlobalSetup]
public void GlobalSetup()
{
_noOperationRequestDelegate = BuildRequestDelegate();
_accessPortsRequestDelegate = BuildRequestDelegate(beforeOwinInvokeAction: env =>
{
_ = env.TryGetValue("server.LocalPort", out var localPort);
_ = env.TryGetValue("server.RemotePort", out var remotePort);
});
_accessHeadersRequestDelegate = BuildRequestDelegate(
beforeOwinInvokeAction: env =>
{
_ = env.TryGetValue("owin.RequestHeaders", out var requestHeaders);
},
afterOwinInvokeAction: env =>
{
_ = env.TryGetValue("owin.ResponseHeaders", out var responseHeaders);
}
);

_defaultHttpContext = new DefaultHttpContext();

_httpContextWithHeaders = new DefaultHttpContext();
_httpContextWithHeaders.Request.Headers["CustomRequestHeader1"] = "CustomRequestValue";
_httpContextWithHeaders.Request.Headers["CustomRequestHeader2"] = "CustomRequestValue";
_httpContextWithHeaders.Response.Headers["CustomResponseHeader1"] = "CustomResponseValue";
_httpContextWithHeaders.Response.Headers["CustomResponseHeader2"] = "CustomResponseValue";
}

[Benchmark]
public async Task OwinRequest_NoOperation()
{
foreach (var i in Enumerable.Range(0, RequestCount))
{
await _noOperationRequestDelegate(_defaultHttpContext);
}
}

[Benchmark]
public async Task OwinRequest_AccessPorts()
{
foreach (var i in Enumerable.Range(0, RequestCount))
{
await _accessPortsRequestDelegate(_defaultHttpContext);
}
}

[Benchmark]
public async Task OwinRequest_AccessHeaders()
{
foreach (var i in Enumerable.Range(0, RequestCount))
{
await _accessHeadersRequestDelegate(_httpContextWithHeaders);
}
}

private static RequestDelegate BuildRequestDelegate(
Action<IDictionary<string, object>> beforeOwinInvokeAction = null,
Action<IDictionary<string, object>> afterOwinInvokeAction = null)
{
var serviceProvider = new ServiceCollection().BuildServiceProvider();
var builder = new ApplicationBuilder(serviceProvider);

return builder.UseOwin(addToPipeline =>
{
addToPipeline(next =>
{
return async env =>
{
if (beforeOwinInvokeAction is not null)
{
beforeOwinInvokeAction(env);
}

await next(env);

if (afterOwinInvokeAction is not null)
{
afterOwinInvokeAction(env);
}
};
});
}).Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="BenchmarkDotNet" />
<Reference Include="Microsoft.AspNetCore.Http" />
<Reference Include="Microsoft.AspNetCore.Owin" />
<Reference Include="Microsoft.Extensions.DependencyInjection" />

<Compile Include="$(SharedSourceRoot)BenchmarkRunner\*.cs" />
</ItemGroup>

</Project>
50 changes: 44 additions & 6 deletions src/Http/Owin/src/DictionaryStringArrayWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace Microsoft.AspNetCore.Owin;

internal sealed class DictionaryStringArrayWrapper : IDictionary<string, string[]>
{
public readonly IHeaderDictionary Inner;

public DictionaryStringArrayWrapper(IHeaderDictionary inner)
{
Inner = inner;
}

public readonly IHeaderDictionary Inner;

private static KeyValuePair<string, StringValues> Convert(KeyValuePair<string, string[]> item) => new KeyValuePair<string, StringValues>(item.Key, item.Value);
private static KeyValuePair<string, StringValues> Convert(KeyValuePair<string, string[]> item) => new(item.Key, item.Value);

private static KeyValuePair<string, string[]> Convert(KeyValuePair<string, StringValues> item) => new KeyValuePair<string, string[]>(item.Key, item.Value);
private static KeyValuePair<string, string[]> Convert(KeyValuePair<string, StringValues> item) => new(item.Key, item.Value);

private string[] Convert(StringValues item) => item;

Expand Down Expand Up @@ -55,9 +55,11 @@ void ICollection<KeyValuePair<string, string[]>>.CopyTo(KeyValuePair<string, str
}
}

IEnumerator IEnumerable.GetEnumerator() => Inner.Select(Convert).GetEnumerator();
public ConvertingEnumerator GetEnumerator() => new ConvertingEnumerator(Inner);

IEnumerator<KeyValuePair<string, string[]>> IEnumerable<KeyValuePair<string, string[]>>.GetEnumerator() => new ConvertingEnumerator(Inner);

IEnumerator<KeyValuePair<string, string[]>> IEnumerable<KeyValuePair<string, string[]>>.GetEnumerator() => Inner.Select(Convert).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => new ConvertingEnumerator(Inner);

bool ICollection<KeyValuePair<string, string[]>>.Remove(KeyValuePair<string, string[]> item) => Inner.Remove(Convert(item));

Expand All @@ -74,4 +76,40 @@ bool IDictionary<string, string[]>.TryGetValue(string key, out string[] value)
value = default(StringValues);
return false;
}

public struct ConvertingEnumerator : IEnumerator<KeyValuePair<string, string[]>>, IEnumerator
{
private IEnumerator<KeyValuePair<string, StringValues>> _inner;
private KeyValuePair<string, string[]> _current;

internal ConvertingEnumerator(IDictionary<string, StringValues> inner)
{
_inner = inner.GetEnumerator();
_current = default;
}

public void Dispose()
{
_inner?.Dispose();
_inner = null;
}

public bool MoveNext()
{
if (!_inner.MoveNext())
{
_current = default;
return false;
}

_current = Convert(_inner.Current);
return true;
}

public KeyValuePair<string, string[]> Current => _current;

object IEnumerator.Current => Current;

void IEnumerator.Reset() => throw new NotSupportedException();
}
}
50 changes: 44 additions & 6 deletions src/Http/Owin/src/DictionaryStringValuesWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ namespace Microsoft.AspNetCore.Owin;

internal sealed class DictionaryStringValuesWrapper : IHeaderDictionary
{
public readonly IDictionary<string, string[]> Inner;

public DictionaryStringValuesWrapper(IDictionary<string, string[]> inner)
{
Inner = inner;
}

public readonly IDictionary<string, string[]> Inner;

private static KeyValuePair<string, StringValues> Convert(KeyValuePair<string, string[]> item) => new KeyValuePair<string, StringValues>(item.Key, item.Value);
private static KeyValuePair<string, StringValues> Convert(KeyValuePair<string, string[]> item) => new(item.Key, item.Value);

private static KeyValuePair<string, string[]> Convert(KeyValuePair<string, StringValues> item) => new KeyValuePair<string, string[]>(item.Key, item.Value);
private static KeyValuePair<string, string[]> Convert(KeyValuePair<string, StringValues> item) => new(item.Key, item.Value);

private StringValues Convert(string[] item) => item;

Expand Down Expand Up @@ -100,9 +100,11 @@ void ICollection<KeyValuePair<string, StringValues>>.CopyTo(KeyValuePair<string,
}
}

IEnumerator IEnumerable.GetEnumerator() => Inner.Select(Convert).GetEnumerator();
public ConvertingEnumerator GetEnumerator() => new ConvertingEnumerator(Inner);

IEnumerator<KeyValuePair<string, StringValues>> IEnumerable<KeyValuePair<string, StringValues>>.GetEnumerator() => new ConvertingEnumerator(Inner);

IEnumerator<KeyValuePair<string, StringValues>> IEnumerable<KeyValuePair<string, StringValues>>.GetEnumerator() => Inner.Select(Convert).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => new ConvertingEnumerator(Inner);

bool ICollection<KeyValuePair<string, StringValues>>.Remove(KeyValuePair<string, StringValues> item) => Inner.Remove(Convert(item));

Expand All @@ -119,4 +121,40 @@ bool IDictionary<string, StringValues>.TryGetValue(string key, out StringValues
value = default(StringValues);
return false;
}

public struct ConvertingEnumerator : IEnumerator<KeyValuePair<string, StringValues>>, IEnumerator
{
private IEnumerator<KeyValuePair<string, string[]>> _inner;
private KeyValuePair<string, StringValues> _current;

internal ConvertingEnumerator(IDictionary<string, string[]> inner)
{
_inner = inner.GetEnumerator();
_current = default;
}

public void Dispose()
{
_inner?.Dispose();
_inner = null;
}

public bool MoveNext()
{
if (!_inner.MoveNext())
{
_current = default;
return false;
}

_current = Convert(_inner.Current);
return true;
}

public KeyValuePair<string, StringValues> Current => _current;

object IEnumerator.Current => Current;

void IEnumerator.Reset() => throw new NotSupportedException();
}
}
Loading
Loading