From d83400bcdc1bf177aabb6940ee3686f8e4f0e942 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Tue, 31 Oct 2017 16:28:16 +0300 Subject: [PATCH] perf(smileys): Use lazy smileys loading --- src/persistence/smileypack.cpp | 19 ++++++++++++++----- src/persistence/smileypack.h | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/persistence/smileypack.cpp b/src/persistence/smileypack.cpp index 8e0564e182..d292709587 100644 --- a/src/persistence/smileypack.cpp +++ b/src/persistence/smileypack.cpp @@ -203,18 +203,18 @@ bool SmileyPack::load(const QString& filename) const int iconsCount = emoticonElements.size(); emoticons.clear(); emoticonToIcon.clear(); + emoticonToPath.clear(); icons.clear(); - icons.reserve(iconsCount); + for (int i = 0; i < iconsCount; ++i) { QDomNode node = emoticonElements.at(i); QString iconName = node.attributes().namedItem(itemName).nodeValue(); QString iconPath = QDir{path}.filePath(iconName); - icons.append(QIcon{iconPath}); QDomElement stringElement = node.firstChildElement(childName); QStringList emoticonList; while (!stringElement.isNull()) { QString emoticon = stringElement.text().replace("<", "<").replace(">", ">"); - emoticonToIcon.insert(emoticon, &icons[i]); + emoticonToPath.insert(emoticon, iconPath); emoticonList.append(emoticon); stringElement = stringElement.nextSibling().toElement(); } @@ -243,7 +243,7 @@ QString SmileyPack::smileyfied(const QString& msg) QString key = match.captured(); int startPos = match.capturedStart(); int keyLength = key.length(); - if (emoticonToIcon.contains(key)) { + if (emoticonToPath.contains(key)) { QString imgRichText = getAsRichText(key); result.replace(startPos + replaceDiff, keyLength, imgRichText); replaceDiff += imgRichText.length() - keyLength; @@ -270,7 +270,16 @@ QList SmileyPack::getEmoticons() const QIcon SmileyPack::getAsIcon(const QString& emoticon) { QMutexLocker locker(&loadingMutex); - return emoticonToIcon.contains(emoticon) ? *(emoticonToIcon[emoticon]) : QIcon(); + if (emoticonToIcon.contains(emoticon)) + return *(emoticonToIcon[emoticon]); + + if (!emoticonToPath.contains(emoticon)) + return QIcon{}; + + const QString& iconPath = emoticonToPath[emoticon]; + icons.append(QIcon{iconPath}); + emoticonToIcon.insert(emoticon, &icons.last()); + return icons.last(); } void SmileyPack::onSmileyPackChanged() diff --git a/src/persistence/smileypack.h b/src/persistence/smileypack.h index 326ae84050..e47486ec63 100644 --- a/src/persistence/smileypack.h +++ b/src/persistence/smileypack.h @@ -49,6 +49,7 @@ private slots: QVector icons; QMap emoticonToIcon; + QMap emoticonToPath; QList emoticons; QString path; mutable QMutex loadingMutex;