-
Notifications
You must be signed in to change notification settings - Fork 10k
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
c05d859
be3c7d4
8cd24a2
efbca57
fb27a6e
a5f8264
478c6db
a61aff4
8ae8104
e71d10a
ebc7e4d
4ba14d4
7c236dd
32da5f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,16 @@ | |
|
||
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; | ||
|
||
|
@@ -100,9 +100,11 @@ | |
} | ||
} | ||
|
||
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)); | ||
|
||
|
@@ -119,4 +121,40 @@ | |
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; | ||
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-ci (Build Test: macOS)src/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-quarantined-prsrc/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-cisrc/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
Check failure on line 156 in src/Http/Owin/src/DictionaryStringValuesWrapper.cs Azure Pipelines / aspnetcore-cisrc/Http/Owin/src/DictionaryStringValuesWrapper.cs#L156
|
||
|
||
void IEnumerator.Reset() => throw new NotImplementedException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
} |
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.
nit: change to
NotSupportedException
-NotImplementedException
says "the library author is silly",NotSupportedException
says "the caller is silly"trivial example in support of this (see
void IEnumerator.Reset()
): https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABADAAgwRhQG4BYAKHIwCYcAxCCHAb3LwGY982AeASwDsALgD4cAcRiD6EAM4AKAJTNO+HMFgBDANZEAvjgPldQA==