aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-10-25 15:09:46 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-10-26 09:05:52 +0000
commit1632be617002a0d4a60b943bf9db66eaae4be4d7 (patch)
tree9ecd7f5ffb1484ef90335b7bc997c0e2a188254b
parente87218f6b3d8fa7fed37ebdd2c45df33ed2364dc (diff)
Imagine: enable caching for image lookupsv5.10.0-beta3
File system lookups are somewhat expensive, so cache the results to avoid repetitive lookups. The default cache size of 500 is a rough estimate based on that the default assets have 300+ images per scale factor. Caching image lookups gives an average of 20% performance improvement in qmlbench: control before after improvement ---------------------------------------------- busyindicator 29 32 1.10344827586207 button 53 60 1.13207547169811 checkbox 51 60 1.17647058823529 combobox 39 44 1.12820512820513 dial 110 147 1.33636363636364 itemdelegate 54 61 1.12962962962963 label 143 152 1.06293706293706 menuitem 26 34 1.30769230769231 progressbar 13 15 1.15384615384615 radiobutton 52 59 1.13461538461538 scrollbar 37 62 1.67567567567568 scrollview 78 121 1.55128205128205 slider 31 40 1.29032258064516 spinbox 75 85 1.13333333333333 switch 35 43 1.22857142857143 textarea 80 88 1.1 textfield 56 63 1.125 tumbler 126 128 1.01587301587302 ---------------------------------------------- average 1.2102967624703 Caching can be disabled by setting an environment variable: QT_QUICK_CONTROLS_IMAGINE_CACHE=0 but this is not advertised in the docs for the time being. It is there to help debugging, should anything go wrong with caching. Change-Id: I1119f3d8186bc9a51cc174b06fed02ed9e1fb70c Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/imports/controls/imagine/qquickimageselector.cpp21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/imports/controls/imagine/qquickimageselector.cpp b/src/imports/controls/imagine/qquickimageselector.cpp
index 2c3b0cd3..1b3e80a9 100644
--- a/src/imports/controls/imagine/qquickimageselector.cpp
+++ b/src/imports/controls/imagine/qquickimageselector.cpp
@@ -47,6 +47,15 @@
QT_BEGIN_NAMESPACE
+static const int DEFAULT_CACHE = 500;
+
+static inline int cacheSize()
+{
+ static bool ok = false;
+ static const int size = qEnvironmentVariableIntValue("QT_QUICK_CONTROLS_IMAGINE_CACHE", &ok);
+ return ok ? size : DEFAULT_CACHE;
+}
+
Q_DECLARE_LOGGING_CATEGORY(lcQtQuickControlsImagine)
// input: [focused, pressed]
@@ -90,12 +99,14 @@ static QString findFile(const QDir &dir, const QString &baseName, const QStringL
if (QFile::exists(filePath))
return QFileSelector().select(filePath);
}
- return QString();
+ // return an empty string to indicate that the lookup has been done
+ // even if no matching asset was found
+ return QLatin1String("");
}
QQuickImageSelector::QQuickImageSelector(QObject *parent)
: QObject(parent),
- m_cache(false),
+ m_cache(cacheSize() > 0),
m_complete(false),
m_separator(QLatin1String("-"))
{
@@ -224,18 +235,20 @@ QString QQuickImageSelector::cacheKey() const
void QQuickImageSelector::updateSource()
{
- static QCache<QString, QString> cache(200); // TODO: cost
+ static QCache<QString, QString> cache(cacheSize());
const QString key = cacheKey();
QString bestFilePath;
+
if (m_cache) {
QString *cachedPath = cache.object(key);
if (cachedPath)
bestFilePath = *cachedPath;
}
- if (bestFilePath.isEmpty()) {
+ // note: a cached file path may be empty
+ if (bestFilePath.isNull()) {
QDir dir(m_path);
int bestScore = -1;