From 097f0e83e7bfce42da65cf4b8e9a6fdbb5ded506 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Thu, 13 Jun 2019 20:41:52 +0200 Subject: [PATCH] Make Plasma::Svg::elementRect a bit leaner Summary: Don't double-access maps Don't construct ids more times than necessary. Make regular expression static so it's not compiled on every run. Remove unused code. Reviewers: #plasma, #frameworks, davidedmundson Reviewed By: #plasma, davidedmundson Subscribers: bruns, kde-frameworks-devel Tags: #frameworks Differential Revision: https://phabricator.kde.org/D21788 --- src/plasma/private/svg_p.h | 2 +- src/plasma/svg.cpp | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/plasma/private/svg_p.h b/src/plasma/private/svg_p.h index e9d8f41b7..9bfc48a8d 100644 --- a/src/plasma/private/svg_p.h +++ b/src/plasma/private/svg_p.h @@ -81,7 +81,7 @@ public: void eraseRenderer(); QRectF elementRect(const QString &elementId); - QRectF findAndCacheElementRect(const QString &elementId); + QRectF findAndCacheElementRect(const QString &elementId, const QString &cacheId); QMatrix matrixForElement(const QString &elementId); void checkColorHints(); diff --git a/src/plasma/svg.cpp b/src/plasma/svg.cpp index 89a892265..ffcc4b153 100644 --- a/src/plasma/svg.cpp +++ b/src/plasma/svg.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -87,6 +88,7 @@ bool SharedSvgRenderer::load( // Apply the style sheet. if (!styleSheet.isEmpty() && contents.contains("current-color-scheme")) { QByteArray processedContents; + processedContents.reserve(contents.size()); QXmlStreamReader reader(contents); QBuffer buffer(&processedContents); @@ -117,19 +119,18 @@ bool SharedSvgRenderer::load( // Search the SVG to find and store all ids that contain size hints. const QString contentsAsString(QString::fromLatin1(contents)); - QRegExp idExpr(QLatin1String("id\\s*=\\s*(['\"])(\\d+-\\d+-.*)\\1")); - idExpr.setMinimal(true); + static const QRegularExpression idExpr(QLatin1String("id\\s*?=\\s*?(['\"])(\\d+?-\\d+?-.*?)\\1")); + Q_ASSERT(idExpr.isValid()); - int pos = 0; - while ((pos = idExpr.indexIn(contentsAsString, pos)) != -1) { - QString elementId = idExpr.cap(2); + auto matchIt = idExpr.globalMatch(contentsAsString); + while (matchIt.hasNext()) { + auto match = matchIt.next(); + QString elementId = match.captured(2); QRectF elementRect = boundsOnElement(elementId); if (elementRect.isValid()) { interestingElements.insert(elementId, elementRect); } - - pos += idExpr.matchedLength(); } return true; @@ -542,22 +543,23 @@ QRectF SvgPrivate::elementRect(const QString &elementId) } else if (found) { localRectCache.insert(id, rect); } else { - rect = findAndCacheElementRect(elementId); + rect = findAndCacheElementRect(elementId, id); } return rect; } -QRectF SvgPrivate::findAndCacheElementRect(const QString &elementId) +QRectF SvgPrivate::findAndCacheElementRect(const QString &elementId, const QString &id) { //we need to check the id before createRenderer(), otherwise it may generate a different id compared to the previous cacheId)( call - const QString id = cacheId(elementId); createRenderer(); - if (localRectCache.contains(id)) { - return localRectCache.value(id); + const auto it = localRectCache.constFind(id); + if (it != localRectCache.constEnd()) { + return *it; } + //This code will usually never be run because createRenderer already caches all the boundingRect in the elements in the svg QRectF elementRect = renderer->elementExists(elementId) ? renderer->matrixForElement(elementId).map(renderer->boundsOnElement(elementId)).boundingRect() : QRectF(); @@ -570,6 +572,7 @@ QRectF SvgPrivate::findAndCacheElementRect(const QString &elementId) elementRect.width() * dx, elementRect.height() * dy); cacheAndColorsTheme()->insertIntoRectsCache(path, id, elementRect); + localRectCache.insert(id, elementRect); return elementRect; }