Skip to content

[Analyzers] Add analyzer to detect new StringBuilder() instead of StringBuilderCache#8400

Draft
lucaspimentel wants to merge 12 commits intomasterfrom
lpimentel/allocation-analyzer-stringbuilder-cache
Draft

[Analyzers] Add analyzer to detect new StringBuilder() instead of StringBuilderCache#8400
lucaspimentel wants to merge 12 commits intomasterfrom
lpimentel/allocation-analyzer-stringbuilder-cache

Conversation

@lucaspimentel
Copy link
Copy Markdown
Member

@lucaspimentel lucaspimentel commented Apr 1, 2026

Summary of changes

Add a new Roslyn analyzer (DDALLOC003) that detects new StringBuilder() allocations and suggests using StringBuilderCache.Acquire() instead. StringBuilderCache uses a [ThreadStatic] cached instance to avoid heap allocations for short-lived builders.

Reason for change

StringBuilderCache exists in the codebase but there's no automated enforcement. Developers may use new StringBuilder() out of habit, missing the opportunity to reuse cached instances and reduce GC pressure.

Implementation details

  • New analyzer (StringBuilderCacheAnalyzer): flags new StringBuilder() and new StringBuilder(capacity) usages
  • Opt-in via .editorconfig: isEnabledByDefault: false, enabled as warning in tracer/src/Datadog.Trace/.editorconfig
  • False positive suppression:
    • Skips StringBuilderCache class itself
    • Skips field/property initializers and constructor assignments to fields (long-lived builders)
    • Skips methods that already call StringBuilderCache.Acquire()
    • Skips methods with multiple StringBuilder allocations (cache only holds one per thread)
    • Scopes analysis to function boundaries (lambdas/local functions analyzed independently)
  • #pragma suppressions for remaining edge cases:
    • IAST StringBuilderAspects (intentionally replacing customer allocations)
    • MongoDB BsonSerializationHelper smoke test (ownership transferred to external API)

Test coverage

18 unit tests covering:

  • All constructor overloads (no args, capacity, string, string+capacity)
  • Field initializers, property initializers, constructor assignments to fields
  • StringBuilderCache class exclusion
  • Method-level StringBuilderCache.Acquire() suppression
  • Multiple builders in same method suppression
  • Lambda/local function scope isolation
  • Multiple builders in different methods (both flagged independently)

Other details

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

dotnet_diagnostic.DDALLOC003.severity = warning

"I tried to cache a joke about StringBuilders, but it kept getting garbage collected." — 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>
…ingBuilderCache`

Diagnostic-only Roslyn analyzer (no code fix) that flags `new StringBuilder()`
allocations and suggests using `StringBuilderCache.Acquire()` instead.
Suppressed when the enclosing method already uses StringBuilderCache or
when inside the StringBuilderCache class itself.

🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
…ning

🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
…ssignments

🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
@lucaspimentel lucaspimentel changed the title [Tracer] Add DDALLOC003 analyzer: detect new StringBuilder() instead of StringBuilderCache [Analyzers] Add analyzer to detect new StringBuilder() instead of StringBuilderCache Apr 1, 2026
@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 1, 2026
Disabled DDALLOC003 warning until existing usages are migrated.
@dd-trace-dotnet-ci-bot
Copy link
Copy Markdown

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8400) 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) ms72.49 ± (72.40 - 72.76) ms-4.7%
.NET Framework 4.8 - Bailout
duration80.67 ± (80.52 - 80.90) ms76.12 ± (76.06 - 76.44) ms-5.6%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1098.66 ± (1099.16 - 1104.78) ms1058.98 ± (1060.73 - 1067.44) ms-3.6%
.NET Core 3.1 - Baseline
process.internal_duration_ms23.20 ± (23.15 - 23.25) ms22.42 ± (22.38 - 22.47) ms-3.3%
process.time_to_main_ms89.25 ± (89.05 - 89.44) ms84.18 ± (83.99 - 84.37) ms-5.7%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.89 ± (10.89 - 10.90) MB10.92 ± (10.92 - 10.92) MB+0.2%✅⬆️
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) ms22.29 ± (22.26 - 22.32) ms-4.1%
process.time_to_main_ms90.56 ± (90.34 - 90.78) ms85.37 ± (85.17 - 85.57) ms-5.7%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.93 ± (10.93 - 10.94) MB10.95 ± (10.94 - 10.95) MB+0.1%✅⬆️
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) ms222.21 ± (221.14 - 223.28) ms-4.8%
process.time_to_main_ms538.04 ± (536.72 - 539.35) ms515.12 ± (514.00 - 516.24) ms-4.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed47.63 ± (47.60 - 47.65) MB47.77 ± (47.74 - 47.81) MB+0.3%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%
.NET 6 - Baseline
process.internal_duration_ms21.79 ± (21.75 - 21.83) ms20.92 ± (20.88 - 20.96) ms-4.0%
process.time_to_main_ms76.48 ± (76.26 - 76.70) ms71.68 ± (71.53 - 71.83) ms-6.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.62 ± (10.62 - 10.62) MB10.63 ± (10.63 - 10.64) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.65 ± (21.60 - 21.71) ms20.99 ± (20.95 - 21.02) ms-3.1%
process.time_to_main_ms77.17 ± (76.98 - 77.35) ms73.11 ± (72.95 - 73.28) ms-5.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.73 ± (10.72 - 10.73) MB10.75 ± (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) ms384.61 ± (382.36 - 386.85) ms-0.0%
process.time_to_main_ms539.71 ± (538.63 - 540.78) ms516.26 ± (515.33 - 517.19) ms-4.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed49.93 ± (49.89 - 49.96) MB49.91 ± (49.89 - 49.94) MB-0.0%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.1%
.NET 8 - Baseline
process.internal_duration_ms20.01 ± (19.97 - 20.06) ms19.29 ± (19.26 - 19.31) ms-3.6%
process.time_to_main_ms75.99 ± (75.83 - 76.14) ms71.61 ± (71.46 - 71.77) ms-5.8%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.66 ± (7.66 - 7.67) MB7.68 ± (7.67 - 7.68) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms20.05 ± (20.00 - 20.10) ms19.41 ± (19.37 - 19.45) ms-3.2%
process.time_to_main_ms77.45 ± (77.30 - 77.61) ms72.56 ± (72.41 - 72.70) ms-6.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.72 ± (7.71 - 7.73) MB7.72 ± (7.72 - 7.73) MB+0.1%✅⬆️
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) ms309.88 ± (307.91 - 311.85) ms+1.1%✅⬆️
process.time_to_main_ms498.65 ± (497.71 - 499.60) ms473.81 ± (472.99 - 474.63) ms-5.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.95 ± (36.93 - 36.97) MB36.98 ± (36.95 - 37.00) MB+0.1%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)+0.7%✅⬆️

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration188.70 ± (188.76 - 189.41) ms188.74 ± (188.67 - 189.39) ms+0.0%✅⬆️
.NET Framework 4.8 - Bailout
duration192.42 ± (192.25 - 192.58) ms192.24 ± (192.00 - 192.38) ms-0.1%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1128.71 ± (1128.00 - 1134.47) ms1131.57 ± (1134.03 - 1142.97) ms+0.3%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms184.42 ± (184.11 - 184.73) ms183.77 ± (183.51 - 184.04) ms-0.3%
process.time_to_main_ms79.28 ± (79.08 - 79.47) ms79.08 ± (78.92 - 79.23) ms-0.3%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.01 ± (15.93 - 16.10) MB16.07 ± (15.99 - 16.14) MB+0.3%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 20)19 ± (19 - 19)-1.7%
.NET Core 3.1 - Bailout
process.internal_duration_ms183.49 ± (183.27 - 183.71) ms183.54 ± (183.26 - 183.83) ms+0.0%✅⬆️
process.time_to_main_ms80.41 ± (80.31 - 80.50) ms80.35 ± (80.26 - 80.45) ms-0.1%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.80 ± (15.66 - 15.95) MB16.12 ± (16.06 - 16.18) MB+2.0%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)21 ± (20 - 21)+3.1%✅⬆️
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms391.01 ± (389.77 - 392.24) ms391.90 ± (390.84 - 392.96) ms+0.2%✅⬆️
process.time_to_main_ms501.90 ± (500.88 - 502.92) ms502.56 ± (501.54 - 503.58) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.21 ± (58.04 - 58.38) MB58.37 ± (58.23 - 58.51) MB+0.3%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)+0.0%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms188.03 ± (187.78 - 188.28) ms188.63 ± (188.37 - 188.89) ms+0.3%✅⬆️
process.time_to_main_ms68.93 ± (68.80 - 69.07) ms69.07 ± (68.94 - 69.20) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.55 ± (15.37 - 15.72) MB15.71 ± (15.54 - 15.89) MB+1.1%✅⬆️
runtime.dotnet.threads.count17 ± (17 - 18)17 ± (17 - 18)-0.3%
.NET 6 - Bailout
process.internal_duration_ms187.74 ± (187.60 - 187.88) ms187.30 ± (187.15 - 187.44) ms-0.2%
process.time_to_main_ms69.81 ± (69.76 - 69.85) ms69.99 ± (69.93 - 70.05) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.95 ± (15.79 - 16.11) MB15.44 ± (15.28 - 15.60) MB-3.2%
runtime.dotnet.threads.count19 ± (19 - 19)18 ± (18 - 18)-3.3%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms596.68 ± (593.95 - 599.41) ms594.92 ± (591.70 - 598.14) ms-0.3%
process.time_to_main_ms504.29 ± (503.37 - 505.22) ms502.95 ± (502.14 - 503.76) ms-0.3%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed61.44 ± (61.35 - 61.54) MB61.47 ± (61.37 - 61.56) MB+0.0%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.1%
.NET 8 - Baseline
process.internal_duration_ms185.45 ± (185.21 - 185.70) ms185.94 ± (185.66 - 186.22) ms+0.3%✅⬆️
process.time_to_main_ms68.18 ± (68.05 - 68.30) ms68.42 ± (68.29 - 68.55) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.80 ± (11.75 - 11.86) MB11.68 ± (11.59 - 11.76) MB-1.1%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (17 - 18)-3.3%
.NET 8 - Bailout
process.internal_duration_ms184.85 ± (184.67 - 185.03) ms185.29 ± (185.10 - 185.49) ms+0.2%✅⬆️
process.time_to_main_ms69.27 ± (69.21 - 69.33) ms69.30 ± (69.25 - 69.35) ms+0.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.65 ± (11.54 - 11.75) MB11.67 ± (11.57 - 11.78) MB+0.2%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 19)18 ± (18 - 19)+0.2%✅⬆️
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms523.27 ± (520.98 - 525.56) ms520.58 ± (517.83 - 523.34) ms-0.5%
process.time_to_main_ms457.03 ± (456.32 - 457.74) ms459.18 ± (458.41 - 459.95) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed50.67 ± (50.65 - 50.70) MB50.67 ± (50.64 - 50.70) MB-0.0%
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.0%
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 (8400) - mean (73ms)  : 70, 75
    master - mean (76ms)  : 73, 79

    section Bailout
    This PR (8400) - mean (76ms)  : 74, 79
    master - mean (81ms)  : 78, 83

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (1,064ms)  : 1015, 1113
    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 (8400) - mean (113ms)  : 110, 117
    master - mean (120ms)  : 116, 124

    section Bailout
    This PR (8400) - mean (114ms)  : 112, 117
    master - mean (121ms)  : 118, 124

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (775ms)  : 757, 794
    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 (8400) - mean (99ms)  : 96, 102
    master - mean (105ms)  : 100, 109

    section Bailout
    This PR (8400) - mean (100ms)  : 98, 103
    master - mean (105ms)  : 103, 108

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (928ms)  : 896, 960
    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 (8400) - mean (98ms)  : 95, 102
    master - mean (104ms)  : 101, 107

    section Bailout
    This PR (8400) - mean (100ms)  : 97, 102
    master - mean (105ms)  : 103, 108

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (813ms)  : 780, 846
    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 (8400) - mean (189ms)  : 186, 193
    master - mean (189ms)  : 186, 192

    section Bailout
    This PR (8400) - mean (192ms)  : 190, 194
    master - mean (192ms)  : 191, 194

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (1,139ms)  : 1071, 1206
    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 (8400) - mean (271ms)  : 267, 275
    master - mean (272ms)  : 268, 276

    section Bailout
    This PR (8400) - mean (272ms)  : 268, 275
    master - mean (272ms)  : 269, 275

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (925ms)  : 884, 967
    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 (8400) - mean (266ms)  : 262, 270
    master - mean (265ms)  : 261, 269

    section Bailout
    This PR (8400) - mean (265ms)  : 262, 267
    master - mean (265ms)  : 263, 267

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (1,126ms)  : 1077, 1174
    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 (8400) - mean (264ms)  : 260, 268
    master - mean (263ms)  : 259, 266

    section Bailout
    This PR (8400) - mean (264ms)  : 261, 266
    master - mean (263ms)  : 260, 266

    section CallTarget+Inlining+NGEN
    This PR (8400) - mean (1,013ms)  : 974, 1053
    master - mean (1,012ms)  : 967, 1058

Loading

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