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

Lazy initialisation of LinkedList in TokenOperations constructor -> 7% performance gain for formatter #1453

Merged
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
83 changes: 3 additions & 80 deletions Engine/TokenOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
public class TokenOperations
{
private readonly Token[] tokens;
private LinkedList<Token> tokensLL;
private readonly Lazy<LinkedList<Token>> tokensLL;
private readonly Ast ast;

public Ast Ast { get { return ast; } }
Expand All @@ -39,7 +39,7 @@ public TokenOperations(Token[] tokens, Ast ast)

this.tokens = tokens;
this.ast = ast;
this.tokensLL = new LinkedList<Token>(this.tokens);
this.tokensLL = new Lazy<LinkedList<Token>>(() => new LinkedList<Token>(this.tokens));
}

/// <summary>
Expand Down Expand Up @@ -150,68 +150,6 @@ private IEnumerable<Token> GetBraceInCommandElement(TokenKind tokenKind)
}
}

public IEnumerable<Token> GetCloseBraceInOneLineIfStatement()
{
return GetBraceInOneLineIfStatment(TokenKind.RCurly);
}

public IEnumerable<Token> GetOpenBraceInOneLineIfStatement()
{
return GetBraceInOneLineIfStatment(TokenKind.LCurly);
}

// TODO Fix code duplication in the following method and GetBraceInCommandElement
private IEnumerable<Token> GetBraceInOneLineIfStatment(TokenKind tokenKind)
{
var ifStatementAsts = ast.FindAll(ast =>
{
var ifAst = ast as IfStatementAst;
if (ifAst == null)
{
return false;
}

return ifAst.Extent.StartLineNumber == ifAst.Extent.EndLineNumber;
},
true);

if (ifStatementAsts == null)
{
yield break;
}

var braceTokens = new List<Token>();
foreach (var ast in ifStatementAsts)
{
var ifStatementAst = ast as IfStatementAst;
foreach (var clause in ifStatementAst.Clauses)
{
var tokenIf
= tokenKind == TokenKind.LCurly
? GetTokens(clause.Item2).FirstOrDefault()
: GetTokens(clause.Item2).LastOrDefault();
if (tokenIf != null)
{
yield return tokenIf;
}
}

if (ifStatementAst.ElseClause == null)
{
continue;
}

var tokenElse
= tokenKind == TokenKind.LCurly
? GetTokens(ifStatementAst.ElseClause).FirstOrDefault()
: GetTokens(ifStatementAst.ElseClause).LastOrDefault();
if (tokenElse != null)
{
yield return tokenElse;
}
}
}

public static IEnumerable<Token> GetTokens(Ast outerAst, Ast innerAst, Token[] outerTokens)
{
ThrowIfNull(outerAst, nameof(outerAst));
Expand Down Expand Up @@ -262,7 +200,7 @@ public IEnumerable<LinkedListNode<Token>> GetTokenNodes(TokenKind kind)

public IEnumerable<LinkedListNode<Token>> GetTokenNodes(Func<Token, bool> predicate)
{
var token = tokensLL.First;
var token = tokensLL.Value.First;
while (token != null)
{
if (predicate(token.Value))
Expand Down Expand Up @@ -290,21 +228,6 @@ private IEnumerable<Tuple<Token, int>> GetTokenAndPrecedingWhitespace(TokenKind
}
}

public IEnumerable<Tuple<Token, int>> GetOpenBracesWithWhiteSpacesBefore()
{
return GetTokenAndPrecedingWhitespace(TokenKind.LCurly);
}

public IEnumerable<Tuple<Token, int>> GetOpenParensWithWhiteSpacesBefore()
{
return GetTokenAndPrecedingWhitespace(TokenKind.LParen);
}

public static int GetExtentWidth(IScriptExtent extent)
{
return extent.EndOffset - extent.StartOffset;
}

private bool OnSameLine(Token token1, Token token2)
{
return token1.Extent.StartLineNumber == token2.Extent.EndLineNumber;
Expand Down