From c19e67eb73c8db6a2d30a1260779d3221083382c Mon Sep 17 00:00:00 2001
From: Milian Wolff <milian.wolff@kdab.com>
Date: Sat, 30 May 2015 23:19:21 +0200
Subject: 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>
---
 src/tools/moc/preprocessor.cpp | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

(limited to 'src/tools/moc/preprocessor.cpp')

diff --git a/src/tools/moc/preprocessor.cpp b/src/tools/moc/preprocessor.cpp
index f253c49995c..14850b3e0c4 100644
--- a/src/tools/moc/preprocessor.cpp
+++ b/src/tools/moc/preprocessor.cpp
@@ -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:
-- 
cgit v1.2.3