Skip to content

Kestrel: vectorize query string end search in HTTP/1.1 request line parsing - #70162

Open
davidfowl wants to merge 3 commits into
mainfrom
perf/kestrel-querystring-indexof
Open

davidfowl wants to merge 3 commits into
mainfrom
perf/kestrel-querystring-indexof

Conversation

@davidfowl

Copy link
Copy Markdown
Member

HttpParser.TryParseRequestLine finds the end of the query string with a scalar byte-by-byte loop that looks for the space before the HTTP version. This PR replaces that loop with ReadOnlySpan<byte>.IndexOf(ByteSpace), which is vectorized. The resulting offset is identical: the index of the first space, or requestLine.Length if there is none. The path portion before it already uses IndexOfAny.

Benchmarks

Local BenchmarkDotNet (InProcess, MemoryDiagnoser), 2 vCPU VM, 4 interleaved base/PR rounds. Two benchmarks are new in HttpParserBenchmark:

  • QueryString: /api/products/search?q=...&page=2&pageSize=24, a 121-byte query
  • ShortQueryString: /api/items?id=42
Benchmark main (rounds) PR (rounds) Δ
QueryString 85.6 / 88.2 / 89.6 / 87.2 ns 54.4 / 56.1 / 55.6 / 54.9 ns -37%
ShortQueryString 50.4 / 49.1 / 48.7 / 53.1 ns 50.3 / 50.5 / 50.6 / 53.0 ns noise

Existing HttpParserBenchmark cases (PlaintextTechEmpower, JsonTechEmpower, LiveAspNet, Unicode, MultispanUnicodeHeader) have no query string and are unchanged within noise over 3 rounds. No allocations in any case.

Tests

Microsoft.AspNetCore.Server.Kestrel.Core.Tests filtered to HttpParserTests, Http1ConnectionTests, StartLine, HttpRequestLine and Target: 7194 passed, 0 failed.

No API / behavior change

  • Internal implementation only. No PublicAPI.*.txt changes and no new APIs.
  • Parsed path, query and error behavior are identical.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

…'s request line parser

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 01:10
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Oct 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The frozen review bundle, prior feedback, and independent guide reviews were unavailable, preventing completion of the required review process and verification of revision provenance.

No open issues 🎉

What changed in this PR

Optimizes Kestrel’s HTTP/1.1 request-line parsing by replacing the scalar query-string terminator search with a vectorized span search, preserving the resulting offset.

Changes:

  • Uses ReadOnlySpan<byte>.IndexOf(ByteSpace) to locate the query’s ending space.
  • Adds long- and short-query benchmarks to measure performance.
File Description
src/​Servers/​Kestrel/​perf/​Microbenchmarks/​HttpParserBenchmark.cs Adds long- and short-query request parsing benchmarks.
src/​Servers/​Kestrel/​Core/​src/​Internal/​Http/​HttpParser.cs Replaces the query scan loop with IndexOf, retaining the no-space fallback.

Review effort: Balanced

…tionTest)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Member Author

Pushed an empty commit to retrigger CI. Both failures look unrelated to this change, which only affects how the HTTP/1.1 request-line parser finds the end of the query string:

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

…_Async)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Member Author

On the retriggered run, aspnetcore-ci (build plus all Helix tests) is green, including the Http2 GOAWAY test. The only failure was Blazor E2E (Mono server): ServerVirtualizationTest.CancelsOutdatedRefreshes_Async. That test has a long flakiness history: it was quarantined in #66120/#66123, re-quarantined in #68016, and stabilization was attempted in #68079 and #32869. It's unrelated to Kestrel's HTTP/1.1 request-line parsing, so I pushed another empty commit to retrigger.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants