From 1b5d12737364b64211ff7d1111dd5026a5bd63f9 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 24 Jun 2014 17:34:20 +0200 Subject: [PATCH] Cache results for TokenList::isC() and TokenList::isCPP() to avoid redundant calls to Path::isCPP()/isC(), which perform slow string operations (conversion to lowercase, substring, comparisons) -> Speedup of 2% on self-checking cppcheck-core (MSVC12, x64, not matchcompiled) --- lib/tokenlist.cpp | 27 ++++++++++++--------------- lib/tokenlist.h | 11 +++++++++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 759cffd03..c5ccdc73a 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -60,20 +60,6 @@ const std::string& TokenList::getSourceFilePath() const return getFiles()[0]; } -bool TokenList::isC() const -{ - if (!_settings) - return Path::isC(getSourceFilePath()); - return _settings->enforcedLang == Settings::C || (_settings->enforcedLang == Settings::None && Path::isC(getSourceFilePath())); -} - -bool TokenList::isCPP() const -{ - if (!_settings) - return Path::isCPP(getSourceFilePath()); - return _settings->enforcedLang == Settings::CPP || (_settings->enforcedLang == Settings::None && Path::isCPP(getSourceFilePath())); -} - //--------------------------------------------------------------------------- // Deallocate lists.. @@ -94,6 +80,17 @@ unsigned int TokenList::appendFileIfNew(const std::string &fileName) // The "_files" vector remembers what files have been tokenized.. _files.push_back(Path::simplifyPath(fileName)); + + // Update _isC and _isCPP properties + if (_files.size() == 1) { // Update only useful if first file added to _files + if (!_settings) { + _isC = Path::isC(getSourceFilePath()); + _isCPP = Path::isCPP(getSourceFilePath()); + } else { + _isC = _settings->enforcedLang == Settings::C || (_settings->enforcedLang == Settings::None && Path::isC(getSourceFilePath())); + _isCPP = _settings->enforcedLang == Settings::CPP || (_settings->enforcedLang == Settings::None && Path::isCPP(getSourceFilePath())); + } + } return static_cast(_files.size() - 1); } @@ -210,7 +207,7 @@ void TokenList::insertTokens(Token *dest, const Token *src, unsigned int n) bool TokenList::createTokens(std::istream &code, const std::string& file0) { - _files.push_back(file0); + appendFileIfNew(file0); // line number in parsed code unsigned int lineno = 1; diff --git a/lib/tokenlist.h b/lib/tokenlist.h index 3134c7c8c..69f283240 100644 --- a/lib/tokenlist.h +++ b/lib/tokenlist.h @@ -44,10 +44,14 @@ public: const std::string& getSourceFilePath() const; /** Is the code C. Used for bailouts */ - bool isC() const; + bool isC() const { + return _isC; + } /** Is the code CPP. Used for bailouts */ - bool isCPP() const; + bool isCPP() const { + return _isCPP; + } /** * Delete all tokens in given token list @@ -136,6 +140,9 @@ private: /// private /** settings */ const Settings* _settings; + + /** File is known to be C/C++ code */ + bool _isC, _isCPP; }; /// @}