Skip to content

Commit abe904f

Browse files
dmcppcheckclaude
andcommitted
SymbolDatabase: cache getEnumType() during setValueTypeInTokenList()
getEnumType() scans the whole enum body each time a token with enum type gets a valuetype. With big enums that are used a lot this is quadratic. Cache the result per enum scope while the valuetypes are set; the result is only cached when every literal it looked at already had a valuetype. crispy-doom: total time 124s => 78s. No changed warnings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 128d874 commit abe904f

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6952,7 +6952,7 @@ void SymbolDatabase::setValueType(Token* tok, const Variable& var, const SourceL
69526952
}
69536953
}
69546954

6955-
static ValueType::Type getEnumType(const Scope* scope, const Platform& platform);
6955+
static ValueType::Type getEnumTypeCached(const Scope* scope, const Platform& platform);
69566956

69576957
void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, const SourceLocation &loc)
69586958
{
@@ -6982,7 +6982,7 @@ void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, cons
69826982
setValueType(tok, valuetype);
69836983
} else {
69846984
valuetype.sign = ValueType::SIGNED;
6985-
valuetype.type = getEnumType(enumerator.scope, mSettings.platform);
6985+
valuetype.type = getEnumTypeCached(enumerator.scope, mSettings.platform);
69866986
setValueType(tok, valuetype);
69876987
}
69886988
}
@@ -7547,7 +7547,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
75477547
}
75487548
}
75497549

7550-
static ValueType::Type getEnumType(const Scope* scope, const Platform& platform) // TODO: also determine sign?
7550+
static ValueType::Type getEnumType(const Scope* scope, const Platform& platform, bool* complete = nullptr) // TODO: also determine sign?
75517551
{
75527552
ValueType::Type type = ValueType::Type::INT;
75537553
for (const Token* tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) {
@@ -7564,6 +7564,9 @@ static ValueType::Type getEnumType(const Scope* scope, const Platform& platform)
75647564
if (const ValueType* vt = vTok->valueType()) {
75657565
if ((vt->type > type && (vt->type == ValueType::Type::LONG || vt->type == ValueType::Type::LONGLONG)))
75667566
type = vt->type;
7567+
} else if (complete) {
7568+
// the literal does not have a valuetype yet, the result might change later
7569+
*complete = false;
75677570
}
75687571
continue;
75697572
}
@@ -7577,6 +7580,23 @@ static ValueType::Type getEnumType(const Scope* scope, const Platform& platform)
75777580
return type;
75787581
}
75797582

7583+
// Cache for getEnumType(), only active during SymbolDatabase::setValueTypeInTokenList()
7584+
static thread_local std::unordered_map<const Scope*, ValueType::Type>* enumTypeCache = nullptr;
7585+
7586+
static ValueType::Type getEnumTypeCached(const Scope* scope, const Platform& platform)
7587+
{
7588+
if (!enumTypeCache)
7589+
return getEnumType(scope, platform);
7590+
const auto it = enumTypeCache->find(scope);
7591+
if (it != enumTypeCache->end())
7592+
return it->second;
7593+
bool complete = true;
7594+
const ValueType::Type type = getEnumType(scope, platform, &complete);
7595+
if (complete)
7596+
enumTypeCache->emplace(scope, type);
7597+
return type;
7598+
}
7599+
75807600
static const Token* parsedecl(const Token* type,
75817601
ValueType* const valuetype,
75827602
ValueType::Sign defaultSignedness,
@@ -7609,7 +7629,7 @@ static const Token* parsedecl(const Token* type,
76097629
else if (enum_type->isStandardType())
76107630
valuetype->fromLibraryType(enum_type->str(), settings);
76117631
} else
7612-
valuetype->type = getEnumType(valuetype->typeScope, settings.platform);
7632+
valuetype->type = getEnumTypeCached(valuetype->typeScope, settings.platform);
76137633
} else
76147634
valuetype->type = ValueType::Type::RECORD;
76157635
const bool cpp = type->isCpp();
@@ -7776,7 +7796,7 @@ static const Token* parsedecl(const Token* type,
77767796
else if (!valuetype->typeScope && type->type() && type->type()->classScope && valuetype->type != ValueType::Type::SMART_POINTER) {
77777797
if (type->type()->classScope->type == ScopeType::eEnum) {
77787798
valuetype->sign = ValueType::Sign::SIGNED;
7779-
valuetype->type = getEnumType(type->type()->classScope, settings.platform);
7799+
valuetype->type = getEnumTypeCached(type->type()->classScope, settings.platform);
77807800
} else {
77817801
valuetype->type = ValueType::Type::RECORD;
77827802
}
@@ -7966,6 +7986,13 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
79667986
if (!tokens)
79677987
tokens = mTokenizer.list.front();
79687988

7989+
std::unordered_map<const Scope*, ValueType::Type> enumTypes;
7990+
struct EnumTypeCacheGuard {
7991+
explicit EnumTypeCacheGuard(std::unordered_map<const Scope*, ValueType::Type>* cache) : prev(enumTypeCache) { enumTypeCache = cache; }
7992+
~EnumTypeCacheGuard() { enumTypeCache = prev; }
7993+
std::unordered_map<const Scope*, ValueType::Type>* prev;
7994+
} enumTypeCacheGuard(&enumTypes);
7995+
79697996
for (Token *tok = tokens; tok; tok = tok->next())
79707997
tok->setValueType(nullptr);
79717998

0 commit comments

Comments
 (0)