optimized `Token::Match()` a bit by always inlining `Token::multiCompare()` (#5332)
Scanning `mame_regtest` with `DISABLE_VALUEFLOW=1` and `--enable=all --inconclusive`: Clang 15 `1,170,770,173` -> `1,167,227,434` GGC 12 `1,370,070,422` -> `1,366,775,852`
This commit is contained in:
parent
e669b102f8
commit
74c4daaadf
|
@ -424,7 +424,13 @@ const std::string &Token::strAt(int index) const
|
||||||
return tok ? tok->mStr : emptyString;
|
return tok ? tok->mStr : emptyString;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int multiComparePercent(const Token *tok, const char*& haystack, nonneg int varid)
|
static
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
// GCC does not inline this by itself
|
||||||
|
// need to use the old syntax since the C++11 [[xxx:always_inline]] cannot be used here
|
||||||
|
inline __attribute__((always_inline))
|
||||||
|
#endif
|
||||||
|
int multiComparePercent(const Token *tok, const char*& haystack, nonneg int varid)
|
||||||
{
|
{
|
||||||
++haystack;
|
++haystack;
|
||||||
// Compare only the first character of the string for optimization reasons
|
// Compare only the first character of the string for optimization reasons
|
||||||
|
@ -556,7 +562,12 @@ static int multiComparePercent(const Token *tok, const char*& haystack, nonneg i
|
||||||
return 0xFFFF;
|
return 0xFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid)
|
static
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
// need to use the old syntax since the C++11 [[xxx:always_inline]] cannot be used here
|
||||||
|
inline __attribute__((always_inline))
|
||||||
|
#endif
|
||||||
|
int multiCompareImpl(const Token *tok, const char *haystack, nonneg int varid)
|
||||||
{
|
{
|
||||||
const char *needle = tok->str().c_str();
|
const char *needle = tok->str().c_str();
|
||||||
const char *needlePointer = needle;
|
const char *needlePointer = needle;
|
||||||
|
@ -609,6 +620,12 @@ int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cppcheck-suppress unusedFunction - used in tests only
|
||||||
|
int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid)
|
||||||
|
{
|
||||||
|
return multiCompareImpl(tok, haystack, varid);
|
||||||
|
}
|
||||||
|
|
||||||
bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_len)
|
bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_len)
|
||||||
{
|
{
|
||||||
if (!tok)
|
if (!tok)
|
||||||
|
@ -730,7 +747,7 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
|
||||||
|
|
||||||
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
// Parse multi options, such as void|int|char (accept token which is one of these 3)
|
||||||
else {
|
else {
|
||||||
const int res = multiCompare(tok, p, varid);
|
const int res = multiCompareImpl(tok, p, varid);
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
// Empty alternative matches, use the same token on next round
|
// Empty alternative matches, use the same token on next round
|
||||||
while (*p && *p != ' ')
|
while (*p && *p != ' ')
|
||||||
|
|
|
@ -154,6 +154,8 @@ struct TokenImpl {
|
||||||
* The Token class also has other functions for management of token list, matching tokens, etc.
|
* The Token class also has other functions for management of token list, matching tokens, etc.
|
||||||
*/
|
*/
|
||||||
class CPPCHECKLIB Token {
|
class CPPCHECKLIB Token {
|
||||||
|
friend class TestToken;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TokensFrontBack* mTokensFrontBack{};
|
TokensFrontBack* mTokensFrontBack{};
|
||||||
|
|
||||||
|
@ -788,6 +790,7 @@ public:
|
||||||
return const_cast<Token *>(findmatch(const_cast<const Token *>(startTok), pattern, end, varId));
|
return const_cast<Token *>(findmatch(const_cast<const Token *>(startTok), pattern, end, varId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
/**
|
/**
|
||||||
* Needle is build from multiple alternatives. If one of
|
* Needle is build from multiple alternatives. If one of
|
||||||
* them is equal to haystack, return value is 1. If there
|
* them is equal to haystack, return value is 1. If there
|
||||||
|
@ -804,6 +807,7 @@ public:
|
||||||
*/
|
*/
|
||||||
static int multiCompare(const Token *tok, const char *haystack, nonneg int varid);
|
static int multiCompare(const Token *tok, const char *haystack, nonneg int varid);
|
||||||
|
|
||||||
|
public:
|
||||||
nonneg int fileIndex() const {
|
nonneg int fileIndex() const {
|
||||||
return mImpl->mFileIndex;
|
return mImpl->mFileIndex;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue