Use pre-allocated static responses for fast-path bad request handling#65345
Open
adityamandaleeka wants to merge 1 commit intodotnet:mainfrom
Open
Use pre-allocated static responses for fast-path bad request handling#65345adityamandaleeka wants to merge 1 commit intodotnet:mainfrom
adityamandaleeka wants to merge 1 commit intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes bad request handling in Kestrel's HTTP/1.1 implementation by introducing a fast path that uses pre-allocated static response fragments. When a request is rejected before the application starts processing, the server now bypasses the normal response machinery (header serialization, lock acquisition, WriteSuffix) and writes static response bytes directly to the transport output.
Changes:
- Changed
_requestRejectedExceptionfield visibility from private to protected inHttpProtocolto allow access from derived classes - Added pre-allocated static response fragments for common HTTP error status codes (400, 408, 414, 431, 505)
- Implemented fast-path logic in
TryProduceInvalidRequestResponse()that uses static responses when appropriate - Added
GetStaticErrorResponsePrefix()method to map rejection reasons to corresponding static response prefixes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs | Changed _requestRejectedException field visibility from private to protected to enable access from Http1Connection |
| src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs | Added static response fragments and fast-path implementation for early bad request handling that writes pre-allocated responses directly to transport output |
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.
When a bad request is rejected before the app starts processing, bypass the normal response machinery (CreateResponseHeaders, header serialization, lock acquisition, WriteSuffix) and write static response fragments directly to the transport output.
The cached Date header from DateHeaderValueManager and the conditional Server header are inserted between the static prefix and suffix to maintain full HTTP compliance and response parity with the normal path.
This change adds another ~7% RPS improvement on the single-core bad request test in our perf lab. That's on top of the ~24% improvement done in #65256. The cumulative RPS improvement is ~30%.
We have pretty good test coverage already in this area and all the tests are passing.