Skip to content

Avoid intermediate copies in TimeLimitedDataProtector - #70191

Merged
DeagleGross merged 4 commits into
mainfrom
perf/dataprotection-timelimited-copies
Nov 6, 2026
Merged

DeagleGross merged 4 commits into
mainfrom
perf/dataprotection-timelimited-copies

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Avoid intermediate copies in TimeLimitedDataProtector

Description

TimeLimitedDataProtector.Protect copied the plaintext into a new { expiration (8 B, big-endian UTC ticks) || plaintext } array before protecting it, and Unprotect received 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 (which CreateProtector always returns on .NET), it now:

  • Protect: writes the header and plaintext into a shared 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.
  • Unprotect: unprotects into a 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.

Alg Scenario main PR main alloc PR alloc
CBC Protect 2 KB 14.21 us 13.82 us 7,328 B 2,912 B (-60%)
CBC Unprotect 2 KB 9.78 us 9.57 us 4,704 B 2,624 B (-44%)
GCM Protect 2 KB 8.65 us 8.67 us 4,576 B 2,496 B (-45%)
GCM Unprotect 2 KB 6.41 us 6.24 us 4,424 B 2,344 B (-47%)

Time is neutral (within noise); the win is allocation.

After the review update (8f09526, shared RefPooledArrayBufferWriter with 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.

Scenario main previous head new head main alloc previous alloc new alloc
Protect 32 B 8.62 us 8.50 us 8.58 us 1,344 B 960 B 960 B (-29%)
Unprotect 32 B 6.10 us 6.06 us 6.15 us 736 B 672 B 672 B (-9%)
Protect 2 KB 12.61 us 12.33 us 12.40 us 7,392 B 2,976 B 2,976 B (-60%)
Unprotect 2 KB 7.96 us 7.81 us 7.86 us 4,768 B 2,688 B 2,688 B (-44%)

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 ISpanAuthenticatedEncryptor and take the zero-copy path above. For a custom byte[]-only IAuthenticatedEncryptor, KeyRingBasedSpanDataProtector falls 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

  • Byte-identical payloads: the inner protector gets exactly the same bytes as before. The new known-answer theory Protect_SpanInnerProtector_ProducesSamePayloadAsByteArrayPath (0 B–70 KB) uses a real KeyRingBasedSpanDataProtector with a deterministic RNG. It asserts the span path output equals the existing byte[] path output (the inner protector is wrapped to hide ISpanDataProtector), and that each path unprotects the other's payload with the same expiration.
  • Plaintext hygiene improves: the buffers that hold plaintext are zeroed: rented arrays are cleared when returned to the pool (including when the writer grows), and the 256-byte stack buffers are cleared before the method returns. Previously the intermediate { expiration || plaintext } arrays were left to the GC without being zeroed. No key material passes through these buffers.
  • Nothing pooled is caller-reachable: the buffers are scoped to one call; only freshly allocated result arrays escape.
  • Exceptions unchanged: the span path runs inside the same homogenizing 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.cs and makes the same one-line change to it: it now returns arrays with clearArray: true when 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 *PooledArrayBufferWriter tests: 47 passed.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

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>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 16:04
@github-actions github-actions Bot added the area-dataprotection Includes: DataProtection label Oct 31, 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

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

Comment on lines +88 to +90
if (innerProtector is ISpanDataProtector spanProtector)
{
return UnprotectWithSpanProtector(spanProtector, protectedData, now, out expiration);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

finally
{
ArrayPool<byte>.Shared.Return(plaintextWithHeader, clearArray: true);
destination.Dispose();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when using RefPooledArrayBufferWriter you can use using to dispose

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@DeagleGross

Copy link
Copy Markdown
Member

In general, I am thinking whether to merge this PR at all or not - I wanted to create a new ISpanTimeLimitedDataProtector which should use the extended API of ISpanDataProtector and just become a new zero-allocatey (on its level) API which then can be used by other consumers.

This PR is an improvement, but not the ideal one we would want to take. However does not hurt..?

@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 5, 2026 — with Copilot SWE Agent
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>
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 5, 2026
@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 5, 2026 — with Copilot SWE Agent

Copy link
Copy Markdown
Member Author

I wanted to create a new ISpanTimeLimitedDataProtector which should use the extended API of ISpanDataProtector and just become a new zero-allocatey (on its level) API which then can be used by other consumers.

(Replying to #70191 (comment))

Agreed that a span-based ISpanTimeLimitedDataProtector would be the better end state. It's a new public API, though, and this PR deliberately avoids public API changes, so that would need a separate API proposal. This PR only changes the internals of the existing byte[] API. If that API is added later, its implementation could reuse the internal span path from this PR.

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.

Scenario main this PR
Protect 32 B 8.62 us / 1,344 B 8.58 us / 960 B (-29%)
Unprotect 32 B 6.10 us / 736 B 6.15 us / 672 B (-9%)
Protect 2 KB 12.61 us / 7,392 B 12.40 us / 2,976 B (-60%)
Unprotect 2 KB 7.96 us / 4,768 B 7.86 us / 2,688 B (-44%)

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 IAuthenticatedEncryptor that doesn't implement ISpanAuthenticatedEncryptor pays an extra ciphertext-sized copy on Unprotect, about 2 KB for a 2 KB payload. All built-in encryptors take the span path.

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)

@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 5, 2026
@DeagleGross
DeagleGross enabled auto-merge (squash) November 6, 2026 12:14
@DeagleGross
DeagleGross merged commit 396bd81 into main Nov 6, 2026
27 checks passed
@DeagleGross
DeagleGross deleted the perf/dataprotection-timelimited-copies branch November 6, 2026 12:34
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

✅ 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

  • Conceptual article: Not required — Internal allocation optimization; header format, expiration check, and error behavior unchanged; no public API change.
  • Migration guidance: Not required — No action required of users upgrading.
  • Breaking change: Not required — PR states behavior is unchanged; no compatibility impact.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — ecba2a64 Deployed Nov 6, 2026 by DeagleGross via conclusion #206
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-dataprotection Includes: DataProtection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants