This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Use pooled StringBuilder
to reduce allocations when adding response cookies
#591
Closed
+151
−30
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2,31 +2,64 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Http.Internal; | ||
using Microsoft.Extensions.ObjectPool; | ||
|
||
namespace Microsoft.AspNetCore.Http.Features.Internal | ||
{ | ||
/// <summary> | ||
/// Default implementation of <see cref="IResponseCookiesFeature"/>. | ||
/// </summary> | ||
public class ResponseCookiesFeature : IResponseCookiesFeature | ||
{ | ||
private FeatureReferences<IHttpResponseFeature> _features; | ||
private FeatureReferences<IServiceProvidersFeature> _services; | ||
private IResponseCookies _cookiesCollection; | ||
|
||
/// <summary> | ||
/// Initializes a new <see cref="ResponseCookiesFeature"/> instance. | ||
/// </summary> | ||
/// <param name="features"> | ||
/// <see cref="IFeatureCollection"/> containing all defined features, including this | ||
/// <see cref="IResponseCookiesFeature"/> and the <see cref="IHttpResponseFeature"/>. | ||
/// </param> | ||
public ResponseCookiesFeature(IFeatureCollection features) | ||
{ | ||
_features = new FeatureReferences<IHttpResponseFeature>(features); | ||
_services = new FeatureReferences<IServiceProvidersFeature>(features); | ||
} | ||
|
||
private IHttpResponseFeature HttpResponseFeature => | ||
_features.Fetch(ref _features.Cache, f => null); | ||
/// <inheritdoc /> | ||
/// <value>Default is <c>100</c> <see cref="char"/>s.</value> | ||
public int InitialPooledInstanceCapacity { get; set; } = new StringBuilderPooledObjectPolicy().InitialCapacity; | ||
|
||
/// <inheritdoc /> | ||
/// <value>Default is <c>4048</c> <see cref="char"/>s.</value> | ||
public int MaximumRetainedPooledInstanceCapacity { get; set; } = | ||
new StringBuilderPooledObjectPolicy().MaximumRetainedCapacity; | ||
|
||
private IHttpResponseFeature HttpResponseFeature => _features.Fetch(ref _features.Cache, f => null); | ||
|
||
private IServiceProvidersFeature ServiceProvidersFeature => _services.Fetch(ref _services.Cache, f => null); | ||
|
||
/// <inheritdoc /> | ||
public IResponseCookies Cookies | ||
{ | ||
get | ||
{ | ||
if (_cookiesCollection == null) | ||
{ | ||
var headers = HttpResponseFeature.Headers; | ||
_cookiesCollection = new ResponseCookies(headers); | ||
|
||
var serviceProvider = ServiceProvidersFeature.RequestServices; | ||
var provider = (ObjectPoolProvider)serviceProvider.GetService(typeof(ObjectPoolProvider)) ?? | ||
new DefaultObjectPoolProvider(); | ||
var pool = provider.CreateStringBuilderPool( | ||
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. Does this actually create a new pool per request? Or is it a GetOrCreate? How about if you call DefaultObjectPoolProvider? If so, how is this an improvement over creating a few StringBuilders per request? |
||
InitialPooledInstanceCapacity, | ||
MaximumRetainedPooledInstanceCapacity); | ||
|
||
_cookiesCollection = new ResponseCookies(headers, pool); | ||
} | ||
|
||
return _cookiesCollection; | ||
} | ||
} | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
👎 This is an implementation detail, not a generic feature property.