Skip to content

Commit

Permalink
Lookup optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dantti authored and steveire committed Oct 31, 2021
1 parent 65cf814 commit 735bf06
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
6 changes: 4 additions & 2 deletions templates/lib/cachingloaderdecorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ CachingLoaderDecorator::loadByName(const QString &name,
const Grantlee::Engine *engine) const
{
Q_D(const CachingLoaderDecorator);
if (d->m_cache.contains(name))
return d->m_cache.value(name);
const auto it = d->m_cache.constFind(name);
if (it != d->m_cache.constEnd()) {
return it.value();
}

const auto t = d->m_wrappedLoader->loadByName(name, engine);

Expand Down
7 changes: 4 additions & 3 deletions templates/lib/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ QVariant Context::lookup(const QString &str) const
Q_D(const Context);

// return a variant from the stack.
for (auto &h : d->m_variantHashStack) {
if (h.contains(str)) {
auto var = h.value(str);
for (const auto &h : d->m_variantHashStack) {
auto it = h.constFind(str);
if (it != h.constEnd()) {
auto var = it.value();
// If the user passed a string into the context, turn it into a
// Grantlee::SafeString.
if (var.userType() == qMetaTypeId<QString>()) {
Expand Down
11 changes: 8 additions & 3 deletions templates/lib/customtyperegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ QVariant CustomTypeRegistry::lookup(const QVariant &object,
MetaType::LookupFunction lf;

{
if (!types.contains(id)) {
auto it = types.constFind(id);
if (it == types.constEnd()) {
qCWarning(GRANTLEE_CUSTOMTYPE)
<< "Don't know how to handle metatype" << QMetaType::typeName(id);
// :TODO: Print out error message
return QVariant();
}

const CustomTypeInfo &info = types[id];
const CustomTypeInfo &info = it.value();
if (!info.lookupFunction) {
qCWarning(GRANTLEE_CUSTOMTYPE)
<< "No lookup function for metatype" << QMetaType::typeName(id);
Expand All @@ -80,5 +81,9 @@ QVariant CustomTypeRegistry::lookup(const QVariant &object,

bool CustomTypeRegistry::lookupAlreadyRegistered(int id) const
{
return types.contains(id) && types.value(id).lookupFunction != nullptr;
auto it = types.constFind(id);
if (it != types.constEnd()) {
return it.value().lookupFunction != nullptr;
}
return false;
}
5 changes: 3 additions & 2 deletions templates/lib/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ EnginePrivate::loadScriptableLibrary(const QString &name, uint minorVersion)
if (libFileName.isEmpty())
return nullptr;

if (m_scriptableLibraries.contains(libFileName)) {
auto library = m_scriptableLibraries.value(libFileName);
const auto it = m_scriptableLibraries.constFind(libFileName);
if (it != m_scriptableLibraries.constEnd()) {
auto library = it.value();
library->setNodeFactories(
m_scriptableTagLibrary->nodeFactories(libFileName));
library->setFilters(m_scriptableTagLibrary->filters(libFileName));
Expand Down
10 changes: 6 additions & 4 deletions templates/lib/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ void Parser::skipPast(const QString &tag)
QSharedPointer<Filter> Parser::getFilter(const QString &name) const
{
Q_D(const Parser);
if (!d->m_filters.contains(name))
throw Grantlee::Exception(UnknownFilterError,
QStringLiteral("Unknown filter: %1").arg(name));
return d->m_filters.value(name);
const auto it = d->m_filters.constFind(name);
if (it != d->m_filters.constEnd()) {
return it.value();
}
throw Grantlee::Exception(UnknownFilterError,
QStringLiteral("Unknown filter: %1").arg(name));
}

NodeList Parser::parse(Node *parent, const QString &stopAt)
Expand Down
5 changes: 3 additions & 2 deletions templates/lib/templateloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ bool InMemoryTemplateLoader::canLoadTemplate(const QString &name) const
Template InMemoryTemplateLoader::loadByName(const QString &name,
Engine const *engine) const
{
if (m_namedTemplates.contains(name)) {
return engine->newTemplate(m_namedTemplates.value(name), name);
const auto it = m_namedTemplates.constFind(name);
if (it != m_namedTemplates.constEnd()) {
return engine->newTemplate(it.value(), name);
}
throw Grantlee::Exception(
TagSyntaxError,
Expand Down

0 comments on commit 735bf06

Please sign in to comment.