From b847623fbe2e33e228e4744e5fb4a2bde903397d Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 29 Dec 2021 22:46:04 +0100 Subject: [PATCH] Reduce allocations and time spent creating delegates by half --- src/timeline/DelegateChooser.cpp | 7 ++++--- src/timeline/DelegateChooser.h | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/timeline/DelegateChooser.cpp b/src/timeline/DelegateChooser.cpp index ad734895..151510b6 100644 --- a/src/timeline/DelegateChooser.cpp +++ b/src/timeline/DelegateChooser.cpp @@ -53,7 +53,8 @@ DelegateChooser::setRoleValue(const QVariant &value) { if (value != roleValue_) { roleValue_ = value; - recalcChild(); + if (isComponentComplete()) + recalcChild(); emit roleValueChanged(); } } @@ -96,8 +97,8 @@ void DelegateChooser::recalcChild() { for (const auto choice : qAsConst(choices_)) { - auto choiceValue = choice->roleValue(); - if (!roleValue_.isValid() || !choiceValue.isValid() || choiceValue == roleValue_) { + const auto &choiceValue = choice->roleValueRef(); + if (choiceValue == roleValue_ || (!choiceValue.isValid() && !roleValue_.isValid())) { if (child_) { child_->setParentItem(nullptr); child_ = nullptr; diff --git a/src/timeline/DelegateChooser.h b/src/timeline/DelegateChooser.h index 3e4b16d7..4e83fce1 100644 --- a/src/timeline/DelegateChooser.h +++ b/src/timeline/DelegateChooser.h @@ -25,10 +25,11 @@ public: Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged) Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) - QQmlComponent *delegate() const; + [[nodiscard]] QQmlComponent *delegate() const; void setDelegate(QQmlComponent *delegate); - QVariant roleValue() const; + [[nodiscard]] QVariant roleValue() const; + [[nodiscard]] const QVariant &roleValueRef() const { return roleValue_; } void setRoleValue(const QVariant &value); signals: @@ -53,10 +54,10 @@ public: QQmlListProperty choices(); - QVariant roleValue() const; + [[nodiscard]] QVariant roleValue() const; void setRoleValue(const QVariant &value); - QQuickItem *child() const { return child_; } + [[nodiscard]] QQuickItem *child() const { return child_; } void recalcChild(); void componentComplete() override;