Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

ResponseCookies.Append() allocates lots, big lots #561

Closed
dougbu opened this issue Feb 16, 2016 · 6 comments
Closed

ResponseCookies.Append() allocates lots, big lots #561

dougbu opened this issue Feb 16, 2016 · 6 comments
Assignees
Milestone

Comments

@dougbu
Copy link
Contributor

dougbu commented Feb 16, 2016

Despite using StringValuess, ResponseCookies.Append() is very allocation-heavy. Even just reusing the StringBuilder created in SetCookieHeaderValue.ToString() would be a significant improvement.

For example, running a scenario that adds an Antiforgery token to every response (~156 characters in the token in this case) allocates almost 1 KB extra per response. That's more than 2% of the allocations in this scenario.

Data for 3000 requests:
responsecookies append strings
responsecookies append chars

@muratg muratg added the Perf label Mar 14, 2016
@muratg
Copy link

muratg commented Mar 14, 2016

@dougbu Are you able to send a PR to reuse the StringBuilder?

@dougbu
Copy link
Contributor Author

dougbu commented Mar 14, 2016

Sure. I'll do that today.

@dougbu
Copy link
Contributor Author

dougbu commented Mar 14, 2016

Offline discussion w/ @Tratcher: Reusing a StringBuilder per ResponseCookies instance won't do much because few cookies are set per request. Will instead add an object pool of a small class containing (at least for now) just a StringBuilder instance.

@muratg
Copy link

muratg commented Mar 15, 2016

@dougbu That makes sense.

dougbu added a commit to aspnet/Hosting that referenced this issue Mar 15, 2016
- can now use pooled `StringBuilder`s to address aspnet/HttpAbstractions#561 and similar issues

nit: Fix incomplete test noted when adding new one
dougbu added a commit that referenced this issue Mar 17, 2016
…tures` package and namespace

- related to #561
- move required interfaces and classes down from `Microsoft.AspNetCore.Http.Abstractions`
- move everything in the `Microsoft.AspNetCore.Http.Features` into their own directory

nit: remove transient dependencies listed in `Microsoft.AspNetCore.Http.Abstractions`'s `project.json`
dougbu added a commit that referenced this issue Mar 18, 2016
…tures` package and namespace

- related to #561
- move required interfaces and classes down from `Microsoft.AspNetCore.Http.Abstractions`
- move everything in the `Microsoft.AspNetCore.Http.Features` into their own directory

nit: remove transient dependencies listed in `Microsoft.AspNetCore.Http.Abstractions`'s `project.json`
dougbu added a commit that referenced this issue Mar 20, 2016
… 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
dougbu added a commit that referenced this issue Mar 21, 2016
…tures` package and namespace

- #590, also related to #561
- move feature interfaces from `Microsoft.AspNetCore.Http` package
- move required classes from `Microsoft.AspNetCore.Http.Abstractions` package
- move `ISession` and `WebSocketAcceptContext` to `Microsoft.AspNetCore.Http` namespace (#590)

nit: remove transient dependencies listed in `Microsoft.AspNetCore.Http.Abstractions`'s `project.json`
@dougbu
Copy link
Contributor Author

dougbu commented Mar 21, 2016

For exposing the feature interfaces (no longer in .Internal namespaces):

dougbu added a commit that referenced this issue Mar 21, 2016
… 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
dougbu added a commit that referenced this issue Mar 21, 2016
… 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
dougbu added a commit to aspnet/Session that referenced this issue Mar 23, 2016
dougbu added a commit to aspnet/Security that referenced this issue Mar 23, 2016
dougbu added a commit that referenced this issue Mar 23, 2016
… 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
dougbu added a commit to aspnet/Hosting that referenced this issue Mar 23, 2016
- e.g. take advantage of aspnet/HttpAbstractions#561 fix wherever cookies are used
dougbu added a commit to aspnet/HttpSysServer that referenced this issue Mar 24, 2016
dougbu added a commit that referenced this issue Mar 25, 2016
… cookies

- #561
- new `SetCookieHeaderValue.AppendToStringBuilder()` method; avoids per-call `StringBuilder` allocation
- `ResponseCookies` uses `ObjectPool<StringBuilder>` that `ResponseCookiesFeature` provides
 - `ResponseCookies` works fine if no `ObjectPoolProvider` is available
- `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>`

nit: Add some doc comments
dougbu added a commit to aspnet/Hosting that referenced this issue Mar 25, 2016
- e.g. take advantage of aspnet/HttpAbstractions#561 fix wherever cookies are used
dougbu added a commit to aspnet/HttpSysServer that referenced this issue Mar 25, 2016
@dougbu
Copy link
Contributor Author

dougbu commented Mar 25, 2016

Part two for the actual fix:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants