Skip HttpContext.Items allocation in CsrfProtectionMiddleware hot path#67488
Open
DeagleGross wants to merge 4 commits into
Open
Skip HttpContext.Items allocation in CsrfProtectionMiddleware hot path#67488DeagleGross wants to merge 4 commits into
DeagleGross wants to merge 4 commits into
Conversation
PR dotnet#67119 introduced an unconditional context.Items[MiddlewareInvokedKeys.CsrfProtection] = ... write in CsrfProtectionMiddleware.InvokeAsync, which forces the lazy HttpContext.Items dictionary to allocate on every request. This blew up allocations on hot non-antiforgery paths (~264 B/request extra; ~1.3 GB/s at TechEmpower plaintext throughput). See PR dotnet#67119 comment 4835979504. Gate the marker write on either endpoint == null (re-execute into an antiforgery-required page must still find the marker) or the matched endpoint carrying any IAntiforgeryMetadata (both DisableAntiforgery() for re-execute and the FormFeature backstop for RequiresValidation:true). For endpoints with no antiforgery metadata at all - the hot non-antiforgery path - the marker has no consumer and is now skipped. Adds CsrfProtection_HotPath_NoEndpointMetadata_DoesNotAllocateItemsDictionary as a regression guard using GC.GetAllocatedBytesForCurrentThread(). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes the auto-injected CsrfProtectionMiddleware hot path by avoiding unnecessary HttpContext.Items initialization when an endpoint has no antiforgery metadata, while preserving the marker behavior needed for reroute/re-execute scenarios and antiforgery/disable-antiforgery endpoints.
Changes:
- Conditionally stamps the
MiddlewareInvokedKeys.CsrfProtectionmarker only whenendpointisnullor whenIAntiforgeryMetadatais present. - Updates integration tests to assert the marker is not set for endpoints without antiforgery metadata.
Show a summary per file
| File | Description |
|---|---|
| src/DefaultBuilder/src/Internal/CsrfProtectionMiddleware.cs | Avoids HttpContext.Items allocation by not stamping the marker on the no-antiforgery-metadata path. |
| src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/CsrfProtectionIntegrationTests.cs | Adjusts assertions to reflect that the marker is not set when the endpoint has no antiforgery metadata. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes degradation noticed by @BrennanConroy (thanks!) #67119 (comment)
CsrfProtectionMiddlewarewas unconditionally writingMiddlewareInvokedKeys.CsrfProtectiontocontext.Items. I did not run the crank-based tests to confirm allocation improvement, but used a local test (see source code below). The results show thatCsrfProtectionMiddlewareallocates 0 bytes compared to 264 bytes without the fix.