Skip to content

Use pre-allocated static responses for fast-path bad request handling#65345

Open
adityamandaleeka wants to merge 1 commit intodotnet:mainfrom
adityamandaleeka:prealloc
Open

Use pre-allocated static responses for fast-path bad request handling#65345
adityamandaleeka wants to merge 1 commit intodotnet:mainfrom
adityamandaleeka:prealloc

Conversation

@adityamandaleeka
Copy link
Member

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.

Copilot AI review requested due to automatic review settings February 6, 2026 06:02
@github-actions github-actions bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Feb 6, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _requestRejectedException field visibility from private to protected in HttpProtocol to 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Perf

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants