Reuse buffer for encoding headers instead of allocating one per request#65
Draft
raneashay wants to merge 1 commit intomicrosoft:mainfrom
Draft
Reuse buffer for encoding headers instead of allocating one per request#65raneashay wants to merge 1 commit intomicrosoft:mainfrom
raneashay wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Prior to this patch, every HTTP request created a new 16KB buffer for encoding the header, which are typically only a few hundred bytes long. Consequently, this increased pressure on the garbage collector when lots of requests streamed in. This patch instead makes the header encoder reuse the header encoder buffer. The caveat, however, is that the downstream consumers of the header are asynchronous, so the encoder needs to take special care to ensure that it doesn't modify or invalidate the buffer after it hands the buffer over to the downstream asynchronous pipeline. To resolve this, this patch snapshots the buffer data into compact copies sized to the actual encoded length. The cached buffer is then immediately available for reuse via `clear()` and `limit()`. For typical requests, this reduces per-request allocation from ~16KB to a few hundred bytes (i.e. the size of the compact copy of the encoded headers), with the 16KB encoding buffer allocated once per connection instead of once per request.
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.
Prior to this patch, every HTTP request created a new 16KB buffer for
encoding the header, which are typically only a few hundred bytes long.
Consequently, this increased pressure on the garbage collector when lots
of requests streamed in. This patch instead makes the header encoder
reuse the header encoder buffer.
The caveat, however, is that the downstream consumers of the header are
asynchronous, so the encoder needs to take special care to ensure that
it doesn't modify or invalidate the buffer after it hands the buffer
over to the downstream asynchronous pipeline. To resolve this, this
patch snapshots the buffer data into compact copies sized to the actual
encoded length. The cached buffer is then immediately available for
reuse via
clear()andlimit().For typical requests, this reduces per-request allocation from ~16KB to
a few hundred bytes (i.e. the size of the compact copy of the encoded
headers), with the 16KB encoding buffer allocated once per connection
instead of once per request.