-
Notifications
You must be signed in to change notification settings - Fork 191
Use pooled StringBuilder
to reduce allocations when adding response cookies
#594
Changes from all commits
4d4b7b7
7e93722
f7350bf
52cb1f9
47090f5
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 |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
"System.Net.WebSockets": "4.0.0-*", | ||
"System.Runtime.Extensions": "4.1.0-*", | ||
"System.Security.Claims": "4.0.1-*", | ||
"System.Security.Cryptography.X509Certificates": "4.1.0-*", | ||
"System.Security.Cryptography.X509Certificates": "4.0.0-*", | ||
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. Not sure this will be necessary next time CI updates the feed. @pranavkm? 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. Yup, the 4.1.0 package is available in the new corefx build. |
||
"System.Security.Principal": "4.0.1-*" | ||
}, | ||
"imports": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,51 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Http.Features.Internal; | ||
using Microsoft.Extensions.ObjectPool; | ||
|
||
namespace Microsoft.AspNetCore.Http.Internal | ||
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. Wasn't the plan to move the Features out of Internal namespace? 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. |
||
{ | ||
public class HttpContextFactory : IHttpContextFactory | ||
{ | ||
private IHttpContextAccessor _httpContextAccessor; | ||
private readonly ObjectPool<StringBuilder> _builderPool; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public HttpContextFactory() : this(httpContextAccessor: null) | ||
public HttpContextFactory(ObjectPoolProvider poolProvider) | ||
: this(poolProvider, httpContextAccessor: null) | ||
{ | ||
} | ||
|
||
public HttpContextFactory(IHttpContextAccessor httpContextAccessor) | ||
public HttpContextFactory(ObjectPoolProvider poolProvider, IHttpContextAccessor httpContextAccessor) | ||
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. Not testing constructor overloads w/ a non- |
||
{ | ||
if (poolProvider == null) | ||
{ | ||
throw new ArgumentNullException(nameof(poolProvider)); | ||
} | ||
|
||
_builderPool = poolProvider.CreateStringBuilderPool(); | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public HttpContext Create(IFeatureCollection featureCollection) | ||
{ | ||
if (featureCollection == null) | ||
{ | ||
throw new ArgumentNullException(nameof(featureCollection)); | ||
} | ||
|
||
var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool); | ||
featureCollection.Set<IResponseCookiesFeature>(responseCookiesFeature); | ||
|
||
var httpContext = new DefaultHttpContext(featureCollection); | ||
if (_httpContextAccessor != null) | ||
{ | ||
_httpContextAccessor.HttpContext = httpContext; | ||
} | ||
|
||
return httpContext; | ||
} | ||
|
||
|
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.
Won't need this addition once I'm done #592.