[Analyzers] Add analyzer for capturing lambdas in Task.Run/ContinueWith#8395
Draft
lucaspimentel wants to merge 8 commits intomasterfrom
Draft
[Analyzers] Add analyzer for capturing lambdas in Task.Run/ContinueWith#8395lucaspimentel wants to merge 8 commits intomasterfrom
Task.Run/ContinueWith#8395lucaspimentel wants to merge 8 commits intomasterfrom
Conversation
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
Backlog of 7 analyzers (DDALLOC001-007) to detect heap-allocation anti-patterns at compile time, covering: ToString() in log calls, missing JsonArrayPool, new StringBuilder, Enum.HasFlag/ToString boxing, capturing lambdas in Task.Run, and throw-in-AggressiveInlining methods. 🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
Detects non-static lambdas passed to Task.Run, Task.Factory.StartNew, and .ContinueWith that capture variables from the enclosing scope, causing closure allocations (display class + delegate). Uses SemanticModel.AnalyzeDataFlow to detect actual captures rather than flagging all non-static lambdas. Reports captured variable names in the diagnostic message. 🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
Task.Run/ContinueWith
Task.Run/ContinueWithTask.Run/ContinueWith
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (8395) and master. ✅ No regressions detected - check the details below Full Metrics ComparisonFakeDbCommand
HttpMessageHandler
Comparison explanationExecution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:
Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard. Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph). Duration chartsFakeDbCommand (.NET Framework 4.8)gantt
title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (76ms) : 73, 78
master - mean (76ms) : 73, 79
section Bailout
This PR (8395) - mean (80ms) : 78, 82
master - mean (81ms) : 78, 83
section CallTarget+Inlining+NGEN
This PR (8395) - mean (1,105ms) : 1053, 1157
master - mean (1,102ms) : 1061, 1143
FakeDbCommand (.NET Core 3.1)gantt
title Execution time (ms) FakeDbCommand (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (119ms) : 115, 122
master - mean (120ms) : 116, 124
section Bailout
This PR (8395) - mean (121ms) : 117, 124
master - mean (121ms) : 118, 124
section CallTarget+Inlining+NGEN
This PR (8395) - mean (812ms) : 789, 835
master - mean (810ms) : 786, 835
FakeDbCommand (.NET 6)gantt
title Execution time (ms) FakeDbCommand (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (105ms) : 101, 109
master - mean (105ms) : 100, 109
section Bailout
This PR (8395) - mean (107ms) : 103, 110
master - mean (105ms) : 103, 108
section CallTarget+Inlining+NGEN
This PR (8395) - mean (952ms) : 920, 984
master - mean (950ms) : 919, 982
FakeDbCommand (.NET 8)gantt
title Execution time (ms) FakeDbCommand (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (104ms) : 101, 107
master - mean (104ms) : 101, 107
section Bailout
This PR (8395) - mean (105ms) : 102, 108
master - mean (105ms) : 103, 108
section CallTarget+Inlining+NGEN
This PR (8395) - mean (839ms) : 800, 877
master - mean (835ms) : 795, 876
HttpMessageHandler (.NET Framework 4.8)gantt
title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (189ms) : 186, 193
master - mean (189ms) : 186, 192
section Bailout
This PR (8395) - mean (192ms) : 191, 194
master - mean (192ms) : 191, 194
section CallTarget+Inlining+NGEN
This PR (8395) - mean (1,135ms) : 1093, 1176
master - mean (1,131ms) : 1085, 1177
HttpMessageHandler (.NET Core 3.1)gantt
title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (271ms) : 266, 277
master - mean (272ms) : 268, 276
section Bailout
This PR (8395) - mean (271ms) : 268, 275
master - mean (272ms) : 269, 275
section CallTarget+Inlining+NGEN
This PR (8395) - mean (917ms) : 895, 939
master - mean (921ms) : 892, 951
HttpMessageHandler (.NET 6)gantt
title Execution time (ms) HttpMessageHandler (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (266ms) : 263, 269
master - mean (265ms) : 261, 269
section Bailout
This PR (8395) - mean (265ms) : 263, 267
master - mean (265ms) : 263, 267
section CallTarget+Inlining+NGEN
This PR (8395) - mean (1,130ms) : 1090, 1171
master - mean (1,131ms) : 1092, 1170
HttpMessageHandler (.NET 8)gantt
title Execution time (ms) HttpMessageHandler (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8395) - mean (264ms) : 259, 268
master - mean (263ms) : 259, 266
section Bailout
This PR (8395) - mean (264ms) : 261, 266
master - mean (263ms) : 260, 266
section CallTarget+Inlining+NGEN
This PR (8395) - mean (1,011ms) : 970, 1052
master - mean (1,012ms) : 967, 1058
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Summary of changes
Add a new Roslyn analyzer (
DDALLOC006) that detects lambdas passed toTask.Run,Task.Factory.StartNew, and.ContinueWiththat capture variables from the enclosing scope, causing closure allocations.Reason for change
Capturing lambdas in task-scheduling APIs allocate a compiler-generated display class and delegate on every invocation. This analyzer helps identify these allocations so they can be replaced with static lambdas using the
stateparameter overloads.Implementation details
CapturingLambdaAnalyzerusing Roslyn'sAnalyzeDataFlowto detect captured variablesTask.Run,Task.Factory.StartNew, and.ContinueWithon bothTaskandTask<T>isEnabledByDefault: false) — projects opt in via.editorconfigDatadog.Trace/.editorconfiguntil existing 23 usages are migratedTest coverage
13 unit tests covering:
Task.Run,StartNew,ContinueWith,Task<T>, anonymous delegates, multiple captures,thiscapturestateparameter overloads, unrelated methods, async non-capturing lambdasOther details
The analyzer is intentionally shipped disabled. To enable, add to
.editorconfig: