[Analyzers] Add analyzer to detect new StringBuilder() instead of StringBuilderCache#8400
Draft
lucaspimentel wants to merge 12 commits intomasterfrom
Draft
[Analyzers] Add analyzer to detect new StringBuilder() instead of StringBuilderCache#8400lucaspimentel wants to merge 12 commits intomasterfrom
new StringBuilder() instead of StringBuilderCache#8400lucaspimentel wants to merge 12 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>
…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>
new StringBuilder() instead of StringBuilderCache
Disabled DDALLOC003 warning until existing usages are migrated.
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (8400) 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 (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
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
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
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
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
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
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
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 (
DDALLOC003) that detectsnew StringBuilder()allocations and suggests usingStringBuilderCache.Acquire()instead.StringBuilderCacheuses a[ThreadStatic]cached instance to avoid heap allocations for short-lived builders.Reason for change
StringBuilderCacheexists in the codebase but there's no automated enforcement. Developers may usenew StringBuilder()out of habit, missing the opportunity to reuse cached instances and reduce GC pressure.Implementation details
StringBuilderCacheAnalyzer): flagsnew StringBuilder()andnew StringBuilder(capacity)usages.editorconfig:isEnabledByDefault: false, enabled aswarningintracer/src/Datadog.Trace/.editorconfigStringBuilderCacheclass itselfStringBuilderCache.Acquire()StringBuilderallocations (cache only holds one per thread)#pragmasuppressions for remaining edge cases:StringBuilderAspects(intentionally replacing customer allocations)BsonSerializationHelpersmoke test (ownership transferred to external API)Test coverage
18 unit tests covering:
StringBuilderCacheclass exclusionStringBuilderCache.Acquire()suppressionOther details
The analyzer is intentionally shipped disabled. To enable, add to
.editorconfig: