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

perf: improve allocations in OwinEnvironment #58917

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
38 changes: 38 additions & 0 deletions AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.OpenAp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.App.SourceGenerators", "src\Framework\AspNetCoreAnalyzers\src\SourceGenerators\Microsoft.AspNetCore.App.SourceGenerators.csproj", "{C3928C15-1836-46DB-A09D-9EFBCCA33E08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalSampleOwin", "src\Http\samples\MinimalSampleOwin\MinimalSampleOwin.csproj", "{95032BC4-8BE5-4703-8075-93CB9DFF93EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Owin.Microbenchmarks", "src\Http\Owin\benchmarks\Microsoft.AspNetCore.Owin.Microbenchmarks\Microsoft.AspNetCore.Owin.Microbenchmarks.csproj", "{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -10977,6 +10981,38 @@ Global
{C3928C15-1836-46DB-A09D-9EFBCCA33E08}.Release|x64.Build.0 = Release|Any CPU
{C3928C15-1836-46DB-A09D-9EFBCCA33E08}.Release|x86.ActiveCfg = Release|Any CPU
{C3928C15-1836-46DB-A09D-9EFBCCA33E08}.Release|x86.Build.0 = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|arm64.ActiveCfg = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|arm64.Build.0 = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|x64.ActiveCfg = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|x64.Build.0 = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|x86.ActiveCfg = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Debug|x86.Build.0 = Debug|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|Any CPU.Build.0 = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|arm64.ActiveCfg = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|arm64.Build.0 = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|x64.ActiveCfg = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|x64.Build.0 = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|x86.ActiveCfg = Release|Any CPU
{95032BC4-8BE5-4703-8075-93CB9DFF93EF}.Release|x86.Build.0 = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|arm64.ActiveCfg = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|arm64.Build.0 = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|x64.ActiveCfg = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|x64.Build.0 = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|x86.ActiveCfg = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Debug|x86.Build.0 = Debug|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|Any CPU.Build.0 = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|arm64.ActiveCfg = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|arm64.Build.0 = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|x64.ActiveCfg = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|x64.Build.0 = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|x86.ActiveCfg = Release|Any CPU
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -11874,6 +11910,8 @@ Global
{B32FF7A7-9CB3-4DCD-AE97-3B2594DB9DAC} = {2299CCD8-8F9C-4F2B-A633-9BF4DA81022B}
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7} = {B32FF7A7-9CB3-4DCD-AE97-3B2594DB9DAC}
{C3928C15-1836-46DB-A09D-9EFBCCA33E08} = {B5D98AEB-9409-4280-8225-9C1EC6A791B2}
{95032BC4-8BE5-4703-8075-93CB9DFF93EF} = {EB5E294B-9ED5-43BF-AFA9-1CD2327F3DC1}
{E0FBCBF6-030C-4F15-A58A-F0F33DCC26E9} = {AA465A56-D9D0-4684-95B0-192F4436C582}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}
Expand Down
8 changes: 5 additions & 3 deletions src/Http/HttpAbstractions.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,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 Down
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run();
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 Enumerator GetEnumerator() => new Enumerator(Inner);

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

IEnumerator<KeyValuePair<string, string[]>> IEnumerable<KeyValuePair<string, string[]>>.GetEnumerator() => Inner.Select(Convert).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(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 Enumerator : IEnumerator<KeyValuePair<string, string[]>>, IEnumerator
{
private IEnumerator<KeyValuePair<string, StringValues>> _inner;
private KeyValuePair<string, string[]> _current;

internal Enumerator(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 NotImplementedException();
DeagleGross marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 Enumerator GetEnumerator() => new Enumerator(Inner);

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

IEnumerator<KeyValuePair<string, StringValues>> IEnumerable<KeyValuePair<string, StringValues>>.GetEnumerator() => Inner.Select(Convert).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(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 Enumerator : IEnumerator<KeyValuePair<string, StringValues>>, IEnumerator
{
private IEnumerator<KeyValuePair<string, string[]>> _inner;
private KeyValuePair<string, StringValues> _current;

internal Enumerator(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 NotImplementedException();
DeagleGross marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading