From e08dfa5863c971a2e1faf427d26b4fef85ac6b0c Mon Sep 17 00:00:00 2001 From: Nick Babcock Date: Fri, 11 Jul 2014 18:59:38 -0500 Subject: [PATCH] Cache next character queue count Saves up to 10% by not continuously querying the object when reading the next character --- Pdoxcl2Sharp/ParadoxParser.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Pdoxcl2Sharp/ParadoxParser.cs b/Pdoxcl2Sharp/ParadoxParser.cs index 9035d19..35f3bc8 100644 --- a/Pdoxcl2Sharp/ParadoxParser.cs +++ b/Pdoxcl2Sharp/ParadoxParser.cs @@ -47,6 +47,7 @@ public class ParadoxParser private LexerToken currentToken; private LexerToken? nextToken; private Queue nextChars = new Queue(); + private bool nextCharsEmpty = true; private char currentChar; private int currentPosition; private int bufferSize; @@ -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()); } @@ -734,8 +736,12 @@ public bool NextIsBracketed() /// The next character in the buffer or '\0' if the end of the stream was reached 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) {