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)
This commit is contained in:
PKEuS 2014-06-24 17:34:20 +02:00
parent 731180b7dd
commit 1b5d127373
2 changed files with 21 additions and 17 deletions

View File

@ -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<unsigned int>(_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;

View File

@ -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;
};
/// @}