Skip to content

Share ticket serialization format logic across buffer and virtual paths - #70820

Open
halter73 wants to merge 2 commits into
dotnet:mainfrom
halter73:halter73-combined-ticket-serializer
Open

halter73 wants to merge 2 commits into
dotnet:mainfrom
halter73:halter73-combined-ticket-serializer

Conversation

@halter73

@halter73 halter73 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: b14765b213f1aec3cf8a9c2fe16b2880cb9189af

Dimension Assessment Rationale
Change confidence 7/10 ↑ Targeted serializer tests pass 22/22; implementation builds without warnings; shared format logic preserves virtual dispatch.
↑ Interleaved measurements show lower allocations and faster default paths than main.
↓ Derived paths are slower in the mixed-use harness, though derived-only timings overlap; derived serialization allocates 16 B/op more under default JIT settings.
↓ Malformed-input exception types match, but messages differ; the follow-up incomplete-UTF-8 fix remains local and is not included in this revision.

Share ticket serialization format logic across buffer and virtual paths

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Share ticket format logic while avoiding streams on the default paths.

Description

Combined replacement for #70165 (fast ticket deserialization) and #70172 (fast ticket serialization). Those PRs are referenced for context; neither branch is modified by this PR.

BinaryPayloadReader and BinaryPayloadWriter are internal ref structs that either adapt a caller-provided BinaryReader/BinaryWriter or operate directly on a payload buffer. Ticket and properties format logic is implemented once, rather than duplicated between optimized and legacy paths. The default serializers bypass streams; derived serializers retain the virtual reader/writer methods and nested identity/claim dispatch, including caller-provided encoding. Counts are written before contents as on main, without backpatching. No new public API or assembly-access mechanism is introduced.

Tests cover byte compatibility, overridden virtual methods, reader/writer encoding, null-argument parameter names, invalid UTF-8, malformed lengths and truncation. Malformed input preserves the tested exception types, not identical exception messages: the buffer reader uses parameterless exceptions instead of hand-written or interpolated English messages. Claim-string interning from #70165 is intentionally omitted. A local follow-up for incomplete UTF-8 compatibility is not included in this PR revision.

Measurements and tradeoffs

Baseline is main at d848fbf2e3. We built each PR's serializer changes against that same base, and compared them with this PR's published head using frozen Release binaries. The Identity-cookie-shaped ticket in the custom harness was warmed up and measured over six process runs per implementation in alternating order, with fixed four-core affinity and high process priority. Allocation uses GC.GetAllocatedBytesForCurrentThread; timing uses Stopwatch. These are exploratory custom-harness results, not BenchmarkDotNet results or performance guarantees.

Operation Main #70165 implementation #70172 implementation Combined PR
Default deserialize 3,592 B / 1,932 ns 2,288 B / 959 ns 3,592 B / 1,883 ns 2,896 B / 1,125 ns
Default serialize 2,680 B / 1,209 ns 2,680 B / 1,199 ns 768 B / 862 ns 728 B / 919 ns
Derived deserialize 3,592 B / 1,850 ns 3,592 B / 1,823 ns 3,592 B / 1,836 ns 3,592 B / 2,199 ns
Derived serialize 2,680 B / 1,266 ns 2,680 B / 1,246 ns 2,680 B / 1,229 ns 2,696 B / 1,428 ns

Default-path results retain the gains from both focused PRs, although #70165's hand-maintained claim-string cache reduces allocations further on deserialization. The combined PR omits that cache because it is a separate, read-only optimization with a manually maintained list; it could be evaluated independently.

Derived-path tradeoff: In the mixed-use harness, derived serialization and deserialization were slower for the combined implementation. The six-process median ranges were 1,720–2,113 ns on main vs. 2,092–2,618 ns combined for derived deserialize, and 1,203–1,319 ns vs. 1,372–1,584 ns for derived serialize. Derived serialization also allocated 16 B/op more. A separate derived-only run produced overlapping timings (deserialize 1,839 vs. 1,817 ns; serialize 1,237 vs. 1,262 ns), so the timing impact appears workload/JIT-profile dependent; its cause is not established. The focused PR implementations leave the legacy virtual path unchanged and did not show this combined-wrapper cost.

