Skip to content

Commit d667325

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

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
@@ -6975,7 +6975,7 @@ void SymbolDatabase::setValueType(Token* tok, const Variable& var, const SourceL
69756975
}
69766976
}
69776977

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

69806980
void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, const SourceLocation &loc)
69816981
{
@@ -7005,7 +7005,7 @@ void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, cons
70057005
setValueType(tok, valuetype);
70067006
} else {
70077007
valuetype.sign = ValueType::SIGNED;
7008-
valuetype.type = getEnumType(enumerator.scope, mSettings.platform);
7008+
valuetype.type = getEnumTypeCached(enumerator.scope, mSettings.platform);
70097009
setValueType(tok, valuetype);
70107010
}
70117011
}
@@ -7570,7 +7570,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
75707570
}
75717571
}
75727572

7573-
static ValueType::Type getEnumType(const Scope* scope, const Platform& platform) // TODO: also determine sign?
7573+
static ValueType::Type getEnumType(const Scope* scope, const Platform& platform, bool* complete = nullptr) // TODO: also determine sign?
75747574
{
75757575
ValueType::Type type = ValueType::Type::INT;
75767576
for (const Token* tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) {
@@ -7587,6 +7587,9 @@ static ValueType::Type getEnumType(const Scope* scope, const Platform& platform)
75877587
if (const ValueType* vt = vTok->valueType()) {
75887588
if ((vt->type > type && (vt->type == ValueType::Type::LONG || vt->type == ValueType::Type::LONGLONG)))
75897589
type = vt->type;
7590+
} else if (complete) {
7591+
// the literal does not have a valuetype yet, the result might change later
7592+
*complete = false;
75907593
}
75917594
continue;
75927595
}
@@ -7600,6 +7603,23 @@ static ValueType::Type getEnumType(const Scope* scope, const Platform& platform)
76007603
return type;
76017604
}
76027605

7606+
// Cache for getEnumType(), only active during SymbolDatabase::setValueTypeInTokenList()
7607+
static thread_local std::unordered_map<const Scope*, ValueType::Type>* enumTypeCache = nullptr;
7608+
7609+
static ValueType::Type getEnumTypeCached(const Scope* scope, const Platform& platform)
7610+
{
7611+
if (!enumTypeCache)
7612+
return getEnumType(scope, platform);
7613+
const auto it = enumTypeCache->find(scope);
7614+
if (it != enumTypeCache->end())
7615+
return it->second;
7616+
bool complete = true;
7617+
const ValueType::Type type = getEnumType(scope, platform, &complete);
7618+
if (complete)
7619+
enumTypeCache->emplace(scope, type);
7620+
return type;
7621+
}
7622+
76037623
static const Token* parsedecl(const Token* type,
76047624
ValueType* const valuetype,
76057625
ValueType::Sign defaultSignedness,
@@ -7632,7 +7652,7 @@ static const Token* parsedecl(const Token* type,
76327652
else if (enum_type->isStandardType())
76337653
valuetype->fromLibraryType(enum_type->str(), settings);
76347654
} else
7635-
valuetype->type = getEnumType(valuetype->typeScope, settings.platform);
7655+
valuetype->type = getEnumTypeCached(valuetype->typeScope, settings.platform);
76367656
} else
76377657
valuetype->type = ValueType::Type::RECORD;
76387658
const bool cpp = type->isCpp();
@@ -7799,7 +7819,7 @@ static const Token* parsedecl(const Token* type,
77997819
else if (!valuetype->typeScope && type->type() && type->type()->classScope && valuetype->type != ValueType::Type::SMART_POINTER) {
78007820
if (type->type()->classScope->type == ScopeType::eEnum) {
78017821
valuetype->sign = ValueType::Sign::SIGNED;
7802-
valuetype->type = getEnumType(type->type()->classScope, settings.platform);
7822+
valuetype->type = getEnumTypeCached(type->type()->classScope, settings.platform);
78037823
} else {
78047824
valuetype->type = ValueType::Type::RECORD;
78057825
}
@@ -7989,6 +8009,13 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
79898009
if (!tokens)
79908010
tokens = mTokenizer.list.front();
79918011

8012+
std::unordered_map<const Scope*, ValueType::Type> enumTypes;
8013+
struct EnumTypeCacheGuard {
8014+
explicit EnumTypeCacheGuard(std::unordered_map<const Scope*, ValueType::Type>* cache) : prev(enumTypeCache) { enumTypeCache = cache; }
8015+
~EnumTypeCacheGuard() { enumTypeCache = prev; }
8016+
std::unordered_map<const Scope*, ValueType::Type>* prev;
8017+
} enumTypeCacheGuard(&enumTypes);
8018+
79928019
for (Token *tok = tokens; tok; tok = tok->next())
79938020
tok->setValueType(nullptr);
79948021

0 commit comments

Comments
 (0)