Skip to content

[Analyzers] Add analyzer for capturing lambdas in Task.Run/ContinueWith#8395

Draft
lucaspimentel wants to merge 8 commits intomasterfrom
lpimentel/allocation-analyzer-capturing-lambdas
Draft

[Analyzers] Add analyzer for capturing lambdas in Task.Run/ContinueWith#8395
lucaspimentel wants to merge 8 commits intomasterfrom
lpimentel/allocation-analyzer-capturing-lambdas

Conversation

@lucaspimentel
Copy link
Copy Markdown
Member

@lucaspimentel lucaspimentel commented Apr 1, 2026

Summary of changes

Add a new Roslyn analyzer (DDALLOC006) that detects lambdas passed to Task.Run, Task.Factory.StartNew, and .ContinueWith that 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 state parameter overloads.

Implementation details

  • New analyzer: CapturingLambdaAnalyzer using Roslyn's AnalyzeDataFlow to detect captured variables
  • Reports the specific captured variable names in the diagnostic message
  • Handles Task.Run, Task.Factory.StartNew, and .ContinueWith on both Task and Task<T>
  • Correctly skips static lambdas, method groups, and non-capturing lambdas
  • Disabled by default (isEnabledByDefault: false) — projects opt in via .editorconfig
  • Suppressed in Datadog.Trace/.editorconfig until existing 23 usages are migrated

Test coverage

13 unit tests covering:

  • Positive cases: Task.Run, StartNew, ContinueWith, Task<T>, anonymous delegates, multiple captures, this capture
  • Negative cases: static lambdas, non-capturing lambdas, method groups, state parameter overloads, unrelated methods, async non-capturing lambdas

Other details

The analyzer is intentionally shipped disabled. To enable, add to .editorconfig:

dotnet_diagnostic.DDALLOC006.severity = warning

"I analyze your closures so you don't have to close your eyes to allocations." — Claude 🤖

