From 5bc716d956c4be484a747db8aab8c643adb684d3 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Tue, 11 Feb 2025 14:42:48 +0100 Subject: [PATCH] QmlDesiger: add LazyUniquePtr Creating a memory on heap is not cheap. To defer it LazyUniquePtr is allocating as the type is cast. Change-Id: Id3239da7136101040fcb47c9359c96d8ca236bb5 Reviewed-by: Vikas Pachdha --- .../designsysteminterface.cpp | 6 ++- .../libs/designsystem/dsthememanager.cpp | 4 +- .../libs/qmldesignerutils/CMakeLists.txt | 1 + .../libs/qmldesignerutils/memory.h | 52 +++++++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/plugins/qmldesigner/libs/qmldesignerutils/memory.h diff --git a/src/plugins/qmldesigner/components/designsystemview/designsysteminterface.cpp b/src/plugins/qmldesigner/components/designsystemview/designsysteminterface.cpp index e7f07cbf9a1..dee99902d04 100644 --- a/src/plugins/qmldesigner/components/designsystemview/designsysteminterface.cpp +++ b/src/plugins/qmldesigner/components/designsystemview/designsysteminterface.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include namespace QmlDesigner { @@ -74,8 +76,8 @@ QStringList DesignSystemInterface::collections() const CollectionModel *DesignSystemInterface::createModel(const QString &typeName, DSThemeManager *collection) { auto [newItr, success] = m_models.try_emplace(typeName, - std::make_unique(collection, - m_store)); + makeLazyUniquePtr(collection, + m_store)); if (success) { // Otherwise the model will be deleted by the QML engine. QQmlEngine::setObjectOwnership(newItr->second.get(), QQmlEngine::CppOwnership); diff --git a/src/plugins/qmldesigner/libs/designsystem/dsthememanager.cpp b/src/plugins/qmldesigner/libs/designsystem/dsthememanager.cpp index d891ba7b153..498c4796ec5 100644 --- a/src/plugins/qmldesigner/libs/designsystem/dsthememanager.cpp +++ b/src/plugins/qmldesigner/libs/designsystem/dsthememanager.cpp @@ -263,9 +263,7 @@ void DSThemeManager::decorateThemeInterface(ModelNode rootNode) const DSThemeGroup *DSThemeManager::propertyGroup(GroupType type) { - auto itr = m_groups.find(type); - if (itr == m_groups.end()) - itr = m_groups.try_emplace(type, std::make_unique(type)).first; + auto itr = m_groups.try_emplace(type, makeLazySharedPtr(type)).first; return itr->second.get(); } diff --git a/src/plugins/qmldesigner/libs/qmldesignerutils/CMakeLists.txt b/src/plugins/qmldesigner/libs/qmldesignerutils/CMakeLists.txt index 2684cb3d65e..cca42246688 100644 --- a/src/plugins/qmldesigner/libs/qmldesignerutils/CMakeLists.txt +++ b/src/plugins/qmldesigner/libs/qmldesignerutils/CMakeLists.txt @@ -8,6 +8,7 @@ add_qtc_library(QmlDesignerUtils STATIC asset.cpp asset.h designeralgorithm.h filedownloader.cpp filedownloader.h + memory.h multifiledownloader.cpp multifiledownloader.h fileextractor.cpp fileextractor.h hdrimage.cpp hdrimage.h diff --git a/src/plugins/qmldesigner/libs/qmldesignerutils/memory.h b/src/plugins/qmldesigner/libs/qmldesignerutils/memory.h new file mode 100644 index 00000000000..17b8874d3bb --- /dev/null +++ b/src/plugins/qmldesigner/libs/qmldesignerutils/memory.h @@ -0,0 +1,52 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#pragma once + +#include +#include + +namespace QmlDesigner { + +template +class LazyPtr +{ + using ResultType = Pointer::element_type; + +public: + LazyPtr(Arguments &&...arguments) + : m_arguments{std::forward_as_tuple(std::forward(arguments)...)} + {} + + operator Pointer() const + { + return std::apply( + [](auto &&...arguments) { + return Pointer(new ResultType(std::forward(arguments)...)); + }, + m_arguments); + } + +private: + std::tuple m_arguments; +}; + +template +using LazyUniquePtr = LazyPtr, Arguments...>; + +template +LazyUniquePtr makeLazyUniquePtr(Arguments &&...arguments) +{ + return LazyUniquePtr(std::forward(arguments)...); +} + +template +using LazySharedPtr = LazyPtr, Arguments...>; + +template +LazySharedPtr makeLazySharedPtr(Arguments &&...arguments) +{ + return LazySharedPtr(std::forward(arguments)...); +} + +} // namespace QmlDesigner