Pool ResponseBufferingStream in HttpLoggingMiddleware#65147
Merged
Conversation
- Add parameterless constructor to ResponseBufferingStream for pooling - Add Initialize() method to set per-request state - Add ResetForPool() method to clear state when returning to pool - Create ResponseBufferingStreamPooledObjectPolicy for the pool - Update HttpLoggingMiddleware to inject and use the pool - Register ObjectPool<ResponseBufferingStream> in DI This reduces object allocations per request when response body logging is enabled or interceptors are present, reducing GC pressure on high-traffic servers.
Updated HttpLoggingMiddlewareTests to use the new constructor signature: - Added ObjectPool<ResponseBufferingStream> parameter to CreateMiddleware helper - Updated Ctor_ThrowsExceptionsWhenNullArgs test to include pool in all cases - Added new test case for null responseBufferingStreamPool argument
ObjectPool.Create<T>() requires a public parameterless constructor. ResponseBufferingStream has an internal constructor for pooling, so we need to use DefaultObjectPoolProvider.Create() with our policy instead. This fixes the build errors in both the source and test projects.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces object pooling for ResponseBufferingStream to reduce heap allocations when response body logging or interceptors are enabled.
Changes:
- Added
ResponseBufferingStreamPooledObjectPolicyto manage pooled stream lifecycle - Modified
ResponseBufferingStreamto support pooling with parameterless constructor,Initialize(), andResetForPool()methods - Updated
HttpLoggingMiddlewareto use object pool for getting/returning streams instead of creating/disposing them - Registered the stream pool in the DI container
- Updated all test constructors to include the new pool parameter
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
ResponseBufferingStreamPooledObjectPolicy.cs |
New pooling policy that creates streams and resets them when returned to pool |
ResponseBufferingStream.cs |
Added pooling support with initialization and reset methods to enable reuse |
HttpLoggingMiddleware.cs |
Replaced stream instantiation/disposal with pool Get/Return pattern |
HttpLoggingServicesExtensions.cs |
Registered ResponseBufferingStream pool as singleton in DI container |
HttpLoggingMiddlewareTests.cs |
Updated test middleware constructors to include new pool parameter |
DeagleGross
approved these changes
Jan 26, 2026
Member
DeagleGross
left a comment
There was a problem hiding this comment.
Thanks for doing this, Aditya!
I think it would be beneficial to have benchmarks added to see how much do we save; but for such a change probably not needed.
BrennanConroy
approved these changes
Feb 3, 2026
Member
BrennanConroy
left a comment
There was a problem hiding this comment.
nit: don't need to reset the logger
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

The
ResponseBufferingStreamwas being instantiated on every request where response body logging is enabled or interceptors are present. This change uses object pooling to reduce those allocations.