close
Skip to content

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

Merged
adityamandaleeka merged 2 commits into
dotnet:mainfrom
adityamandaleeka:prealloc
Feb 11, 2026
Merged

Use pre-allocated static responses for fast-path bad request handling#65345
adityamandaleeka merged 2 commits into
dotnet:mainfrom
adityamandaleeka:prealloc

Conversation

@adityamandaleeka
Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs Outdated
Comment thread src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs
Comment thread src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs
@adityamandaleeka adityamandaleeka enabled auto-merge (squash) February 11, 2026 00:25
@adityamandaleeka adityamandaleeka merged commit 1b762e4 into dotnet:main Feb 11, 2026
25 checks passed
@dotnet-policy-service dotnet-policy-service Bot added this to the 11.0-preview2 milestone Feb 11, 2026
@wtgodbe wtgodbe modified the milestones: 11.0-preview2, 11.0-preview3 Feb 27, 2026
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.

5 participants