Skip to content

Commit 74c4daa

Browse files
authored
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`
1 parent e669b10 commit 74c4daa

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/token.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,13 @@ const std::string &Token::strAt(int index) const
424424
return tok ? tok->mStr : emptyString;
425425
}
426426

427-
static int multiComparePercent(const Token *tok, const char*& haystack, nonneg int varid)
427+
static
428+
#if defined(__GNUC__)
429+
// GCC does not inline this by itself
430+
// need to use the old syntax since the C++11 [[xxx:always_inline]] cannot be used here
431+
inline __attribute__((always_inline))
432+
#endif
433+
int multiComparePercent(const Token *tok, const char*& haystack, nonneg int varid)
428434
{
429435
++haystack;
430436
// 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
556562
return 0xFFFF;
557563
}
558564

559-
int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid)
565+
static
566+
#if defined(__GNUC__)
567+
// need to use the old syntax since the C++11 [[xxx:always_inline]] cannot be used here
568+
inline __attribute__((always_inline))
569+
#endif
570+
int multiCompareImpl(const Token *tok, const char *haystack, nonneg int varid)
560571
{
561572
const char *needle = tok->str().c_str();
562573
const char *needlePointer = needle;
@@ -609,6 +620,12 @@ int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid
609620
return -1;
610621
}
611622

623+
// cppcheck-suppress unusedFunction - used in tests only
624+
int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid)
625+
{
626+
return multiCompareImpl(tok, haystack, varid);
627+
}
628+
612629
bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_len)
613630
{
614631
if (!tok)
@@ -730,7 +747,7 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
730747

731748
// Parse multi options, such as void|int|char (accept token which is one of these 3)
732749
else {
733-
const int res = multiCompare(tok, p, varid);
750+
const int res = multiCompareImpl(tok, p, varid);
734751
if (res == 0) {
735752
// Empty alternative matches, use the same token on next round
736753
while (*p && *p != ' ')

lib/token.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ struct TokenImpl {
154154
* The Token class also has other functions for management of token list, matching tokens, etc.
155155
*/
156156
class CPPCHECKLIB Token {
157+
friend class TestToken;
158+
157159
private:
158160
TokensFrontBack* mTokensFrontBack{};
159161

@@ -788,6 +790,7 @@ class CPPCHECKLIB Token {
788790
return const_cast<Token *>(findmatch(const_cast<const Token *>(startTok), pattern, end, varId));
789791
}
790792

793+
private:
791794
/**
792795
* Needle is build from multiple alternatives. If one of
793796
* them is equal to haystack, return value is 1. If there
@@ -804,6 +807,7 @@ class CPPCHECKLIB Token {
804807
*/
805808
static int multiCompare(const Token *tok, const char *haystack, nonneg int varid);
806809

810+
public:
807811
nonneg int fileIndex() const {
808812
return mImpl->mFileIndex;
809813
}

0 commit comments

Comments
 (0)