Repository navigation
Avoid intermediate copies in TimeLimitedDataProtector - #70191
Conversation
Use ISpanDataProtector so the { expiration || plaintext } buffer and the
unprotected payload use pooled buffers that are cleared on return.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The new branch introduces extra ciphertext, authenticated-data, and plaintext copies for custom byte-array-only encryptors, creating an allocation regression that needs correction.
1 open issue
What changed in this PR
This PR reduces intermediate allocations in TimeLimitedDataProtector by using pooled buffers when the inner protector supports span-based operations.
Changes:
- Adds pooled span-based protection and unprotection paths while retaining the existing array-based fallback.
- Introduces a shared buffer writer that clears rented buffers on growth and disposal.
- Adds tests for payload compatibility, expiration, malformed input, exception handling, and buffer growth.
| File | Description |
|---|---|
src/DataProtection/shared/src/ClearingPooledBufferWriter.cs |
Adds a clearing, pooled buffer writer. |
src/DataProtection/Extensions/test/TimeLimitedDataProtectorTests.cs |
Tests span-path behavior and compatibility with the array-based path. |
src/DataProtection/Extensions/src/TimeLimitedDataProtector.cs |
Uses pooled buffers for span-capable inner protectors. |
Review effort: Balanced
| if (innerProtector is ISpanDataProtector spanProtector) | ||
| { | ||
| return UnprotectWithSpanProtector(spanProtector, protectedData, now, out expiration); |
There was a problem hiding this comment.
Partially agree. I pushed ca43838, which removes the avoidable copy: in the span Unprotect fallback, GetAadForKey already returns a byte[], so it's now passed directly instead of aad.ToArray().
The remaining extra copy (actualCiphertext.ToArray()) is inherent to running a span-based protector over a byte[]-only encryptor, and it only affects custom IAuthenticatedEncryptor implementations. All built-in encryptors implement ISpanAuthenticatedEncryptor: managed CBC (ManagedAuthenticatedEncryptor), managed GCM (AesGcmAuthenticatedEncryptor), CNG CBC (CbcAuthenticatedEncryptor) and CNG GCM (CngGcmAuthenticatedEncryptor). They take the zero-copy path benchmarked in the PR description (−44% to −60% allocations).
I measured the custom-encryptor case with a byte[]-only wrapper around the managed CBC encryptor, using a cookie purpose chain and a 2 KB payload, averaged over 1,000 ops (GC.GetAllocatedBytesForCurrentThread), after this commit:
| Path | Protect | Unprotect |
|---|---|---|
| main (byte[] path) | 8,297 B | 5,818 B |
| this PR (span path) | 8,224 B | 7,780 B |
Protect is neutral. Unprotect pays one ciphertext-sized copy (about 2 KB here) only for custom byte[]-only encryptors. TimeLimitedDataProtector can't tell which encryptor the key ring will choose without resolving the key ring per call, so keeping a byte[] path just for this rare case would give up the win for every built-in configuration. The same fallback cost already applies to any direct ISpanDataProtector caller. I'm leaving this thread open for a maintainer to weigh the trade-off.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)
GetAadForKey already returns an array, so pass it directly to the byte[]-based encryptor instead of copying it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/review |
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(plaintextWithHeader, clearArray: true); | ||
| destination.Dispose(); |
There was a problem hiding this comment.
when using RefPooledArrayBufferWriter you can use using to dispose
There was a problem hiding this comment.
Partly done in 8f09526. The { expiration || plaintext } writer is now a using var. The destination writers are passed to ISpanDataProtector.Protect/Unprotect by ref, and C# doesn't allow that for using variables: error CS1657: Cannot use 'a' as a ref or out value because it is a 'using variable', verified with the current SDK. Those two writers are still disposed in finally, the same pattern used by SpanDataProtectorComparison and CookieTempDataProvider. I'm leaving this open in case you'd prefer a different shape.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)
There was a problem hiding this comment.
Correction to my previous reply: CookieTempDataProvider isn't an example of this pattern. It doesn't dispose its RefPooledArrayBufferWriter. The try/finally reference is SpanDataProtectorComparison.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)
|
In general, I am thinking whether to merge this PR at all or not - I wanted to create a new This PR is an improvement, but not the ideal one we would want to take. However does not hurt..? |
Replace the custom ClearingPooledBufferWriter with the shared
RefPooledArrayBufferWriter, starting from a 256-byte stack buffer that
grows from the pool as needed. This removes the size-estimate constant.
The { expiration || plaintext } buffer is a 'using' writer; writers passed
by ref are disposed in finally (CS1657). Stack buffers holding plaintext
are cleared, and RefPooledArrayBufferWriter now clears arrays it returns
to the pool when it grows (same change as #70187).
Add tests on both sides of the 256-byte initial buffer.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
(Replying to #70191 (comment)) Agreed that a span-based On "does not hurt": here's what I measured for the current head (8f09526) against main. Setup: BenchmarkDotNet, CBC, cookie-style purpose chain, median of 2 interleaved rounds.
Time is within noise and the gain is allocation. Payloads are byte-identical to main, which a known-answer test checks across sizes on both sides of the 256-byte stack buffer. Error behavior is unchanged too. The one downside: a custom Whether to take this as an interim improvement or wait for the new API is your call. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
✅ No documentation PR was created. Confidence that documentation is needed: 5% No docs needed: internal refactoring/performance work with no public or observable behavior change. Documentation surfaces considered
|

Avoid intermediate copies in TimeLimitedDataProtector
Description
TimeLimitedDataProtector.Protectcopied the plaintext into a new{ expiration (8 B, big-endian UTC ticks) || plaintext }array before protecting it, andUnprotectreceived the full{ expiration || plaintext }array from the inner protector and then copied the payload into a second array.On .NET, when the inner protector implements
ISpanDataProtector(whichCreateProtectoralways returns on .NET), it now:RefPooledArrayBufferWriter<byte>that starts from a 256-byte stack buffer, and protects it into a second writer of the same kind. The writers rent from the pool only when the data doesn't fit, so no size estimates are needed. The only array allocated is the returned protected payload.RefPooledArrayBufferWriter<byte>that also starts from a 256-byte stack buffer, validates the header and expiration exactly as before, and copies only the payload into the returned array.The header format, the expiration check, the error messages and the exception homogenization (
try/catch→CryptographicException) are unchanged. Non-span inner protectors and non-.NET TFMs use the existing code.Found by profiling (EventPipe CPU + allocation sampling): the header+plaintext copies were 28–46% of the allocations in the TimeLimited scenarios.
Benchmarks
Linux x64, 2 cores, BenchmarkDotNet in-process, 3 interleaved rounds vs main (median).
ToTimeLimitedDataProtector()over a cookie-style purpose chain, 2 KB payload.Time is neutral (within noise); the win is allocation.
After the review update (8f09526, shared
RefPooledArrayBufferWriterwith a 256-byte stack buffer), the A/B below compares main, the previous head (ca43838, custom writer) and the new head. Setup: Linux x64, BenchmarkDotNet, 2 interleaved rounds (median), default CBC algorithm, cookie-style purpose chain.The shared writer is allocation-identical to the previous custom writer and time stays within noise.
Custom (byte[]-only) encryptors
All built-in encryptors (managed CBC/GCM, CNG CBC/GCM) implement
ISpanAuthenticatedEncryptorand take the zero-copy path above. For a custom byte[]-onlyIAuthenticatedEncryptor,KeyRingBasedSpanDataProtectorfalls back to copying the ciphertext span. ca43838 removes the avoidable AAD copy in that fallback. Measured with a byte[]-only wrapper around the managed CBC encryptor, 2 KB payload: Protect 8,297 B → 8,224 B (neutral); Unprotect 5,818 B → 7,780 B (one ciphertext-sized copy). The trade-off is discussed in the review thread.Security reasoning
Protect_SpanInnerProtector_ProducesSamePayloadAsByteArrayPath(0 B–70 KB) uses a realKeyRingBasedSpanDataProtectorwith a deterministic RNG. It asserts the span path output equals the existing byte[] path output (the inner protector is wrapped to hideISpanDataProtector), and that each path unprotects the other's payload with the same expiration.{ expiration || plaintext }arrays were left to the GC without being zeroed. No key material passes through these buffers.try, with the same "payload invalid" and "payload expired" errors (covered by new tests). No public API change, no PublicAPI.*.txt changes, no unsafe code. No crypto, comparison or validation logic is touched.Overlap
No overlap with #70146 (managed encryptor internals). Like #70187, this PR uses the shared
src/Shared/Buffers/RefPooledArrayBufferWriter.csand makes the same one-line change to it: it now returns arrays withclearArray: truewhen it grows. The line is identical in both PRs, so whichever merges second has no conflict.Tests
Microsoft.AspNetCore.DataProtection.Extensions.Tests(net12.0): 35 passed, 1 skipped (pre-existing). New tests cover the span header/payload, expired, malformed and homogenized-exception cases, large payload growth and the known-answer theory. They also cover both sides of the 256-byte stack buffer: known-answer payloads of 247/248/249 bytes, plus a round-trip sweep over every plaintext length from 0 to 600 bytes.Microsoft.AspNetCore.DataProtection.Tests: 337 passed. Shared*PooledArrayBufferWritertests: 47 passed.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)