Skip to content

Commit 022009b

Browse files
bergmeisterChristoph Bergmeister
and
Christoph Bergmeister
authored
Lazy initialisation of LinkedList in TokenOperations constructor -> 7% performance gain for formatter (#1453)
* Lazy initialisation of LinkedList in TokenOperations constructor -> 7% performance gain Also remove unused method * remove another unused function Co-authored-by: Christoph Bergmeister <christoph.bergmeister@bjss.com>
1 parent bd5a4b1 commit 022009b

File tree

1 file changed

+3
-80
lines changed

1 file changed

+3
-80
lines changed

Engine/TokenOperations.cs

+3-80
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
1515
public class TokenOperations
1616
{
1717
private readonly Token[] tokens;
18-
private LinkedList<Token> tokensLL;
18+
private readonly Lazy<LinkedList<Token>> tokensLL;
1919
private readonly Ast ast;
2020

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

4040
this.tokens = tokens;
4141
this.ast = ast;
42-
this.tokensLL = new LinkedList<Token>(this.tokens);
42+
this.tokensLL = new Lazy<LinkedList<Token>>(() => new LinkedList<Token>(this.tokens));
4343
}
4444

4545
/// <summary>
@@ -150,68 +150,6 @@ private IEnumerable<Token> GetBraceInCommandElement(TokenKind tokenKind)
150150
}
151151
}
152152

153-
public IEnumerable<Token> GetCloseBraceInOneLineIfStatement()
154-
{
155-
return GetBraceInOneLineIfStatment(TokenKind.RCurly);
156-
}
157-
158-
public IEnumerable<Token> GetOpenBraceInOneLineIfStatement()
159-
{
160-
return GetBraceInOneLineIfStatment(TokenKind.LCurly);
161-
}
162-
163-
// TODO Fix code duplication in the following method and GetBraceInCommandElement
164-
private IEnumerable<Token> GetBraceInOneLineIfStatment(TokenKind tokenKind)
165-
{
166-
var ifStatementAsts = ast.FindAll(ast =>
167-
{
168-
var ifAst = ast as IfStatementAst;
169-
if (ifAst == null)
170-
{
171-
return false;
172-
}
173-
174-
return ifAst.Extent.StartLineNumber == ifAst.Extent.EndLineNumber;
175-
},
176-
true);
177-
178-
if (ifStatementAsts == null)
179-
{
180-
yield break;
181-
}
182-
183-
var braceTokens = new List<Token>();
184-
foreach (var ast in ifStatementAsts)
185-
{
186-
var ifStatementAst = ast as IfStatementAst;
187-
foreach (var clause in ifStatementAst.Clauses)
188-
{
189-
var tokenIf
190-
= tokenKind == TokenKind.LCurly
191-
? GetTokens(clause.Item2).FirstOrDefault()
192-
: GetTokens(clause.Item2).LastOrDefault();
193-
if (tokenIf != null)
194-
{
195-
yield return tokenIf;
196-
}
197-
}
198-
199-
if (ifStatementAst.ElseClause == null)
200-
{
201-
continue;
202-
}
203-
204-
var tokenElse
205-
= tokenKind == TokenKind.LCurly
206-
? GetTokens(ifStatementAst.ElseClause).FirstOrDefault()
207-
: GetTokens(ifStatementAst.ElseClause).LastOrDefault();
208-
if (tokenElse != null)
209-
{
210-
yield return tokenElse;
211-
}
212-
}
213-
}
214-
215153
public static IEnumerable<Token> GetTokens(Ast outerAst, Ast innerAst, Token[] outerTokens)
216154
{
217155
ThrowIfNull(outerAst, nameof(outerAst));
@@ -262,7 +200,7 @@ public IEnumerable<LinkedListNode<Token>> GetTokenNodes(TokenKind kind)
262200

263201
public IEnumerable<LinkedListNode<Token>> GetTokenNodes(Func<Token, bool> predicate)
264202
{
265-
var token = tokensLL.First;
203+
var token = tokensLL.Value.First;
266204
while (token != null)
267205
{
268206
if (predicate(token.Value))
@@ -290,21 +228,6 @@ private IEnumerable<Tuple<Token, int>> GetTokenAndPrecedingWhitespace(TokenKind
290228
}
291229
}
292230

293-
public IEnumerable<Tuple<Token, int>> GetOpenBracesWithWhiteSpacesBefore()
294-
{
295-
return GetTokenAndPrecedingWhitespace(TokenKind.LCurly);
296-
}
297-
298-
public IEnumerable<Tuple<Token, int>> GetOpenParensWithWhiteSpacesBefore()
299-
{
300-
return GetTokenAndPrecedingWhitespace(TokenKind.LParen);
301-
}
302-
303-
public static int GetExtentWidth(IScriptExtent extent)
304-
{
305-
return extent.EndOffset - extent.StartOffset;
306-
}
307-
308231
private bool OnSameLine(Token token1, Token token2)
309232
{
310233
return token1.Extent.StartLineNumber == token2.Extent.EndLineNumber;

0 commit comments

Comments
 (0)