Skip to content

Commit

Permalink
Use pooled List<SyntaxToken> rather than allocating new one
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinCampbell committed Mar 18, 2024
1 parent 938b8c4 commit c36b8d2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Expand Up @@ -67,15 +67,15 @@ public void LookaheadUntil_PassesThePreviousTokensInTheSameOrder()

// Act
var i = 3;
IEnumerable<SyntaxToken> previousTokens = null;
SyntaxToken[] previousTokens = null;
var tokenFound = tokenizer.LookaheadUntil((s, p) =>
{
previousTokens = p;
previousTokens = p.ToArray();
return --i == 0;
});

// Assert
Assert.Equal(4, previousTokens.Count());
Assert.Equal(4, previousTokens.Length);

// For the very first element, there will be no previous items, so null is expected
var orderIndex = 0;
Expand Down
Expand Up @@ -101,17 +101,19 @@ protected SyntaxToken Lookahead(int count)
return CurrentToken;
}

using var _ = ListPool<SyntaxToken>.GetPooledObject(out var tokens);

// We add 1 in order to store the current token.
var tokens = new SyntaxToken[count + 1];
tokens.SetCapacityIfLarger(count + 1);
var currentToken = CurrentToken;

tokens[0] = currentToken;
tokens.Add(currentToken);

// We need to look forward "count" many times.
for (var i = 1; i <= count; i++)
{
NextToken();
tokens[i] = CurrentToken;
tokens.Add(CurrentToken);
}

// Restore Tokenizer's location to where it was pointing before the look-ahead.
Expand Down Expand Up @@ -142,7 +144,7 @@ protected bool LookaheadUntil(Func<SyntaxToken, IEnumerable<SyntaxToken>, bool>

var matchFound = false;

var tokens = new List<SyntaxToken>();
using var _ = ListPool<SyntaxToken>.GetPooledObject(out var tokens);
tokens.Add(CurrentToken);

while (true)
Expand Down

0 comments on commit c36b8d2

Please sign in to comment.