Repository navigation
Vectorize skipping of empty values in header list parsing - #70121
Merged
Merged
Conversation
HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex skipped consecutive empty
values (",,,,") one character at a time. Move that rare path out of line and
use IndexOfAnyExcept to skip separator/whitespace runs, keeping the common
path unchanged.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the header-parsing optimization.
No open issues 🎉
What changed in this PR
Optimizes shared HTTP header-list parsing by vectorizing the skipping of consecutive empty values while preserving whitespace and obsolete line-folding handling.
Changes:
- Uses
IndexOfAnyExceptto skip separator and whitespace runs, with non-inlined helpers to keep the common path small. - Adds tests for long empty-value runs, folded whitespace, and invalid CRLF sequences.
- Adds realistic header-parsing benchmarks to check for performance regressions.
| File | Description |
|---|---|
src/Shared/HttpRuleParser.cs |
Adds vectorized separator and whitespace scanning with line-folding support. |
src/Http/Http/perf/Microbenchmarks/HeaderParsingBenchmarks.cs |
Adds Accept, Accept-Encoding, and Accept-Language benchmarks. |
src/Http/Headers/test/EntityTagHeaderValueTest.cs |
Tests long empty-value runs and valid versus invalid folding. |
src/Http/Headers/src/HeaderUtilities.cs |
Routes consecutive empty values through a non-inlined helper. |
Review effort: Balanced
Member
|
/review |
BrennanConroy
approved these changes
Nov 2, 2026
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:23 — with
GitHub Actions
Inactive
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:24 — with
GitHub Actions
Inactive
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:25 — with
GitHub Actions
Inactive
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:26 — with
GitHub Actions
Inactive
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:28 — with
GitHub Actions
Inactive
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:29 — with
GitHub Actions
Inactive
Contributor
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% Confident that no docs are needed: this is an internal performance refactor with tests and benchmarks only. Documentation surfaces considered
|
BrennanConroy
temporarily deployed
to
copilot-pat-pool
November 2, 2026 19:30 — with
GitHub Actions
Inactive
This branch was previously deployed
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.
Skip runs of empty header list values with a vectorized search
Vectorize skipping of empty values (
a,,,,b) in header list parsingDescription
HeaderUtilities.GetNextNonEmptyOrWhitespaceIndexis used by everyParseList/ParseStrictList(Accept,Accept-Encoding,If-None-Match,Cache-Control, ...). It skipped runs of consecutive empty values one character at a time, callingGetWhitespaceLengthafter each comma. Long comma/whitespace runs therefore cost far more per byte than real values.Changes:
[NoInlining]SkipEmptyValueshelper, so the hot path stays the same size. Inlining the loop growth regressed realistic headers in my earlier attempts.HttpRuleParser.GetSeparatorsAndWhitespaceLengthskips,/SP/HT runs withIndexOfAnyExcept. It still honours obsolete line folding (CRLF + SP/HT) through the existingGetWhitespaceLength, so accepted and rejected inputs are unchanged.The new tests in
EntityTagHeaderValueTestcover long runs of empty values (plain, spaces/tabs, obs-fold), plus CR without folding (which must fail). They pass both before and after the change.Benchmarks
The
EntityTagSeparatorsandMediaTypeMissingSlashcases are the existingHeaderParserListBenchmarks. The newHeaderParsingBenchmarksadds realisticAccept,Accept-EncodingandAccept-Languagevalues to check for regressions.Setup: local BenchmarkDotNet with MemoryDiagnoser on a 2-vCPU Linux VM, run as 3 interleaved base/candidate rounds with the same config.
Realistic headers are neutral within noise. Comma-run input, which an attacker controls, is up to ~70x cheaper.
To reproduce:
dotnet run -c Release --filter '*HeaderParsingBenchmarks*' '*HeaderParserListBenchmarks*'fromsrc/Http/Http/perf/Microbenchmarks.Tests
Microsoft.Net.Http.Headers.Tests: 1825 passed, 0 failed.There is no existing issue; this came out of a perf survey of the request parsing paths.