Optimize moc: Remove temporary allocations during macro expansion.

Previously, a temporary list was allocated and then fed into the
bigger list of results. Now, we push data into the final list
directly, removing the overhead of the temporary allocation.

Change-Id: I9bea0fd3c23b1434b4be2728c60ac22a66908efc
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
bb10
Milian Wolff 2015-05-30 23:19:21 +02:00 committed by Marc Mutz
parent 4efd2ebb27
commit c19e67eb73
2 changed files with 7 additions and 10 deletions

View File

@ -529,7 +529,7 @@ Symbols Preprocessor::tokenize(const QByteArray& input, int lineNum, Preprocesso
return symbols;
}
Symbols Preprocessor::macroExpand(Preprocessor *that, Symbols &toExpand, int &index,
void Preprocessor::macroExpand(Symbols *into, Preprocessor *that, Symbols &toExpand, int &index,
int lineNum, bool one, const QSet<QByteArray> &excludeSymbols)
{
SymbolStack symbols;
@ -539,16 +539,15 @@ Symbols Preprocessor::macroExpand(Preprocessor *that, Symbols &toExpand, int &in
sf.excludedSymbols = excludeSymbols;
symbols.push(sf);
Symbols result;
if (toExpand.isEmpty())
return result;
return;
for (;;) {
QByteArray macro;
Symbols newSyms = macroExpandIdentifier(that, symbols, lineNum, &macro);
if (macro.isEmpty()) {
result += newSyms;
*into += newSyms;
} else {
SafeSymbols sf;
sf.symbols = newSyms;
@ -565,8 +564,6 @@ Symbols Preprocessor::macroExpand(Preprocessor *that, Symbols &toExpand, int &in
index = symbols.top().index;
else
index = toExpand.size();
return result;
}
@ -653,7 +650,7 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym
if (i == macro.symbols.size() - 1 || macro.symbols.at(i + 1).token != PP_HASHHASH) {
Symbols arg = arguments.at(index);
int idx = 1;
expansion += macroExpand(that, arg, idx, lineNum, false, symbols.excludeSymbols());
macroExpand(&expansion, that, arg, idx, lineNum, false, symbols.excludeSymbols());
} else {
expansion += arguments.at(index);
}
@ -726,7 +723,7 @@ void Preprocessor::substituteUntilNewline(Symbols &substituted)
while (hasNext()) {
Token token = next();
if (token == PP_IDENTIFIER) {
substituted += macroExpand(this, symbols, index, symbol().lineNum, true);
macroExpand(&substituted, this, symbols, index, symbol().lineNum, true);
} else if (token == PP_DEFINED) {
bool braces = test(PP_LPAREN);
next(PP_IDENTIFIER);
@ -1148,7 +1145,7 @@ void Preprocessor::preprocess(const QByteArray &filename, Symbols &preprocessed)
}
case PP_IDENTIFIER: {
// substitute macros
preprocessed += macroExpand(this, symbols, index, symbol().lineNum, true);
macroExpand(&preprocessed, this, symbols, index, symbol().lineNum, true);
continue;
}
case PP_HASH:

View File

@ -76,7 +76,7 @@ public:
void substituteUntilNewline(Symbols &substituted);
static Symbols macroExpandIdentifier(Preprocessor *that, SymbolStack &symbols, int lineNum, QByteArray *macroName);
static Symbols macroExpand(Preprocessor *that, Symbols &toExpand, int &index, int lineNum, bool one,
static void macroExpand(Symbols *into, Preprocessor *that, Symbols &toExpand, int &index, int lineNum, bool one,
const QSet<QByteArray> &excludeSymbols = QSet<QByteArray>());
int evaluateCondition();