Skip to content

Commit 19ef919

Browse files
committed
Use the string builder cache when possible
1 parent 688c157 commit 19ef919

9 files changed

Lines changed: 26 additions & 17 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/FileHeaderCodeFixProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace StyleCop.Analyzers.DocumentationRules
1818
using Microsoft.CodeAnalysis.Formatting;
1919
using StyleCop.Analyzers.Helpers;
2020
using StyleCop.Analyzers.Settings.ObjectModel;
21+
using Helpers.ObjectPools;
2122

2223
/// <summary>
2324
/// Implements a code fix for file header diagnostics.
@@ -135,7 +136,7 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
135136

136137
// Pad line that used to be next to a /*
137138
triviaStringParts[0] = commentIndentation + interlinePadding + " " + triviaStringParts[0];
138-
StringBuilder sb = new StringBuilder();
139+
StringBuilder sb = StringBuilderPool.Allocate();
139140
var copyrightText = commentIndentation + interlinePadding + " " +
140141
GetCopyrightText(commentIndentation + interlinePadding, settings.DocumentationRules.CopyrightText, newLineText);
141142
var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, document.Name, settings, newLineText);
@@ -196,8 +197,7 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
196197
sb.Append((i == 0 ? string.Empty : newLineText) + lines[i].TrimEnd());
197198
}
198199

199-
var newTrivia = SyntaxFactory.SyntaxTrivia(SyntaxKind.MultiLineCommentTrivia, sb.ToString());
200-
200+
var newTrivia = SyntaxFactory.SyntaxTrivia(SyntaxKind.MultiLineCommentTrivia, StringBuilderPool.ReturnAndFree(sb));
201201
return root.WithLeadingTrivia(trivia.Replace(commentTrivia, newTrivia));
202202
}
203203

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/FileHeaderHelpers.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Helpers
1010
using System.Xml.Linq;
1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CSharp;
13+
using ObjectPools;
1314

1415
/// <summary>
1516
/// Helper class used for working with file headers
@@ -30,7 +31,7 @@ internal static FileHeader ParseFileHeader(SyntaxNode root)
3031
return FileHeader.MissingFileHeader;
3132
}
3233

33-
var sb = new StringBuilder();
34+
var sb = StringBuilderPool.Allocate();
3435
var endOfLineCount = 0;
3536
var done = false;
3637
var fileHeaderStart = int.MaxValue;
@@ -92,6 +93,7 @@ internal static FileHeader ParseFileHeader(SyntaxNode root)
9293

9394
if (fileHeaderStart > fileHeaderEnd)
9495
{
96+
StringBuilderPool.Free(sb);
9597
return FileHeader.MissingFileHeader;
9698
}
9799

@@ -102,7 +104,7 @@ internal static FileHeader ParseFileHeader(SyntaxNode root)
102104
sb.Remove(sb.Length - eolLength, eolLength);
103105
}
104106

105-
return new FileHeader(sb.ToString(), fileHeaderStart, fileHeaderEnd);
107+
return new FileHeader(StringBuilderPool.ReturnAndFree(sb), fileHeaderStart, fileHeaderEnd);
106108
}
107109

108110
/// <summary>
@@ -162,7 +164,7 @@ internal static XmlFileHeader ParseXmlFileHeader(SyntaxNode root)
162164

163165
private static string ProcessSingleLineCommentsHeader(SyntaxTriviaList triviaList, int startIndex, out int fileHeaderStart, out int fileHeaderEnd)
164166
{
165-
var sb = new StringBuilder();
167+
var sb = StringBuilderPool.Allocate();
166168
var endOfLineCount = 0;
167169
var done = false;
168170

@@ -212,12 +214,12 @@ private static string ProcessSingleLineCommentsHeader(SyntaxTriviaList triviaLis
212214
}
213215

214216
sb.AppendLine("</root>");
215-
return sb.ToString();
217+
return StringBuilderPool.ReturnAndFree(sb);
216218
}
217219

218220
private static string ProcessMultiLineCommentsHeader(SyntaxTrivia multiLineComment, out int fileHeaderStart, out int fileHeaderEnd)
219221
{
220-
var sb = new StringBuilder();
222+
var sb = StringBuilderPool.Allocate();
221223

222224
// wrap the XML from the file header in a single root element to make XML parsing work.
223225
sb.AppendLine("<root>");
@@ -237,7 +239,7 @@ private static string ProcessMultiLineCommentsHeader(SyntaxTrivia multiLineComme
237239
}
238240

239241
sb.AppendLine("</root>");
240-
return sb.ToString();
242+
return StringBuilderPool.ReturnAndFree(sb);
241243
}
242244
}
243245
}

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/NameSyntaxHelpers.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Helpers
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
using ObjectPools;
1011

1112
/// <summary>
1213
/// Class containing the extension methods for the <see cref="NameSyntax"/> class.
@@ -36,11 +37,11 @@ internal static string ToNormalizedString(this NameSyntax nameSyntax)
3637
/// <returns>The name contained in the <see cref="NameSyntax"/>, with its alias removed (if any).</returns>
3738
internal static string ToUnaliasedString(this NameSyntax nameSyntax)
3839
{
39-
var sb = new StringBuilder();
40+
var sb = StringBuilderPool.Allocate();
4041

4142
BuildName(nameSyntax, sb, false);
4243

43-
return sb.ToString();
44+
return StringBuilderPool.ReturnAndFree(sb);
4445
}
4546

4647
private static void BuildName(NameSyntax nameSyntax, StringBuilder builder, bool includeAlias)

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/ObjectPools/Extensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4+
// This code was copied from the Roslyn code base (and slightly modified)
45
using System.Collections.Generic;
56
using System.Text;
67

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/ObjectPools/ObjectPool.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4+
// This code was copied from the Roslyn code base (and slightly modified)
45
using System;
56
using System.Diagnostics;
67
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/ObjectPools/PooledObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4+
// This code was copied from the Roslyn code base (and slightly modified)
45
using System;
56
using System.Collections.Generic;
67
using System.Text;

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/XmlCommentHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Helpers
1010
using System.Text.RegularExpressions;
1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
13+
using ObjectPools;
1314

1415
/// <summary>
1516
/// Provides helper methods to work with XML comments
@@ -170,14 +171,14 @@ internal static string GetText(XmlTextSyntax textElement, bool normalizeWhitespa
170171
return null;
171172
}
172173

173-
StringBuilder stringBuilder = new StringBuilder();
174+
StringBuilder stringBuilder = StringBuilderPool.Allocate();
174175

175176
foreach (var item in textElement.TextTokens)
176177
{
177178
stringBuilder.Append(item);
178179
}
179180

180-
string result = stringBuilder.ToString();
181+
string result = StringBuilderPool.ReturnAndFree(stringBuilder);
181182
if (normalizeWhitespace)
182183
{
183184
result = Regex.Replace(result, @"\s+", " ");

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1310CodeFixProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.NamingRules
1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CodeActions;
1313
using Microsoft.CodeAnalysis.CodeFixes;
14+
using Helpers.ObjectPools;
1415

1516
/// <summary>
1617
/// Implements a code fix for <see cref="SA1310FieldNamesMustNotContainUnderscore"/>.
@@ -58,7 +59,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5859

5960
private static string BuildProposedName(string currentName)
6061
{
61-
StringBuilder builder = new StringBuilder(currentName.Length);
62+
StringBuilder builder = StringBuilderPool.Allocate();
6263

6364
bool foundNonUnderscore = false;
6465
bool capitalizeNextLetter = false;
@@ -93,7 +94,7 @@ private static string BuildProposedName(string currentName)
9394
capitalizeNextLetter = true;
9495
}
9596

96-
return builder.ToString();
97+
return StringBuilderPool.ReturnAndFree(builder);
9798
}
9899
}
99100
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027CodeFixProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace StyleCop.Analyzers.SpacingRules
1515
using Microsoft.CodeAnalysis.CodeFixes;
1616
using Microsoft.CodeAnalysis.Text;
1717
using StyleCop.Analyzers.Helpers;
18+
using Helpers.ObjectPools;
1819

1920
/// <summary>
2021
/// Implements a code fix for <see cref="SA1027TabsMustNotBeUsed"/>.
@@ -62,7 +63,7 @@ private static TextChange FixDiagnostic(IndentationOptions indentationOptions, S
6263

6364
TextLine startLine = sourceText.Lines.GetLineFromPosition(span.Start);
6465
string text = sourceText.ToString(TextSpan.FromBounds(startLine.Start, span.End));
65-
StringBuilder replacement = new StringBuilder(indentationOptions.TabSize * span.Length);
66+
StringBuilder replacement = StringBuilderPool.Allocate();
6667
int column = 0;
6768
for (int i = 0; i < text.Length; i++)
6869
{
@@ -97,7 +98,7 @@ private static TextChange FixDiagnostic(IndentationOptions indentationOptions, S
9798
}
9899
}
99100

100-
return new TextChange(span, replacement.ToString());
101+
return new TextChange(span, StringBuilderPool.ReturnAndFree(replacement));
101102
}
102103

103104
private class FixAll : DocumentBasedFixAllProvider

0 commit comments

Comments
 (0)