Skip to content

Avoid validation algorithm allocation in ManagedAuthenticatedEncryptor - #69789

Merged
DeagleGross merged 1 commit into
dotnet:mainfrom
DeagleGross:deaglegross-antiforgery-performance-regression
Oct 14, 2026
Merged

DeagleGross merged 1 commit into
dotnet:mainfrom
DeagleGross:deaglegross-antiforgery-performance-regression

Conversation

@DeagleGross

@DeagleGross DeagleGross commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

I've looked into #66536, and while I did not figure out if that's the runtime regression (see discussion between Brennan and Kevin in #66536 (comment)), I found we could avoid allocation for SHA256 and SHA512 (most popular covered algorithms).

Local runs on sample:

Operation Before Updated Time reduction
DataProtection Protect — SHA256 8.224 µs 5.827 µs 29.1%
DataProtection Unprotect — SHA256 6.535 µs 4.209 µs 35.6%
Managed SHA512 encryption 10.274 µs 7.850 µs 23.6%
Managed SHA512 decryption 9.001 µs 6.137 µs 31.8%

And SpanDataProtectorComparison benchmarks:

Method Plaintext bytes Old time (µs) RC2 time (µs) Patched time (µs) Old allocation (B/op) RC2 allocation (B/op) Patched allocation (B/op)
ByteArray 5 11.922 12.283 9.089 2,184 2,344 1,592
PooledWriter 5 11.817 12.741 8.658 1,840 1,960 1,208
RefWriter 5 12.343 12.111 8.623 1,776 1,896 1,144
ByteArray 50 12.423 12.348 8.902 2,328 2,488 1,736
PooledWriter 50 11.863 12.255 8.858 1,840 1,960 1,208
RefWriter 50 11.876 12.707 9.092 1,776.5¹ 1,896 1,144
ByteArray 80 11.948 12.495 9.282 2,416 2,576 1,824
PooledWriter 80 11.799 12.312 8.775 1,840 1,960 1,208
RefWriter 80 11.893 12.747 8.785 1,776 1,896 1,144
ByteArray 100 11.563 12.467 9.306 2,472 2,632 1,880
PooledWriter 100 11.985 12.352 9.201 1,840 1,960 1,208
RefWriter 100 11.472 12.212 8.769 1,776 1,896 1,144

Fixes #66536

@DeagleGross DeagleGross self-assigned this Oct 13, 2026
@DeagleGross DeagleGross added the area-dataprotection Includes: DataProtection label Oct 13, 2026
Copilot AI balanced review requested due to automatic review settings October 13, 2026 17:05
@DeagleGross DeagleGross added the area-perf Performance infrastructure issues label Oct 13, 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.

🟢 Approval recommended

The optimization preserves existing fallback behavior and remains covered by SHA-256 and SHA-512 round-trip paths.

0 open findings

What changed in this PR

Optimizes managed Data Protection by caching HMAC classification and avoiding per-operation algorithm allocations for SHA-256 and SHA-512.

Changes:

  • Caches the validation algorithm type during construction.
  • Uses static one-shot HMAC APIs for common algorithms.
  • Retains the existing factory path for other algorithms.
File Description
ManagedAuthenticatedEncryptor.cs Uses cached HMAC classification during encryption and validation.
HashAlgorithmType.cs Defines internal algorithm categories.
HashAlgorithmExtensions.cs Maps keyed hash implementations to those categories.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@DeagleGross
DeagleGross merged commit f4777b2 into dotnet:main Oct 14, 2026
39 of 42 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

No docs needed: internal allocation-reduction change with no public or observable behavior change.

Documentation surfaces considered

  • Conceptual article: Not required — Internal perf optimization in HashAlgorithmExtensions/ManagedAuthenticatedEncryptor; no public API or behavior change.
  • Migration guidance: Not required — No user action needed on upgrade.
  • Breaking change: Not required — No compatibility impact; no signals detected.

@DeagleGross

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

This branch was previously deployed

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

Labels

area-dataprotection Includes: DataProtection area-perf Performance infrastructure issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perf regression: antiforgery-generation, antiforgery-validation

3 participants