Skip to content

Commit e2c7689

Browse files
dmcppcheckclaude
andcommitted
Tokenizer: avoid quadratic backward search for commas in setVarIdParseDeclaration()
For a name after a comma the tokens before it were searched back to ';', '(', '[' or '{'. In a long comma separated list of names this search was repeated from every element. Reuse the result of the search from the previous comma. crispy-doom: total time 78s => 64s. No changed warnings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent abe904f commit e2c7689

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,7 +4393,15 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace)
43934393
/**
43944394
* @throws Token* thrown when closing brackets are missing
43954395
*/
4396-
static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap, bool executableScope, Standards::cstd_t cStandard)
4396+
namespace {
4397+
// Result of the last backward search from a comma in setVarIdParseDeclaration()
4398+
struct CommaSearchCache {
4399+
const Token* comma = nullptr;
4400+
const Token* result = nullptr;
4401+
};
4402+
}
4403+
4404+
static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap, bool executableScope, Standards::cstd_t cStandard, CommaSearchCache& commaCache)
43974405
{
43984406
const Token* const tok1 = tok;
43994407
Token* tok2 = tok;
@@ -4484,11 +4492,19 @@ static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap
44844492
{
44854493
const Token *tok3 = tok->previous();
44864494
if (tok3 && tok3->str() == ",") {
4495+
// In a long comma separated list the search from the previous comma gives the same result
4496+
const Token* const comma = tok3;
44874497
while (tok3 && !Token::Match(tok3,";|(|[|{")) {
4498+
if (tok3 != comma && tok3 == commaCache.comma) {
4499+
tok3 = commaCache.result;
4500+
break;
4501+
}
44884502
if (Token::Match(tok3, ")|]"))
44894503
tok3 = tok3->link();
44904504
tok3 = tok3->previous();
44914505
}
4506+
commaCache.comma = comma;
4507+
commaCache.result = tok3;
44924508

44934509
if (tok3 && executableScope && Token::Match(tok3->previous(), "%name% (")) {
44944510
const Token *fdecl = tok3->previous();
@@ -4791,6 +4807,7 @@ void Tokenizer::setVarIdPass1()
47914807

47924808
VariableMap variableMap;
47934809
std::map<nonneg int, std::map<std::string, nonneg int>> structMembers;
4810+
CommaSearchCache commaCache;
47944811

47954812
std::stack<VarIdScopeInfo> scopeStack;
47964813

@@ -4991,7 +5008,7 @@ void Tokenizer::setVarIdPass1()
49915008
}
49925009

49935010
try { /* Ticket #8151 */
4994-
decl = setVarIdParseDeclaration(tok2, variableMap, scopeStack.top().isExecutable, mSettings.standards.c);
5011+
decl = setVarIdParseDeclaration(tok2, variableMap, scopeStack.top().isExecutable, mSettings.standards.c, commaCache);
49955012
} catch (const Token * errTok) {
49965013
syntaxError(errTok);
49975014
}

0 commit comments

Comments
 (0)