From eb1c9ba357598aca97fc340caf2e1f3f745341d5 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 25 May 2019 10:04:01 +0200 Subject: [PATCH] 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. --- lib/templatesimplifier.cpp | 9 +++++++-- lib/templatesimplifier.h | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 0d04af68c..cd5e09ef0 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -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(); diff --git a/lib/templatesimplifier.h b/lib/templatesimplifier.h index 19e7260d8..e7664ce00 100644 --- a/lib/templatesimplifier.h +++ b/lib/templatesimplifier.h @@ -29,6 +29,7 @@ #include #include #include +#include #include 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 mMemberFunctionsToDelete; std::vector mExplicitInstantiationsToDelete; std::vector mTypesUsedInTemplateInstantiation; + std::unordered_map mTemplateNamePos; }; /// @}