Skip to content

feat: target .NET 11 and speed up parsing with SearchValues - #103

Merged
BenjaminAbt merged 5 commits into
mainfrom
feat/net11-searchvalues
Nov 22, 2026
Merged

BenjaminAbt merged 5 commits into
mainfrom
feat/net11-searchvalues

Conversation

@BenjaminAbt

Copy link
Copy Markdown
Member

Summary

Adds .NET 11 as a target framework and makes uncached parsing 26–58% faster. Robot and mobile matching now use SearchValues<string>, and the hand-written SIMD code for version extraction is replaced with framework span APIs. The public API and parsing results are unchanged.

Performance

Uncached Parse, current code against the parser from 36ec145. Both ran on .NET 11.0.0 x64 (AMD Ryzen 9 9950X):

User agent Before After Change
Chrome on Windows 723.10 ns 300.91 ns -58%
Firefox on Linux 569.95 ns 360.02 ns -37%
Chrome on Android 691.54 ns 466.74 ns -33%
Safari on iPhone 740.59 ns 441.33 ns -40%
Applebot 465.22 ns 212.52 ns -54%
Googlebot 9.87 ns 8.45 ns within error
Unknown client 632.35 ns 467.71 ns -26%

Allocations are unchanged (0–40 B per parse). The README describes how to reproduce this.

Changes

Parser (src/HttpUserAgentParser)

  • Robots and mobile devices (net9.0+): tokens are grouped by eight into SearchValues<string> with OrdinalIgnoreCase. A group is only checked token by token when its pre-filter matches, so rule priority stays the same.
  • Mutable Robots array: HttpUserAgentStatics.Robots is public and writable. Entries a caller replaces at runtime are still checked in their original position. GetRobot_UsesReplacementRuleInOriginalOrder covers this.
  • Version extraction: IndexOfAnyInRange('0', '9') plus IndexOfAnyExcept(SearchValues "0123456789.") replace the Vector128/Vector256 code. VectorExtensions.cs is removed.
  • net8.0: robot and mobile matching keep the previous loop. Only version extraction changes.

Bug fix

On ARM64 under net8.0/net9.0, the old vector path narrowed chars with AdvSimd.Arm64.UnzipEven and kept only the low byte. It ran once the text after the browser token was at least 16 chars long. A non-ASCII char whose low byte is a digit, such as ı (U+0131 → 0x31), was then treated as part of the version. For example, Chrome/1.2ı3 followed by a long suffix returned 1.2ı3 instead of 1.2. x64 and net10.0+ used saturating narrowing and were not affected. This bug was found by reading the code and was not reproduced on ARM64 hardware. GetBrowser_ExtractsAsciiVersion now pins the expected result on every platform.

Build and dependencies

  • Target frameworks: net8.0;net9.0;net10.0;net11.0. The SDK is pinned to 11.0.100 and LangVersion to 15.0 instead of preview.
  • Microsoft.Extensions.* versions are now set per target framework (see Compatibility).
  • On net11.0 the DI and caching abstractions are part of the shared framework. NuGet would prune them (NU1510), so the direct references are dropped there.
  • Tests use xunit.v3.mtp-off, because coverlet.msbuild runs through VSTest. Coverage is written per target framework.
  • Updated: Roslynator 5.0.1, Meziantou.Analyzer 3.0.341, Roslyn 5.12.0, Microsoft.NET.Test.Sdk 18.12.0, NSubstitute 6.2.0, coverlet 10.1.0.
  • Removed: BenchmarkDotNet.Diagnostics.Windows and NaughtyStrings (no longer used).

CI

  • The SDK requirement makes a per-runtime matrix impossible, so PR validation runs as one job with the 8, 9, 10 and 11 runtimes. dotnet test covers every target framework.
  • TRX files are named per test run (LogFilePrefix) so they no longer overwrite each other.
  • The release workflow only pushes packages, so it only needs the .NET 11 SDK.

Benchmarks

  • HttpUserAgentParserBenchmarks measures one parse per user agent, with current browser and crawler user agents as parameters.
  • BenchmarkBaselineAssembly=<path> adds a baseline benchmark against an older build through an extern alias.
  • LibraryComparisonBenchmarks runs in-process. BenchmarkDotNet 0.15.8 cannot build out-of-process jobs for .NET 11 (GetRuntimeVersion not implemented for NotRecognized), so just bench crashed there.

