Skip to content
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

Produce less string allocs while formatting documents #73452

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private List<IndentBlockOperation> GetParentIndentBlockOperations(SyntaxToken to
allNodes.Do(n => _formattingRules.AddIndentBlockOperations(list, n));

// sort them in right order
list.RemoveAll(CommonFormattingHelpers.IsNull);
list.RemoveAll(static o => o is null);
list.Sort(CommonFormattingHelpers.IndentBlockOperationComparer);

return list;
Expand Down Expand Up @@ -296,7 +296,7 @@ private SyntaxToken GetAlignmentBaseTokenFor(SyntaxToken token)
}

// well, found no appropriate one
list.RemoveAll(CommonFormattingHelpers.IsNull);
list.RemoveAll(static o => o is null);
if (list.Count == 0)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -53,7 +54,8 @@ public static bool ContainsElasticTrivia(this SuppressOperation operation, Token
var endToken = tokenStream.GetTokenData(operation.EndToken);
var previousToken = endToken.GetPreviousTokenData();

return tokenStream.GetTriviaData(startToken, nextToken).TreatAsElastic || tokenStream.GetTriviaData(previousToken, endToken).TreatAsElastic;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting TriviaData is not cheap. It allocates up front (The substring between tokesn). I looked into changing that, but it was non-trivial. so thsi was the expedient solution.

return CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(startToken.Token, nextToken.Token) ||
CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(previousToken.Token, endToken.Token);
}

public static bool HasAnyWhitespaceElasticTrivia(this SyntaxTriviaList list)
Expand All @@ -62,9 +64,7 @@ public static bool HasAnyWhitespaceElasticTrivia(this SyntaxTriviaList list)
foreach (var trivia in list)
{
if (trivia.IsElastic())
{
return true;
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,15 @@ public static int GetStartPositionOfSpan(SyntaxToken token)

public static bool HasAnyWhitespaceElasticTrivia(SyntaxToken previousToken, SyntaxToken currentToken)
{
if ((!previousToken.ContainsAnnotations && !currentToken.ContainsAnnotations) ||
(!previousToken.HasTrailingTrivia && !currentToken.HasLeadingTrivia))
{
if (!previousToken.ContainsAnnotations && !currentToken.ContainsAnnotations)
return false;

if (!previousToken.HasTrailingTrivia && !currentToken.HasLeadingTrivia)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just formatting so i could understand the code.

return false;
}

return previousToken.TrailingTrivia.HasAnyWhitespaceElasticTrivia() || currentToken.LeadingTrivia.HasAnyWhitespaceElasticTrivia();
}

public static bool IsNull<T>(T t) where T : class
=> t == null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly pointless. so inlined.


public static bool IsNotNull<T>(T t) where T : class
=> !IsNull(t);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not called.


public static TextSpan GetFormattingSpan(SyntaxNode root, TextSpan span)
{
Contract.ThrowIfNull(root);
Expand Down
Loading