From e22c177003c8159a4ddaacd0500df8262837acf2 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 24 May 2016 23:15:09 +0200 Subject: [PATCH] Optimized Tokenizer::setVarIdPass1(): Avoid costly std::stack>::push/pop() operations for struct initializers --- lib/tokenize.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0bb77afe9..2cfb91a33 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -41,13 +41,14 @@ namespace { // in order to store information about the scope struct VarIdscopeInfo { VarIdscopeInfo() - :isExecutable(false), startVarid(0) { + :isExecutable(false), isStructInit(false), startVarid(0) { } - VarIdscopeInfo(bool _isExecutable, unsigned int _startVarid) - :isExecutable(_isExecutable), startVarid(_startVarid) { + VarIdscopeInfo(bool _isExecutable, bool _isStructInit, unsigned int _startVarid) + :isExecutable(_isExecutable), isStructInit(_isStructInit), startVarid(_startVarid) { } const bool isExecutable; + const bool isStructInit; const unsigned int startVarid; }; } @@ -2662,7 +2663,7 @@ void Tokenizer::setVarIdPass1() variableId.swap(scopeInfo.top()); scopeInfo.pop(); } else if (tok->str() == "{") - scopeStack.push(VarIdscopeInfo(true, _varId)); + scopeStack.push(VarIdscopeInfo(true, scopeStack.top().isStructInit || tok->strAt(-1) == "=", _varId)); } else if (!initlist && tok->str()=="(") { const Token * newFunctionDeclEnd = nullptr; if (!scopeStack.top().isExecutable) @@ -2692,10 +2693,11 @@ void Tokenizer::setVarIdPass1() } else { isExecutable = ((scopeStack.top().isExecutable || initlist || tok->strAt(-1) == "else") && !isClassStructUnionEnumStart(tok)); - scopeInfo.push(variableId); + if (!(scopeStack.top().isStructInit || tok->strAt(-1) == "=")) + scopeInfo.push(variableId); } initlist = false; - scopeStack.push(VarIdscopeInfo(isExecutable, _varId)); + scopeStack.push(VarIdscopeInfo(isExecutable, scopeStack.top().isStructInit || tok->strAt(-1) == "=", _varId)); } else { /* if (tok->str() == "}") */ bool isNamespace = false; for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous()) @@ -2708,11 +2710,13 @@ void Tokenizer::setVarIdPass1() structMembers); } - if (scopeInfo.empty()) { - variableId.clear(); - } else { - variableId.swap(scopeInfo.top()); - scopeInfo.pop(); + if (!scopeStack.top().isStructInit) { + if (scopeInfo.empty()) { + variableId.clear(); + } else { + variableId.swap(scopeInfo.top()); + scopeInfo.pop(); + } } scopeStack.pop();