Skip to content

Commit 9ba46cd

Browse files
committed
Improve performance of IsPrecededByWhitespace
1 parent bd2f647 commit 9ba46cd

13 files changed

Lines changed: 21 additions & 18 deletions

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/TokenHelper.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.Helpers
55
{
6+
using System.Threading;
67
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.CSharp;
89

@@ -61,17 +62,19 @@ internal static bool IsLastInLine(this SyntaxToken token)
6162
/// Gets a value indicating whether the <paramref name="token"/> is preceded by a whitespace.
6263
/// </summary>
6364
/// <param name="token">The token to process.</param>
65+
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
6466
/// <returns>true if token is preceded by a whitespace, otherwise false.</returns>
65-
internal static bool IsPrecededByWhitespace(this SyntaxToken token)
67+
internal static bool IsPrecededByWhitespace(this SyntaxToken token, CancellationToken cancellationToken)
6668
{
67-
SyntaxTriviaList triviaList = token.LeadingTrivia;
68-
if (triviaList.Count > 0)
69+
// Perf directly access the text instead of trivia
70+
int pos = token.Span.Start - 1;
71+
72+
if (pos < 0 || token.SyntaxTree == null)
6973
{
70-
return triviaList.Last().IsKind(SyntaxKind.WhitespaceTrivia);
74+
return false;
7175
}
7276

73-
triviaList = token.GetPreviousToken().TrailingTrivia;
74-
return triviaList.Count > 0 && triviaList.Last().IsKind(SyntaxKind.WhitespaceTrivia);
77+
return char.IsWhiteSpace(token.SyntaxTree.GetText(cancellationToken)[pos]);
7578
}
7679

7780
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo
9999
}
100100
}
101101

102-
if (token.IsFirstInLine() || token.IsPrecededByWhitespace())
102+
if (token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken))
103103
{
104104
// comma must{ not} be {preceded} by whitespace
105105
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePrecedingPreserveLayout, " not", "preceded"));

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1009ClosingParenthesisMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
7979
return;
8080
}
8181

82-
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
82+
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
8383
bool followedBySpace = token.IsFollowedByWhitespace();
8484
bool lastInLine = token.IsLastInLine();
8585
bool precedesStickyCharacter;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
7979

8080
if (!firstInLine)
8181
{
82-
precededBySpace = token.IsPrecededByWhitespace();
82+
precededBySpace = token.IsPrecededByWhitespace(context.CancellationToken);
8383

8484
// ignore if handled by SA1026
8585
ignorePrecedingSpaceProblem = precededBySpace && token.GetPreviousToken().IsKind(SyntaxKind.NewKeyword);

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
8585
}
8686

8787
bool firstInLine = token.IsFirstInLine();
88-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
88+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
8989
bool followedBySpace = token.IsFollowedByWhitespace();
9090
bool lastInLine = token.IsLastInLine();
9191
bool precedesSpecialCharacter;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1012OpeningBracesMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
9191
return;
9292
}
9393

94-
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
94+
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
9595

9696
if (!precededBySpace)
9797
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1013ClosingBracesMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
7676
return;
7777
}
7878

79-
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
79+
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
8080

8181
if (token.Parent is InterpolationSyntax)
8282
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1014OpeningGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static void HandleLessThanToken(SyntaxTreeAnalysisContext context, Synta
8484
}
8585

8686
bool firstInLine = token.IsFirstInLine();
87-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
87+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
8888
bool followedBySpace = token.IsFollowedByWhitespace();
8989

9090
if (!firstInLine && precededBySpace)

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy
8787

8888
bool firstInLine = token.IsFirstInLine();
8989
bool lastInLine = token.IsLastInLine();
90-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
90+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
9191
bool followedBySpace = token.IsFollowedByWhitespace();
9292
bool allowTrailingNoSpace;
9393
bool allowTrailingSpace;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1019MemberAccessSymbolsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static void HandleQuestionToken(SyntaxTreeAnalysisContext context, Synta
101101
private static void HandleMemberAccessSymbol(SyntaxTreeAnalysisContext context, SyntaxToken token)
102102
{
103103
bool firstInLine = token.IsFirstInLine();
104-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
104+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
105105
bool followedBySpace = token.IsFollowedByWhitespace();
106106

107107
if (!firstInLine && precededBySpace)

0 commit comments

Comments
 (0)