Skip to content

Commit

Permalink
Merge pull request #3651 from ninedan/fyuan_GetText
Browse files Browse the repository at this point in the history
Make XmlCommentHelper faster
  • Loading branch information
sharwell authored May 18, 2023
2 parents 5d98796 + 589f040 commit abf41d7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,11 @@ protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, Styl
continue;
}

if (documentationTexts.Contains(documentation))
if (!documentationTexts.Add(documentation))
{
// Add violation
context.ReportDiagnostic(Diagnostic.Create(Descriptor, documentationSyntax.GetLocation()));
}
else
{
documentationTexts.Add(documentation);
}
}

objectPool.ClearAndFree(documentationTexts);
Expand Down Expand Up @@ -148,15 +144,11 @@ protected override void HandleCompleteDocumentation(SyntaxNodeAnalysisContext co
continue;
}

if (documentationTexts.Contains(documentation))
if (!documentationTexts.Add(documentation))
{
// Add violation
context.ReportDiagnostic(Diagnostic.Create(Descriptor, diagnosticLocations.First()));
}
else
{
documentationTexts.Add(documentation);
}
}

objectPool.ClearAndFree(documentationTexts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public static void Free(StringBuilder builder)

public static string ReturnAndFree(StringBuilder builder)
{
SharedPools.Default<StringBuilder>();
return builder.ToString();
string result = builder.ToString();

StringBuilderPool.Free(builder);

return result;
}
}
}
92 changes: 86 additions & 6 deletions StyleCop.Analyzers/StyleCop.Analyzers/Helpers/XmlCommentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace StyleCop.Analyzers.Helpers
{
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -268,20 +267,101 @@ internal static string GetText(XmlTextSyntax textElement, bool normalizeWhitespa
return null;
}

StringBuilder stringBuilder = StringBuilderPool.Allocate();
bool lastWhitespace = false;

string single = string.Empty;

StringBuilder stringBuilder = null;

foreach (var item in textElement.TextTokens)
{
stringBuilder.Append(item);
if (single.Length == 0)
{
single = item.ToString();
}
else
{
if (stringBuilder == null)
{
stringBuilder = StringBuilderPool.Allocate();
stringBuilder.AppendNormalize(single, normalizeWhitespace, ref lastWhitespace);
}

stringBuilder.AppendNormalize(item.ToString(), normalizeWhitespace, ref lastWhitespace);
}
}

string result = StringBuilderPool.ReturnAndFree(stringBuilder);
if (stringBuilder == null)
{
if (normalizeWhitespace)
{
stringBuilder = StringBuilderPool.Allocate();

if (!stringBuilder.AppendNormalize(single, normalizeWhitespace, ref lastWhitespace))
{
StringBuilderPool.Free(stringBuilder);

// No change is needed, return original string.
return single;
}
}
else
{
return single;
}
}

return StringBuilderPool.ReturnAndFree(stringBuilder);
}

/// <summary>
/// Append to StringBuilder and perform white space normalization.
/// </summary>
/// <param name="builder">StringBuilder to append to.</param>
/// <param name="text">String to append.</param>
/// <param name="normalizeWhitespace">Normalize flag.</param>
/// <param name="lastWhitespace">last char is white space flag.</param>
/// <returns>True if output is different.</returns>
internal static bool AppendNormalize(this StringBuilder builder, string text, bool normalizeWhitespace, ref bool lastWhitespace)
{
bool diff = false;

if (normalizeWhitespace)
{
result = Regex.Replace(result, @"\s+", " ");
foreach (char ch in text)
{
if (char.IsWhiteSpace(ch))
{
if (lastWhitespace)
{
diff = true;
}
else
{
if (ch != ' ')
{
diff = true;
}

builder.Append(' ');
}

lastWhitespace = true;
}
else
{
builder.Append(ch);

lastWhitespace = false;
}
}
}
else
{
builder.Append(text);
}

return result;
return diff;
}

internal static T GetFirstAttributeOrDefault<T>(XmlNodeSyntax nodeSyntax)
Expand Down

0 comments on commit abf41d7

Please sign in to comment.