Unrefactor peek() to use tail recursion instead of a loop

Profiling RGBDS on building pret/pokecrystal reveals commit
cfe1f60e47 as a clear outlier
among the past few hundred commits for reducing performance
(an 8% increase on my machine from ~5 to ~5.4 seconds).
This commit is contained in:
Rangi42
2025-07-29 00:17:10 -04:00
parent 2341d1ee50
commit d21e6669ce

View File

@@ -720,14 +720,17 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth);
static int peek() {
int c = lexerState->peekChar();
for (; lexerState->macroArgScanDistance == 0; c = lexerState->peekChar()) {
if (lexerState->macroArgScanDistance > 0) {
return c;
}
++lexerState->macroArgScanDistance; // Do not consider again
if (c == '\\' && !lexerState->disableMacroArgs) {
// If character is a backslash, check for a macro arg
++lexerState->macroArgScanDistance;
if (!isMacroChar(lexerState->peekCharAhead())) {
break;
return c;
}
// If character is a macro arg char, do macro arg expansion
@@ -740,16 +743,19 @@ static int peek() {
// https://en.wikipedia.org/wiki/Painted_blue
lexerState->macroArgScanDistance += str->length();
}
return peek(); // Tail recursion
} else if (c == '{' && !lexerState->disableInterpolation) {
// If character is an open brace, do symbol interpolation
shiftChar();
if (std::shared_ptr<std::string> str = readInterpolation(0); str) {
beginExpansion(str, *str);
}
}
}
return peek(); // Tail recursion
} else {
return c;
}
}
static void shiftChar() {