From f679062987cbc31e4ad146e1b66e0c260d538380 Mon Sep 17 00:00:00 2001 From: davidfowl <95136+davidfowl@users.noreply.github.com> Date: Fri, 30 Oct 2026 19:01:42 +0000 Subject: [PATCH] Skip runs of empty list values in header parsing with vectorized search 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> --- src/Http/Headers/src/HeaderUtilities.cs | 18 +++++++---- .../Headers/test/EntityTagHeaderValueTest.cs | 17 +++++++++++ .../HeaderParsingBenchmarks.cs | 23 ++++++++++++++ src/Shared/HttpRuleParser.cs | 30 +++++++++++++++++++ 4 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/Http/Http/perf/Microbenchmarks/HeaderParsingBenchmarks.cs diff --git a/src/Http/Headers/src/HeaderUtilities.cs b/src/Http/Headers/src/HeaderUtilities.cs index 2e6439e00944..5a7469a80c05 100644 --- a/src/Http/Headers/src/HeaderUtilities.cs +++ b/src/Http/Headers/src/HeaderUtilities.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Globalization; +using System.Runtime.CompilerServices; using Microsoft.Extensions.Primitives; namespace Microsoft.Net.Http.Headers; @@ -169,18 +170,23 @@ internal static int GetNextNonEmptyOrWhitespaceIndex( current++; // skip delimiter. current = current + HttpRuleParser.GetWhitespaceLength(input, current); - if (skipEmptyValues) + if (skipEmptyValues && (current < input.Length) && (input[current] == ',')) { - while ((current < input.Length) && (input[current] == ',')) - { - current++; // skip delimiter. - current = current + HttpRuleParser.GetWhitespaceLength(input, current); - } + current = SkipEmptyValues(input, current); } return current; } + // Rare: one or more consecutive empty values (e.g. "a,,b"). Kept out of line so that + // GetNextNonEmptyOrWhitespaceIndex stays small; long runs are skipped vectorized. + [MethodImpl(MethodImplOptions.NoInlining)] + private static int SkipEmptyValues(StringSegment input, int current) + { + Debug.Assert(input[current] == ','); + return current + HttpRuleParser.GetSeparatorsAndWhitespaceLength(input, current, ','); + } + private static int AdvanceCacheDirectiveIndex(int current, string headerValue) { // Skip until the next potential name diff --git a/src/Http/Headers/test/EntityTagHeaderValueTest.cs b/src/Http/Headers/test/EntityTagHeaderValueTest.cs index a2aab6639d09..21f1e0f70ead 100644 --- a/src/Http/Headers/test/EntityTagHeaderValueTest.cs +++ b/src/Http/Headers/test/EntityTagHeaderValueTest.cs @@ -258,6 +258,23 @@ public void ParseList_NullOrEmptyArray_ReturnsEmptyList() Assert.Empty(result); } + [Theory] + [InlineData(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,")] + [InlineData(", , , ,\t,\t , ,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,")] + [InlineData(",,,,\r\n ,,,,\r\n\t,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,")] + public void TryParseStrictList_LongRunsOfEmptyValues_Skipped(string separators) + { + var input = "\"a\"" + separators + "\"b\"" + separators; + Assert.True(EntityTagHeaderValue.TryParseStrictList(new[] { input }, out var results)); + Assert.Equal(new[] { new EntityTagHeaderValue("\"a\""), new EntityTagHeaderValue("\"b\"") }, results); + } + + [Fact] + public void TryParseStrictList_EmptyValuesFollowedByCRWithoutFolding_Fails() + { + Assert.False(EntityTagHeaderValue.TryParseStrictList(new[] { "\"a\",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n\"b\"" }, out _)); + } + [Fact] public void TryParseList_NullOrEmptyArray_ReturnsFalse() { diff --git a/src/Http/Http/perf/Microbenchmarks/HeaderParsingBenchmarks.cs b/src/Http/Http/perf/Microbenchmarks/HeaderParsingBenchmarks.cs new file mode 100644 index 000000000000..d03be87e5404 --- /dev/null +++ b/src/Http/Http/perf/Microbenchmarks/HeaderParsingBenchmarks.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using BenchmarkDotNet.Attributes; +using Microsoft.Net.Http.Headers; + +namespace Microsoft.AspNetCore.Http; + +public class HeaderParsingBenchmarks +{ + private readonly string[] _accept = ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"]; + private readonly string[] _acceptEncoding = ["gzip, deflate, br, zstd"]; + private readonly string[] _acceptLanguage = ["en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7"]; + + [Benchmark] + public int ParseAccept() => MediaTypeHeaderValue.ParseList(_accept).Count; + + [Benchmark] + public int ParseAcceptEncoding() => StringWithQualityHeaderValue.ParseList(_acceptEncoding).Count; + + [Benchmark] + public int ParseAcceptLanguage() => StringWithQualityHeaderValue.ParseList(_acceptLanguage).Count; +} diff --git a/src/Shared/HttpRuleParser.cs b/src/Shared/HttpRuleParser.cs index a88adeca4e6f..45888c601ce0 100644 --- a/src/Shared/HttpRuleParser.cs +++ b/src/Shared/HttpRuleParser.cs @@ -4,6 +4,7 @@ using System.Buffers; using System.Diagnostics.Contracts; using System.Globalization; +using System.Runtime.CompilerServices; using System.Text; using Microsoft.Extensions.Primitives; @@ -111,6 +112,35 @@ internal static int GetWhitespaceLength(StringSegment input, int startIndex) return input.Length - startIndex; } + // Returns the length of the run of separator characters and LWS at the start of input. + // Used to skip long runs of empty list elements (e.g. ",,,,") without a per-char loop. + [MethodImpl(MethodImplOptions.NoInlining)] + internal static int GetSeparatorsAndWhitespaceLength(StringSegment input, int startIndex, char separator) + { + var current = startIndex; + while (current < input.Length) + { + var index = input.AsSpan(current).IndexOfAnyExcept(separator, SP, Tab); + if (index < 0) + { + return input.Length - startIndex; + } + + current += index; + + // Handle obsolete line folding (CRLF followed by SP or HT). + var whitespaceLength = GetWhitespaceLength(input, current); + if (whitespaceLength == 0) + { + break; + } + + current += whitespaceLength; + } + + return current - startIndex; + } + internal static int GetNumberLength(StringSegment input, int startIndex, bool allowDecimal) { Contract.Requires((startIndex >= 0) && (startIndex < input.Length));