Skip to content

Reuse buffer for encoding headers instead of allocating one per request#65

Draft
raneashay wants to merge 1 commit intomicrosoft:mainfrom
raneashay:ashay/reuse-http-header-buffer
Draft

Reuse buffer for encoding headers instead of allocating one per request#65
raneashay wants to merge 1 commit intomicrosoft:mainfrom
raneashay:ashay/reuse-http-header-buffer

Conversation

@raneashay
Copy link
Copy Markdown

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant