-
Notifications
You must be signed in to change notification settings - Fork 513
Make XmlCommentHelper faster #3651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0a9f7f0
51e4f00
69c87bd
a7d4102
bf9df99
7b8c31d
589f040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -268,22 +267,109 @@ internal static string GetText(XmlTextSyntax textElement, bool normalizeWhitespa | |
return null; | ||
} | ||
|
||
StringBuilder stringBuilder = StringBuilderPool.Allocate(); | ||
string result = string.Empty; | ||
|
||
StringBuilder stringBuilder = null; | ||
|
||
foreach (var item in textElement.TextTokens) | ||
{ | ||
stringBuilder.Append(item); | ||
if (result.Length == 0) | ||
{ | ||
result = item.ToString(); | ||
} | ||
else | ||
{ | ||
if (stringBuilder == null) | ||
{ | ||
stringBuilder = StringBuilderPool.Allocate(); | ||
stringBuilder.Append(result); | ||
} | ||
|
||
stringBuilder.Append(item.ToString()); | ||
} | ||
} | ||
|
||
if (stringBuilder != null) | ||
{ | ||
result = StringBuilderPool.ReturnAndFree(stringBuilder); | ||
} | ||
|
||
string result = StringBuilderPool.ReturnAndFree(stringBuilder); | ||
if (normalizeWhitespace) | ||
{ | ||
result = Regex.Replace(result, @"\s+", " "); | ||
result = result.NormalizeWhiteSpace(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
internal static string NormalizeWhiteSpace(this string text) | ||
{ | ||
if (text == null) | ||
{ | ||
return null; | ||
} | ||
|
||
int length = text.Length; | ||
|
||
bool lastSpace = false; | ||
|
||
bool diff = false; | ||
|
||
foreach (char ch in text) | ||
{ | ||
if (char.IsWhiteSpace(ch)) | ||
{ | ||
if (lastSpace) | ||
{ | ||
length--; | ||
} | ||
else | ||
{ | ||
if (ch != ' ') | ||
{ | ||
diff = true; | ||
} | ||
|
||
lastSpace = true; | ||
} | ||
} | ||
else | ||
{ | ||
lastSpace = false; | ||
} | ||
} | ||
|
||
if (diff || (length != text.Length)) | ||
{ | ||
char[] buffer = new char[length]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you probably know, if this code was targeting .NET Core (or conditionally compiled thus) you could eliminate this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but this project is targeting multiple runtimes. |
||
|
||
lastSpace = false; | ||
|
||
length = 0; | ||
|
||
foreach (char ch in text) | ||
{ | ||
if (char.IsWhiteSpace(ch)) | ||
{ | ||
if (!lastSpace) | ||
{ | ||
buffer[length++] = ' '; | ||
lastSpace = true; | ||
} | ||
} | ||
else | ||
{ | ||
buffer[length++] = ch; | ||
lastSpace = false; | ||
} | ||
} | ||
|
||
return new string(buffer, 0, length); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
internal static T GetFirstAttributeOrDefault<T>(XmlNodeSyntax nodeSyntax) | ||
where T : XmlAttributeSyntax | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, @stephentoub on the latest .NET (of course this is broader targeted) would you expect regex replace using pre generated regex to have comparable performance to open coded?
I didn't measure, but fwiw
generates code in part like
the code below hides some of this inside
char.IsWhiteSpace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and will return the same string if there are no non-space whitespace chars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After addressing the single line issue and regular expression issue, more optimizations could be having much less return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Note that in .NET 8, the generated code now looks like this:
where that IndexOfAnyWhitespace is emitted as:
such that the search for the whitespace is vectorized. Then the matching logic for finding all the contiguous whitespace currently looks like:
(though we're flirting with a change that'll result in that loop also changing to similarly be vectorized with IndexOfAnyExcept).
Replace itself also gets better in .NET 8.