From 7db37726412fd071886cf2303e1da4a54bcd1c05 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Sun, 17 Nov 2024 01:46:49 -0800 Subject: [PATCH] core/generation: short circuit findObjectGeneration if only one exists --- src/core/generation.cpp | 8 +++++--- src/core/generation.hpp | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/generation.cpp b/src/core/generation.cpp index 147e2f9..f4fcde6 100644 --- a/src/core/generation.cpp +++ b/src/core/generation.cpp @@ -24,7 +24,7 @@ #include "reload.hpp" #include "scan.hpp" -static QHash g_generations; // NOLINT +static QHash g_generations; // NOLINT EngineGeneration::EngineGeneration(const QDir& rootPath, QmlScanner scanner) : rootPath(rootPath) @@ -326,11 +326,13 @@ EngineGeneration* EngineGeneration::currentGeneration() { } else return nullptr; } -EngineGeneration* EngineGeneration::findEngineGeneration(QQmlEngine* engine) { +EngineGeneration* EngineGeneration::findEngineGeneration(const QQmlEngine* engine) { return g_generations.value(engine); } -EngineGeneration* EngineGeneration::findObjectGeneration(QObject* object) { +EngineGeneration* EngineGeneration::findObjectGeneration(const QObject* object) { + if (g_generations.size() == 1) return EngineGeneration::currentGeneration(); + while (object != nullptr) { auto* context = QQmlEngine::contextForObject(object); diff --git a/src/core/generation.hpp b/src/core/generation.hpp index 043d2f7..2d84282 100644 --- a/src/core/generation.hpp +++ b/src/core/generation.hpp @@ -45,8 +45,8 @@ public: void registerExtension(const void* key, EngineGenerationExt* extension); EngineGenerationExt* findExtension(const void* key); - static EngineGeneration* findEngineGeneration(QQmlEngine* engine); - static EngineGeneration* findObjectGeneration(QObject* object); + static EngineGeneration* findEngineGeneration(const QQmlEngine* engine); + static EngineGeneration* findObjectGeneration(const QObject* object); // Returns the current generation if there is only one generation, // otherwise null.