Limitations: a shared, loaded Windows VM; same-build timings varied, particularly for long-string cases; comparisons used a custom harness rather than BenchmarkDotNet. The PR variants were built by overlaying their serializer files on the common main base, not by benchmarking their live branches directly. Non-seekable streams were not separately tested. The full authentication suite was not run. The UTF-8 compatibility fix being explored locally has not been pushed or included in the measurements above.

Validation

After . .\activate.ps1, with $env:ASPNETCORE_TEST_LOG_MAXPATH='32767':

  • dotnet build src\Security\Authentication\Core\src\Microsoft.AspNetCore.Authentication.csproj -v q -nologo — passed, 0 warnings and 0 errors.
  • dotnet test src\Security\Authentication\test\Microsoft.AspNetCore.Authentication.Test.csproj --filter "FullyQualifiedName~TicketSerializerTests" -p:UseIisNativeAssets=false -nologo — passed 22/22, none skipped.
  • dotnet build src\Security\perf\Microbenchmarks\Microsoft.AspNetCore.Security.Microbenchmarks.csproj -c Release -p:UseIisNativeAssets=false — passed when preparing the baseline/candidate benchmark outputs.
  • git diff --check d848fbf2e3 HEAD — passed.

Related PRs: #70165 and #70172. This is a performance/consolidation change, not a bug-fix closing reference.

halter73 and others added 2 commits November 24, 2026 17:26
Combines the fast ticket deserialization (dotnet#70165) and serialization (dotnet#70172)
paths while implementing the ticket and properties formats only once.
BinaryPayloadReader/BinaryPayloadWriter either wrap a BinaryReader/BinaryWriter
or read/write a buffer directly, and nested identities and claims dispatch to
the protected virtuals when wrapping so derived serializers keep working.

Co-authored-by: Copilot App <[email protected]>
- Use parameterless exception constructors for malformed payloads so the
  framework-localized default messages are used without formatting.
- Exhaustively truncate a small representative ticket instead of the
  complex ticket containing a 20,000-character claim.
- Keep TicketSerializer members in main's order, with each private
  dispatcher and core next to its virtual method.
- Remove a duplicate benchmark ProjectReference and restore final newlines.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:03
@github-actions github-actions Bot added the area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation label Nov 25, 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.

🟡 Changes recommended

Long strings ending in incomplete UTF-8 sequences decode differently from BinaryReader.ReadString, causing Deserialize and Read to restore inconsistent values and requiring a compatibility fix with parity tests.

1 open issue
What changed in this PR

This PR consolidates authentication ticket and properties serialization logic, combining the buffer optimizations from #70165 and #70172 while retaining virtual dispatch for derived serializers.

Changes:

  • Adds buffer-backed readers and pooled writers to reduce default-path allocations.
  • Shares format logic between buffer and stream paths without adding public API.
  • Adds compatibility tests and benchmarks for default and derived serializers.
File Description
src/​Security/​perf/​Microbenchmarks/​TicketSerializerBenchmark.cs Adds ticket and properties benchmarks, including derived ticket serializers.
src/​Security/​Authentication/​test/​TicketSerializerTests.cs Adds wire-format, encoding, malformed-input, and override coverage.
src/​Security/​Authentication/​Core/​src/​TicketSerializer.cs Shares ticket format logic and introduces buffer fast paths.
src/​Security/​Authentication/​Core/​src/​PropertiesSerializer.cs Shares properties format logic and introduces buffer fast paths.
src/​Security/​Authentication/​Core/​src/​BinaryPayloadWriter.cs Adds pooled-buffer writing and a caller-provided writer adapter.
src/​Security/​Authentication/​Core/​src/​BinaryPayloadReader.cs Adds direct buffer reading and a caller-provided reader adapter.

Review effort: Balanced


public bool ReadBoolean() => _reader is not null ? _reader.ReadBoolean() : ReadByte() != 0;

public string ReadString() => _reader is not null ? _reader.ReadString() : Encoding.UTF8.GetString(ReadStringBytes());
@PureWeen

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-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants