[sdk/kommit] src/libkommitwidgets/models: libkommitwidgets: build each status icon once
Hamed Masafi <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 33ffb9356e23e2dcba01c466951cfaf9ae0599fc by Hamed Masafi, on behalf of Méven Car.
Committed on 17/08/2026 at 14:59.
Pushed by hamedmasafi into branch 'master'.
libkommitwidgets: build each status icon once
The diff tree asks its model for the icon of every row it lays out, and the icon
was read from the resource it lives in on each of those calls. A tree of a few
thousand files spends seconds of that, opening the same four files over and over.
ChangedFilesModel already keeps the icons it uses; this is the same thing for the
tree.
One icon per status is kept.
M +23 -3 src/libkommitwidgets/models/difftreemodel.cpp
https://invent.kde.org/sdk/kommit/-/commit/33ffb9356e23e2dcba01c466951cfaf9ae0599fc
diff --git a/src/libkommitwidgets/models/difftreemodel.cpp b/src/libkommitwidgets/models/difftreemodel.cpp
index 8fe1418e..14b13cba 100644
--- a/src/libkommitwidgets/models/difftreemodel.cpp
+++ b/src/libkommitwidgets/models/difftreemodel.cpp
@@ -6,6 +6,8 @@ SPDX-License-Identifier: GPL-3.0-or-later
#include "difftreemodel.h"
+#include <QHash>
+#include <QIcon>
#include <QSet>
struct DiffNodeData : public NodeData {
@@ -65,7 +67,25 @@ TreeNode *DiffTreeModel::createPath(const QStringList &path, Diff::DiffType stat
return parent;
}
-QString icon(Diff::DiffType status)
+QString iconPath(Diff::DiffType status);
+
+QIcon statusIcon(Diff::DiffType status)
+{
+ // Built once each: a tree of a few thousand files asks its model for the icon of every
+ // row it lays out, and building one reads the file behind it.
+ static QHash<int, QIcon> icons;
+
+ const auto cached = icons.constFind(static_cast<int>(status));
+ if (cached != icons.constEnd())
+ return *cached;
+
+ const auto icon = QIcon::fromTheme(iconPath(status));
+ icons.insert(static_cast<int>(status), icon);
+
+ return icon;
+}
+
+QString iconPath(Diff::DiffType status)
{
switch (status) {
case Diff::DiffType::Added:
@@ -195,9 +215,9 @@ QVariant DiffTreeModel::data(const QModelIndex &index, int role) const
auto diffData = static_cast<DiffNodeData *>(item->nodeData);
if (diffData)
- return QIcon::fromTheme(icon(diffData->diffType));
+ return statusIcon(diffData->diffType);
else
- return QIcon::fromTheme(icon(calculateNodeType(item)));
+ return statusIcon(calculateNodeType(item));
} else if (role == Qt::ForegroundRole) {
// Node *item = static_cast<Node *>(index.internalPointer());
// return textColor(item->metaData);