Compatibility

  • Public API: unchanged.
  • Parsing results: unchanged on x64. A differential run of old against new over 21,276 user agents found 0 differences in type, name, version, mobile device and platform. The corpus included all test strings, every robot and mobile key in several spellings, browser and version edge cases (128-char window, non-ASCII digits, ı/İ/ſ/Kelvin sign) and 20,000 random combinations.
  • Package dependencies (please review): on net9.0 and net10.0 the packages now require Microsoft.Extensions.* 9.0.23 and 10.0.15. Until now every target framework referenced the 8.0.x versions. net8.0 stays on 8.0.x. On net11.0, MyCSharp.HttpUserAgentParser has no NuGet dependencies, and MyCSharp.HttpUserAgentParser.MemoryCache only depends on Microsoft.Extensions.Caching.Memory 11.0.0.

Verification

  • dotnet build -c Release: 0 warnings, 0 errors
  • dotnet test -c Release: all tests pass on net8.0, net9.0, net10.0 and net11.0 (214 / 13 / 10 per framework)
  • Differential test old vs. new: 0 differences in 21,276 user agents
  • dotnet pack: dependency groups per target framework checked in the nuspec
  • Benchmarks run on .NET 11; the README is updated

Notes for reviewers

  • dotnet format --verify-no-changes reports findings in HttpContextExtensionsTests.cs and HttpUserAgentParserMemoryCacheTelemetryTests.cs. Both already existed before this PR, and this PR doesn't touch those files.
  • .NET 8 and .NET 9 reached end of support on 10 November 2026. They are still targeted here. Dropping them would be a separate decision.
  • The library comparison numbers come from a ShortRun with 3 iterations and are only meant as a rough picture. The uncached comparison above uses 12 iterations.

- Add net11.0 to the target frameworks and pin the .NET 11 SDK (11.0.100).
- Pin LangVersion to C# 15 instead of preview.
- Resolve Microsoft.Extensions.* per target framework (8.0.x, 9.0.23,
  10.0.15, 11.0.0). On net11.0 the DI and caching abstractions ship with
  the shared framework, so the direct references are dropped there (NU1510).
- Switch tests to xunit.v3.mtp-off, because coverlet.msbuild runs through
  VSTest, and write coverage per target framework.
- Update test packages and analyzers (Roslynator 5, Meziantou 3, Roslyn 5.12).
- Use StringComparer.Ordinal in telemetry assertions (new analyzer rule).
- Remove BenchmarkDotNet.Diagnostics.Windows and NaughtyStrings, which are no
  longer referenced.
The solution now requires the .NET 11 SDK, so a per-runtime matrix can no
longer build it with an older SDK. Install the 8, 9, 10 and 11 runtimes in one
job and run the tests for every target framework there. Name the TRX files per
test run so they no longer overwrite each other.

The release workflow only pushes packages, so it only needs the .NET 11 SDK.
- GetRobot and GetMobileDevice (net9.0+) pre-filter groups of eight tokens
  with SearchValues<string> (OrdinalIgnoreCase) and only run the per-token
  check for groups that can match. Rule priority is preserved.
- HttpUserAgentStatics.Robots is a public, mutable array. Entries a caller
  replaces at runtime are still checked in their original position.
- TryExtractVersion uses IndexOfAnyInRange / IndexOfAnyExcept instead of the
  hand-written Vector128/Vector256 code. This also fixes the ARM64 path on
  net8.0/net9.0, which read only the low byte of each char, so a character
  such as U+0131 ('ı') was taken as the digit '1'.
- Remove VectorExtensions, which is no longer used.

The public API is unchanged. A differential run against the previous
implementation over 21,276 user agents showed no differences.
- HttpUserAgentParserBenchmarks measures one parse per user agent, with
  current browser, crawler and unknown user agents as parameters.
- Setting BenchmarkBaselineAssembly to a previous build of the parser adds a
  baseline benchmark through an extern alias.
- LibraryComparisonBenchmarks runs its ShortRun job in-process, because
  BenchmarkDotNet 0.15.8 cannot build out-of-process jobs for .NET 11
  ("GetRuntimeVersion not implemented for NotRecognized").
- State the supported target frameworks and the required SDK.
- Add the uncached parser comparison against the previous implementation and
  how to reproduce it.
- Replace the .NET 10 library comparison with a .NET 11 run and a comparison
  table of the MyCSharp results.
@BenjaminAbt
BenjaminAbt requested a review from gfoidl November 21, 2026 13:29

@gfoidl gfoidl 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.

👍🏻 It's good to throw away the custom vectorized code and to rely on the BCL.

Although at the same time it's a pity because so lovely code is going away 😉.

@BenjaminAbt
BenjaminAbt merged commit be56445 into main Nov 22, 2026
1 check passed
@BenjaminAbt
BenjaminAbt deleted the feat/net11-searchvalues branch November 22, 2026 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants