Skip to content

Commit f7639ab

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 d667325 commit f7639ab

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
@@ -4416,7 +4416,15 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace)
44164416
/**
44174417
* @throws Token* thrown when closing brackets are missing
44184418
*/
4419-
static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap, bool executableScope, Standards::cstd_t cStandard)
4419+
namespace {
4420+
// Result of the last backward search from a comma in setVarIdParseDeclaration()
4421+
struct CommaSearchCache {
4422+
const Token* comma = nullptr;
4423+
const Token* result = nullptr;
4424+
};
4425+
}
4426+
4427+
static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap, bool executableScope, Standards::cstd_t cStandard, CommaSearchCache& commaCache)
44204428
{
44214429
const Token* const tok1 = tok;
44224430
Token* tok2 = tok;
@@ -4507,11 +4515,19 @@ static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap
45074515
{
45084516
const Token *tok3 = tok->previous();
45094517
if (tok3 && tok3->str() == ",") {
4518+
// In a long comma separated list the search from the previous comma gives the same result
4519+
const Token* const comma = tok3;
45104520
while (tok3 && !Token::Match(tok3,";|(|[|{")) {
4521+
if (tok3 != comma && tok3 == commaCache.comma) {
4522+
tok3 = commaCache.result;
4523+
break;
4524+
}
45114525
if (Token::Match(tok3, ")|]"))
45124526
tok3 = tok3->link();
45134527
tok3 = tok3->previous();
45144528
}
4529+
commaCache.comma = comma;
4530+
commaCache.result = tok3;
45154531

45164532
if (tok3 && executableScope && Token::Match(tok3->previous(), "%name% (")) {
45174533
const Token *fdecl = tok3->previous();
@@ -4829,6 +4845,7 @@ void Tokenizer::setVarIdPass1()
48294845

48304846
VariableMap variableMap;
48314847
std::map<nonneg int, std::map<std::string, nonneg int>> structMembers;
4848+
CommaSearchCache commaCache;
48324849

48334850
std::stack<VarIdScopeInfo> scopeStack;
48344851

@@ -5029,7 +5046,7 @@ void Tokenizer::setVarIdPass1()
50295046
}
50305047

50315048
try { /* Ticket #8151 */
5032-
decl = setVarIdParseDeclaration(tok2, variableMap, scopeStack.top().isExecutable, mSettings.standards.c);
5049+
decl = setVarIdParseDeclaration(tok2, variableMap, scopeStack.top().isExecutable, mSettings.standards.c, commaCache);
50335050
} catch (const Token * errTok) {
50345051
syntaxError(errTok);
50355052
}

0 commit comments

Comments
 (0)