Cache template name position to improve performance

This improves the performance of the templatesimplefier by caching the template name position. I am not sure if the works entirely correctly but all the tests do pass with this change. Running this with gtest headers without removing unused template headers the time went from 48s to 5s, almost a 10x improvement.
This commit is contained in:
Paul Fultz II 2019-05-25 10:04:01 +02:00 committed by Daniel Marjamäki
parent 2e93df13cf
commit eb1c9ba357
2 changed files with 10 additions and 3 deletions

View File

@ -1289,6 +1289,10 @@ bool TemplateSimplifier::getTemplateNamePositionTemplateVariable(const Token *to
int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
{
auto it = mTemplateNamePos.find(tok);
if (!mSettings->debugtemplate && it != mTemplateNamePos.end()) {
return it->second;
}
// get the position of the template name
int namepos = 0;
if (Token::Match(tok, "> class|struct|union %type% :|<|;|{"))
@ -1298,8 +1302,8 @@ int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
else if (getTemplateNamePositionTemplateVariable(tok, namepos))
;
else if (!getTemplateNamePositionTemplateFunction(tok, namepos))
return -1; // Name not found
namepos = -1; // Name not found
mTemplateNamePos[tok] = namepos;
return namepos;
}
@ -3080,6 +3084,7 @@ void TemplateSimplifier::simplifyTemplates(
mTemplateInstantiations.clear();
mInstantiatedTemplates.clear();
mExplicitInstantiationsToDelete.clear();
mTemplateNamePos.clear();
}
bool hasTemplates = getTemplateDeclarations();

View File

@ -29,6 +29,7 @@
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
class ErrorLogger;
@ -199,7 +200,7 @@ public:
* @return -1 to bail out or positive integer to identity the position
* of the template name.
*/
static int getTemplateNamePosition(const Token *tok);
int getTemplateNamePosition(const Token *tok);
/**
* Get function template name position
@ -425,6 +426,7 @@ private:
std::list<TokenAndName> mMemberFunctionsToDelete;
std::vector<TokenAndName> mExplicitInstantiationsToDelete;
std::vector<TokenAndName> mTypesUsedInTemplateInstantiation;
std::unordered_map<const Token*, int> mTemplateNamePos;
};
/// @}