Defer display class allocation for async local functions to call site#82430
Open
benaadams wants to merge 4 commits intodotnet:mainfrom
Open
Defer display class allocation for async local functions to call site#82430benaadams wants to merge 4 commits intodotnet:mainfrom
benaadams wants to merge 4 commits intodotnet:mainfrom
Conversation
When async local functions capture variables via closure, the display class is heap-allocated at method entry unconditionally — even when the local function is never called. This defers display class allocation to the call site for eligible async local functions, keeping captured variables as locals on the fast path and only allocating the closure frame when the local function is actually invoked.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates C# closure conversion to defer heap allocation of the display-class (closure frame) for eligible async local functions until the local function is actually invoked, avoiding an unconditional method-entry allocation on the synchronous fast-path.
Changes:
- Adds analysis to conservatively detect “deferrable” top-level closure environments and validate call sites (must be top-level
return ...positions). - Updates closure conversion rewriting to (1) avoid proxying captured locals/parameters on the top-level fast path, and (2) inject frame allocation + captured-value copying at the local-function call site.
- Adds IL-based codegen tests validating deferred vs non-deferred allocation behavior across several scenarios.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs | Adds positive/negative IL tests asserting where the display-class newobj appears relative to get_IsCompleted(). |
| src/Compilers/CSharp/Portable/Lowering/MethodToClassRewriter.cs | Introduces ProxyReplacementContext so proxy replacements can distinguish rewriting context. |
| src/Compilers/CSharp/Portable/Lowering/ClosureConversion/ClosureConversion.cs | Implements deferred frame initialization and dual-mode proxy replacement for deferrable captures; wraps eligible call sites in a sequence that allocates/copies. |
| src/Compilers/CSharp/Portable/Lowering/ClosureConversion/ClosureConversion.Analysis.cs | Adds deferrable-environment detection and a walker to validate call-site constraints. |
| src/Compilers/CSharp/Portable/Lowering/ClosureConversion/ClosureConversion.Analysis.Tree.cs | Extends ClosureEnvironment to track IsDeferrable. |
src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs
Outdated
Show resolved
Hide resolved
Replace SymbolEqualityComparer.ConsiderEverything.Equals with ReferenceEquals for local function identity checks — both sides are OriginalDefinition from the same compilation. Tighten generic test method lookup from "Program.M" to "Program.M<" to avoid matching Main.
…version Switch _deferredEnvironmentsByLocalFunction dictionary from SymbolEqualityComparer.ConsiderEverything to ReferenceEqualityComparer.Instance to make the reference equality intent explicit. Add null-forgiving operators where BoundExpression.Type is known to be non-null.
VisualizeIL formats property getters as "IsCompleted.get", not "get_IsCompleted()".
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 #18946
When an async local function captures variables via closure, the display class is heap-allocated at method entry unconditionally - even when the local function is never called. This means the fast path (where the async work completes synchronously) pays for an allocation it doesn't need.
This PR defers display class allocation to the call site for eligible async local functions. Captured variables remain as locals on the fast path; the closure frame is only allocated when the local function is actually invoked.
Analysis phase (ClosureConversion.Analysis.cs):
Code generation (ClosureConversion.cs):
Proxy context (MethodToClassRewriter.cs):
Tests