Repository navigation
Conversation
When TicketSerializer/PropertiesSerializer are not subclassed, write the payload directly into a private pooled buffer (cleared before return) and copy it once into the result. Byte-for-byte identical to the BinaryWriter output; claim/identity counts are patched in after a single enumeration. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The inserted overload leaves Read(BinaryReader) without its inherited documentation, introducing a CS1591 error that fails CI builds.
1 open issue
What changed in this PR
Reduces write-side allocations for authentication tickets and properties, complementing the separate deserialization optimization in #70165.
Changes:
- Adds a pooled binary writer and base-type serialization fast paths while preserving virtual methods for subclasses.
- Writes identity and claim counts after a single enumeration.
- Adds byte-compatibility tests and serialization microbenchmarks.
| File | Description |
|---|---|
src/Security/perf/Microbenchmarks/TicketSerializerWriteBenchmark.cs |
Adds ticket and properties serialization benchmarks. |
src/Security/perf/Microbenchmarks/Microsoft.AspNetCore.Security.Microbenchmarks.csproj |
References the authentication project for benchmarks. |
src/Security/Authentication/test/TicketSerializerWriteTests.cs |
Tests byte compatibility, round trips, and edge cases. |
src/Security/Authentication/Core/src/TicketSerializer.cs |
Adds pooled serialization and single-pass counting. |
src/Security/Authentication/Core/src/PropertiesSerializer.cs |
Adds a pooled serialization fast path. |
src/Security/Authentication/Core/src/BinaryPayloadWriter.cs |
Implements binary encoding, buffer growth, and pooled-buffer cleanup. |
Review effort: Balanced
Co-authored-by: Copilot <[email protected]>
…ation, #69494) Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit, 4a76c30. The only failure was This PR's serialized output is byte-identical to 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
# Conflicts: # src/Security/perf/Microbenchmarks/Microsoft.AspNetCore.Security.Microbenchmarks.csproj
Co-authored-by: Copilot <[email protected]>
|
The Failing test: Why it's unrelated
I didn't retrigger CI because main E2E is currently flaky. A maintainer can rerun the failed job if needed. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Merge assessmentVerdict: Merge after an Authentication owner accepts the second copy of the ticket writer and
Reasons to merge
Reasons not to merge yet
What would change this verdict: an owner preferring a single writer path would make this a "Don't merge" in its current shape. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
I opened #70820 as a combined alternative to this and #70165. It keeps buffer-based read and write paths for In a head-to-head comparison built against the same main commit, #70820 matches this PR's serialization gains while also optimizing deserialization. For a typical Identity cookie ticket:
Full measurements and limitations are in #70820. Its reader also needs a compatibility fix for incomplete UTF-8 at the end of long strings (also present in #70165, not in this write-only PR); I have a fix with regression tests ready. I think the shared implementation is the better direction. |

Reduce allocations when serializing authentication tickets
Reduce allocations when serializing authentication tickets and properties.
Description
Motivation:
TicketSerializer.Serializeruns on every cookie sign-in and on every sliding-expiration orShouldRenewrefresh of the cookie.PropertiesSerializer.Serializeruns for the OAuth/OIDCstateon every challenge. Both write through aMemoryStreamand aBinaryWriter, then copy the result withToArray(). That means aBinaryWriter, its internal buffers, severalMemoryStreamgrowth arrays and the final copy.identity.Claimsis also enumerated twice, once forCount()and once for writing.This is the write-side counterpart of #70165 (the read side). The two PRs are independent; this one touches only the
Serialize/Writecode paths.Change (internal only, no public API or behavior change):
BinaryPayloadWriterref struct. It writes the same format asBinaryWriter(Stream):Int32and 1-byte booleans;Encoding.UTF8, using the same replacement fallback for invalid surrogates.ArrayPool<byte>.Shared. It never leaves the writer, and the used portion is cleared before being returned to the pool, so claim data can't linger in shared buffers. The result is copied once into an exact-sizebyte[].TicketSerializer.SerializeandPropertiesSerializer.Serializeuse this path only whenGetType()is exactly the base type. Subclasses that might override the virtualWrite/WriteIdentity/WriteClaimmethods keep the existingBinaryWriterpath.Identities/Claims, which avoids the secondCount()enumeration. The count is a fixed 4-byteInt32, so the format is unchanged.ArgumentNullExceptionparameter names are unchanged (ticket,properties,value).BinaryWriterpath directly.Benchmarks
Local BenchmarkDotNet run in-process, 3 rounds, main vs PR. The ticket mimics a typical Identity application cookie: 7 claims (2 roles, 3 well-known types) and properties with Issued, Expires and IsPersistent.
That is about −30% time for tickets and −15% for properties. The remaining ticket allocations are the result array plus the
Claim.Propertiesdictionaries, which the publicClaim.Propertiesgetter creates lazily.The benchmark is added as
src/Security/perf/Microbenchmarks/TicketSerializerWriteBenchmark.cs.Tests
New tests in
TicketSerializerTests:TicketSerializer.Write(BinaryWriter, …)and against a subclass, which takes theBinaryWriterpath. Tickets cover 0, 10 and 500 extra claims, which exercises buffer growth. They include:PropertiesSerializerbyte equality, including a null item value, plus the null-argument case.Microsoft.AspNetCore.Authentication.Test: 721 passed, 3 skipped (pre-existing), 0 failed.Benchmark (3 interleaved rounds)
src/Security/perf/Microbenchmarks/TicketSerializerWriteBenchmark.cs(added in this PR), Release builds.Averaged over rounds: about −31% time / −71% allocations for tickets and −13% time / −64% allocations for properties, consistent with the run above.
Machine: AMD EPYC 9V74, 2 cores, Linux; .NET SDK 12.0.100-alpha.1.26521.103, .NET 12.0.0 (12.0.26.52911) X64 RyuJIT, Server GC. BenchmarkDotNet in-process toolchain,
--warmupCount 3 --iterationCount 10,[MemoryDiagnoser]. "main" is this PR's merge base (4f556f7); both builds use identical benchmark source. Runs were interleaved: main, PR, main, PR, main, PR.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)