🤖 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>
@lucaspimentel lucaspimentel changed the title [Analyzers] Add DDALLOC006 for capturing lambdas in Task.Run/ContinueWith [Analyzers] Add analyzers for capturing lambdas in Task.Run/ContinueWith Apr 1, 2026
@lucaspimentel lucaspimentel changed the title [Analyzers] Add analyzers for capturing lambdas in Task.Run/ContinueWith [Analyzers] Add analyzer for capturing lambdas in Task.Run/ContinueWith Apr 1, 2026
@dd-trace-dotnet-ci-bot
Copy link
Copy Markdown

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8395) and master.

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration76.08 ± (76.02 - 76.40) ms75.48 ± (75.61 - 75.97) ms-0.8%
.NET Framework 4.8 - Bailout
duration80.67 ± (80.52 - 80.90) ms79.78 ± (79.67 - 80.04) ms-1.1%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1098.66 ± (1099.16 - 1104.78) ms1100.12 ± (1101.83 - 1108.69) ms+0.1%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms23.20 ± (23.15 - 23.25) ms23.10 ± (23.04 - 23.16) ms-0.4%
process.time_to_main_ms89.25 ± (89.05 - 89.44) ms88.30 ± (88.10 - 88.51) ms-1.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.89 ± (10.89 - 10.90) MB10.90 ± (10.90 - 10.90) MB+0.1%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms23.24 ± (23.19 - 23.30) ms23.15 ± (23.09 - 23.22) ms-0.4%
process.time_to_main_ms90.56 ± (90.34 - 90.78) ms90.27 ± (90.03 - 90.51) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.93 ± (10.93 - 10.94) MB10.94 ± (10.93 - 10.94) MB+0.0%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms233.52 ± (232.52 - 234.53) ms231.97 ± (231.02 - 232.93) ms-0.7%
process.time_to_main_ms538.04 ± (536.72 - 539.35) ms538.86 ± (537.38 - 540.34) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed47.63 ± (47.60 - 47.65) MB47.71 ± (47.67 - 47.74) MB+0.2%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%
.NET 6 - Baseline
process.internal_duration_ms21.79 ± (21.75 - 21.83) ms21.77 ± (21.72 - 21.82) ms-0.1%
process.time_to_main_ms76.48 ± (76.26 - 76.70) ms76.59 ± (76.38 - 76.79) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.62 ± (10.62 - 10.62) MB10.65 ± (10.64 - 10.65) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.65 ± (21.60 - 21.71) ms21.82 ± (21.76 - 21.88) ms+0.8%✅⬆️
process.time_to_main_ms77.17 ± (76.98 - 77.35) ms77.87 ± (77.67 - 78.08) ms+0.9%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.73 ± (10.72 - 10.73) MB10.74 ± (10.74 - 10.75) MB+0.2%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms384.63 ± (382.52 - 386.74) ms387.17 ± (385.25 - 389.08) ms+0.7%✅⬆️
process.time_to_main_ms539.71 ± (538.63 - 540.78) ms538.53 ± (537.41 - 539.66) ms-0.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed49.93 ± (49.89 - 49.96) MB49.89 ± (49.86 - 49.92) MB-0.1%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.1%
.NET 8 - Baseline
process.internal_duration_ms20.01 ± (19.97 - 20.06) ms19.88 ± (19.83 - 19.92) ms-0.7%
process.time_to_main_ms75.99 ± (75.83 - 76.14) ms75.89 ± (75.72 - 76.07) ms-0.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.66 ± (7.66 - 7.67) MB7.66 ± (7.66 - 7.67) MB-0.0%
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms20.05 ± (20.00 - 20.10) ms19.95 ± (19.90 - 20.00) ms-0.5%
process.time_to_main_ms77.45 ± (77.30 - 77.61) ms77.22 ± (77.00 - 77.44) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.72 ± (7.71 - 7.73) MB7.72 ± (7.71 - 7.72) MB-0.0%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms306.40 ± (304.15 - 308.65) ms310.68 ± (308.29 - 313.07) ms+1.4%✅⬆️
process.time_to_main_ms498.65 ± (497.71 - 499.60) ms498.45 ± (497.63 - 499.27) ms-0.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.95 ± (36.93 - 36.97) MB36.99 ± (36.97 - 37.01) MB+0.1%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)+0.6%✅⬆️

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration188.70 ± (188.76 - 189.41) ms188.86 ± (188.99 - 189.80) ms+0.1%✅⬆️
.NET Framework 4.8 - Bailout
duration192.42 ± (192.25 - 192.58) ms192.41 ± (192.22 - 192.56) ms-0.0%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1128.71 ± (1128.00 - 1134.47) ms1132.78 ± (1131.76 - 1137.60) ms+0.4%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms184.42 ± (184.11 - 184.73) ms184.05 ± (183.72 - 184.38) ms-0.2%
process.time_to_main_ms79.28 ± (79.08 - 79.47) ms79.32 ± (79.11 - 79.52) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.01 ± (15.93 - 16.10) MB16.15 ± (16.07 - 16.22) MB+0.8%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 20)20 ± (20 - 20)+1.0%✅⬆️
.NET Core 3.1 - Bailout
process.internal_duration_ms183.49 ± (183.27 - 183.71) ms183.38 ± (183.16 - 183.60) ms-0.1%
process.time_to_main_ms80.41 ± (80.31 - 80.50) ms80.32 ± (80.23 - 80.42) ms-0.1%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.80 ± (15.66 - 15.95) MB15.96 ± (15.83 - 16.09) MB+1.0%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)+0.5%✅⬆️
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms391.01 ± (389.77 - 392.24) ms389.18 ± (387.69 - 390.68) ms-0.5%
process.time_to_main_ms501.90 ± (500.88 - 502.92) ms498.18 ± (496.94 - 499.41) ms-0.7%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.21 ± (58.04 - 58.38) MB57.86 ± (57.65 - 58.08) MB-0.6%
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.1%
.NET 6 - Baseline
process.internal_duration_ms188.03 ± (187.78 - 188.28) ms188.63 ± (188.41 - 188.85) ms+0.3%✅⬆️
process.time_to_main_ms68.93 ± (68.80 - 69.07) ms69.20 ± (69.05 - 69.36) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.55 ± (15.37 - 15.72) MB15.75 ± (15.57 - 15.93) MB+1.3%✅⬆️
runtime.dotnet.threads.count17 ± (17 - 18)18 ± (17 - 18)+0.7%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms187.74 ± (187.60 - 187.88) ms187.40 ± (187.22 - 187.58) ms-0.2%
process.time_to_main_ms69.81 ± (69.76 - 69.85) ms69.87 ± (69.82 - 69.93) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.95 ± (15.79 - 16.11) MB15.41 ± (15.24 - 15.57) MB-3.4%
runtime.dotnet.threads.count19 ± (19 - 19)18 ± (18 - 19)-2.5%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms596.68 ± (593.95 - 599.41) ms598.03 ± (595.33 - 600.73) ms+0.2%✅⬆️
process.time_to_main_ms504.29 ± (503.37 - 505.22) ms503.42 ± (502.62 - 504.22) ms-0.2%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed61.44 ± (61.35 - 61.54) MB61.58 ± (61.48 - 61.67) MB+0.2%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.3%
.NET 8 - Baseline
process.internal_duration_ms185.45 ± (185.21 - 185.70) ms185.88 ± (185.67 - 186.09) ms+0.2%✅⬆️
process.time_to_main_ms68.18 ± (68.05 - 68.30) ms68.51 ± (68.35 - 68.66) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.80 ± (11.75 - 11.86) MB11.75 ± (11.68 - 11.82) MB-0.4%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)-0.9%
.NET 8 - Bailout
process.internal_duration_ms184.85 ± (184.67 - 185.03) ms185.26 ± (185.09 - 185.43) ms+0.2%✅⬆️
process.time_to_main_ms69.27 ± (69.21 - 69.33) ms69.34 ± (69.28 - 69.40) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.65 ± (11.54 - 11.75) MB11.61 ± (11.50 - 11.72) MB-0.3%
runtime.dotnet.threads.count18 ± (18 - 19)18 ± (18 - 19)+0.4%✅⬆️
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms523.27 ± (520.98 - 525.56) ms518.82 ± (515.93 - 521.70) ms-0.9%
process.time_to_main_ms457.03 ± (456.32 - 457.74) ms460.56 ± (459.87 - 461.24) ms+0.8%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed50.67 ± (50.65 - 50.70) MB50.62 ± (50.59 - 50.65) MB-0.1%
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)+0.3%✅⬆️
Comparison explanation

Execution-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:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

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 charts
FakeDbCommand (.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

Loading
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

Loading
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

Loading
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

Loading
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

Loading
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

Loading
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

Loading
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

Loading

@lucaspimentel lucaspimentel added area:builds project files, build scripts, pipelines, versioning, releases, packages AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos labels Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos area:builds project files, build scripts, pipelines, versioning, releases, packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant