-
Notifications
You must be signed in to change notification settings - Fork 193
Use pooled StringBuilder
to reduce allocations when adding response cookies
#594
Conversation
/// For example, the maximum retained capacity of <see cref="System.Text.StringBuilder"/> instances obtained | ||
/// from an object pool. | ||
/// </remarks> | ||
int MaximumRetainedPooledInstanceCapacity { get; set; } |
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.
Copying @Tratcher's comment from previous PR: 👎 This is an implementation detail, not a generic feature property.
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.
What do you suggest? This is at least abstracted from StringBuilder
and ObjectPool<T>
. I'm loath to expose an even-more-specific ObjectPool<StringBuilder>
property anywhere.
One thought: We could expose these properties only on the feature implementation once I fix #592. Then the required downcast would at least be to a public class.
b5b204c
to
1f2d8bb
Compare
🆙📅 |
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Http.Features.Internal; |
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.
👍 progress |
Much better, I'm not 100% comfortable with this yet. Trying to see how it scales when we add things. |
|
🆙📅 addressed @Tratcher's comments |
… cookies - #561 - new `SetCookieHeaderValue.AppendToStringBuilder()` method; avoids per-call `StringBuilder` allocation - `ResponseCookies` uses `ObjectPool<StringBuilder>` that `ResponseCookiesFeature` provides - `ResponseCookiesFeature` creates an `ObjectPoolProvider` instance if none in DI - `IResponseCookiesFeature` exposes `int` properties supporting middleware overrides of the pool policy nit: Add some doc comments
- `IHttpContextFactory` instance is a singleton instantiated from CI - make `HttpContextFactory` `ObjectPoolProvider` and `ResponseCookiesFeature`-aware - apply same pattern to sample `PooledHttpContextFactory` - pool is not currently configurable; defaults are fine for response cookies - if we need (policy) configuration, would add an `IOptions<HttpContextFactorySettings>` - `ResponseCookies` works fine if no `ObjectPoolProvider` is available
668719b
to
52cb1f9
Compare
@@ -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 comment
The 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 comment
The 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.
🆙📅 (rebased) ping @Tratcher @davidfowl |
🆙📅 (as requested, @davidfowl) |
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
- remove unused field - see pull aspnet/HttpAbstractions#594 and issue aspnet/HttpAbstractions#561
ResponseCookies.Append()
allocates lots, big lots #561SetCookieHeaderValue.AppendToStringBuilder()
method; avoids per-callStringBuilder
allocationResponseCookies
usesObjectPool<StringBuilder>
thatResponseCookiesFeature
providesResponseCookiesFeature
creates anObjectPoolProvider
instance if none in DIIResponseCookiesFeature
exposesint
properties supporting middleware overrides of the pool policynit: Add some doc comments