-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
DeagleGross
wants to merge
17
commits into
dotnet:main
Choose a base branch
from
DeagleGross:dmkorolev/owin/environment-allocations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c05d859
reduce allocations for default usage in OwinEnvironment
DeagleGross be3c7d4
init sample
DeagleGross 8cd24a2
init the owin sample
DeagleGross efbca57
Merge branch 'main' into dmkorolev/owin/environment-allocations
DeagleGross fb27a6e
no-public API change
DeagleGross a5f8264
remove dependency on sample
DeagleGross 478c6db
intro benchmark
DeagleGross a61aff4
make `OwinEntries` sealed
DeagleGross 8ae8104
improve port convert + enumerators for headers
DeagleGross e71d10a
Merge branch 'main' into dmkorolev/owin/environment-allocations
DeagleGross ebc7e4d
dont pre-allocate port buffer
DeagleGross 4ba14d4
fix build errors
DeagleGross 7c236dd
address PR review
DeagleGross 32da5f8
suppress NativeAOT warnings for Owin sample
DeagleGross ff43fb2
address PR comments
DeagleGross d5e05e8
merge main
DeagleGross 09dd500
address PR comments
DeagleGross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...nchmarks/Microsoft.AspNetCore.Owin.Microbenchmarks/Benchmarks/OwinEnvironmentBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...icrosoft.AspNetCore.Owin.Microbenchmarks/Microsoft.AspNetCore.Owin.Microbenchmarks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
6 changes: 6 additions & 0 deletions
6
src/Http/Owin/benchmarks/Microsoft.AspNetCore.Owin.Microbenchmarks/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this not share as
public struct Enumerator : IEnumerator<KeyValuePair<string, T>>, IEnumerator
with the one above? ConvertingEnumerator or somethingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed to
ConvertingEnumerator
. I dont think I can make a generic impl for inner enumerator ofDictionaryStringArrayWrapper
andDictionaryStringValuesWrapper
. That was your idea, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd have to pass in a Convert delegate to reuse the same one. Maybe not worth the bother.