changeset 1609:705d1d979ae4

It turns out icons are loaded repeatedly when recreating menus, causing an actually appreciable slowdown when some cases. Provide a cache
author Chris Cannam
date Thu, 14 May 2020 16:38:05 +0100
parents 6616e1899daa
children bd1a7c84da8c
files widgets/IconLoader.cpp
diffstat 1 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/widgets/IconLoader.cpp	Mon May 11 17:28:12 2020 +0100
+++ b/widgets/IconLoader.cpp	Thu May 14 16:38:05 2020 +0100
@@ -22,11 +22,15 @@
 #include <QFile>
 #include <QSvgRenderer>
 #include <QSettings>
+#include <QMutex>
+#include <QMutexLocker>
 
 #include <vector>
 #include <set>
+#include <map>
 
 #include "base/Debug.h"
+#include "base/Profiler.h"
 
 using namespace std;
 
@@ -58,16 +62,29 @@
     "zoom"
 };
 
-static vector<int> sizes { 0, 16, 22, 24, 32, 48, 64, 128 };
-
 QIcon
 IconLoader::load(QString name)
 {
+    Profiler profiler("IconLoader::load");
+
+    static QMutex mutex;
+    static map<QString, QIcon> icons;
+    static const vector<int> sizes { 0, 16, 22, 24, 32, 48, 64, 128 };
+
+    QMutexLocker locker(&mutex);
+
+    if (icons.find(name) != icons.end()) {
+        return icons.at(name);
+    }
+
     QIcon icon;
     for (int sz: sizes) {
         QPixmap pmap(loadPixmap(name, sz));
         if (!pmap.isNull()) icon.addPixmap(pmap);
     }
+
+    icons[name] = icon;
+    
     return icon;
 }