Skip to content

Commit

Permalink
Cache next character queue count
Browse files Browse the repository at this point in the history
Saves up to 10% by not continuously querying the object when reading the
next character
  • Loading branch information
nickbabcock committed Jul 11, 2014
1 parent 46ab6dd commit e08dfa5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Pdoxcl2Sharp/ParadoxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ParadoxParser
private LexerToken currentToken;
private LexerToken? nextToken;
private Queue<char> nextChars = new Queue<char>();
private bool nextCharsEmpty = true;
private char currentChar;
private int currentPosition;
private int bufferSize;
Expand Down Expand Up @@ -719,6 +720,7 @@ public bool NextIsBracketed()
}
} while ((tempToken == LexerToken.Equals || IsSpace(tempChar)) && !eof);

nextCharsEmpty &= tempQueue.Count == 0;
while (tempQueue.Count > 0)
nextChars.Enqueue(tempQueue.Dequeue());
}
Expand All @@ -734,8 +736,12 @@ public bool NextIsBracketed()
/// <returns>The next character in the buffer or '\0' if the end of the stream was reached</returns>
private char ReadNext()
{
if (nextChars.Count > 0)
return nextChars.Dequeue();
if (!nextCharsEmpty)
{
char result = nextChars.Dequeue();
nextCharsEmpty = nextChars.Count == 0;
return result;
}

if (currentPosition == bufferSize)
{
Expand Down

0 comments on commit e08dfa5

Please sign in to comment.