From 467b15a20c3d6eb611ea5f6ccffd5fc0df81b0c4 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 10 Nov 2016 12:45:49 +0100 Subject: Improve use of QHash to minimize double hashing Avoid looking up by key twice in a row in various locations, but instead using iterators and index lookup. Change-Id: I61a079115199ab9c041ad3a26d36b45ee3f775e0 Reviewed-by: Thiago Macieira --- src/gui/image/qiconloader.cpp | 4 +--- src/gui/image/qpixmapcache.cpp | 19 ++++++++----------- src/gui/kernel/qopenglcontext.cpp | 5 +++-- src/gui/text/qfontengine_ft.cpp | 5 +++-- src/gui/text/qtextformat.cpp | 3 +-- src/gui/text/qtextlayout.cpp | 9 ++++----- .../fontdatabases/windows/qwindowsfontdatabase.cpp | 5 +++-- src/widgets/accessible/itemviews.cpp | 7 +++---- src/widgets/styles/qpixmapstyle.cpp | 6 +++--- 9 files changed, 29 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/gui/image/qiconloader.cpp b/src/gui/image/qiconloader.cpp index eda9d6f24e..17d77a07b5 100644 --- a/src/gui/image/qiconloader.cpp +++ b/src/gui/image/qiconloader.cpp @@ -383,13 +383,11 @@ QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName, // Used to protect against potential recursions visited << themeName; - QIconTheme theme = themeList.value(themeName); + QIconTheme &theme = themeList[themeName]; if (!theme.isValid()) { theme = QIconTheme(themeName); if (!theme.isValid()) theme = QIconTheme(fallbackTheme()); - - themeList.insert(themeName, theme); } const QStringList contentDirs = theme.contentDirs(); diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp index 6d03332367..73448943e1 100644 --- a/src/gui/image/qpixmapcache.cpp +++ b/src/gui/image/qpixmapcache.cpp @@ -324,26 +324,23 @@ QPixmap *QPMCache::object(const QPixmapCache::Key &key) const bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) { - QPixmapCache::Key cacheKey; - QPixmapCache::Key oldCacheKey = cacheKeys.value(key); + QPixmapCache::Key &cacheKey = cacheKeys[key]; //If for the same key we add already a pixmap we should delete it - if (oldCacheKey.d) { - QCache::remove(oldCacheKey); - cacheKeys.remove(key); - } + if (cacheKey.d) + QCache::remove(cacheKey); //we create a new key the old one has been removed cacheKey = createKey(); bool success = QCache::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); if (success) { - cacheKeys.insert(key, cacheKey); if (!theid) { theid = startTimer(flush_time); t = false; } } else { //Insertion failed we released the new allocated key + cacheKeys.remove(key); releaseKey(cacheKey); } return success; @@ -389,12 +386,12 @@ bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int bool QPMCache::remove(const QString &key) { - QPixmapCache::Key cacheKey = cacheKeys.value(key); + auto cacheKey = cacheKeys.constFind(key); //The key was not in the cache - if (!cacheKey.d) + if (cacheKey == cacheKeys.constEnd()) return false; - cacheKeys.remove(key); - return QCache::remove(cacheKey); + cacheKeys.erase(cacheKey); + return QCache::remove(cacheKey.value()); } bool QPMCache::remove(const QPixmapCache::Key &key) diff --git a/src/gui/kernel/qopenglcontext.cpp b/src/gui/kernel/qopenglcontext.cpp index 7e5697e5d8..8aea593bf0 100644 --- a/src/gui/kernel/qopenglcontext.cpp +++ b/src/gui/kernel/qopenglcontext.cpp @@ -848,14 +848,15 @@ QAbstractOpenGLFunctions *QOpenGLContext::versionFunctions(const QOpenGLVersionP // Create object if suitable one not cached QAbstractOpenGLFunctions* funcs = 0; - if (!d->versionFunctions.contains(vp)) { + auto it = d->versionFunctions.constFind(vp); + if (it == d->versionFunctions.constEnd()) { funcs = QOpenGLVersionFunctionsFactory::create(vp); if (funcs) { funcs->setOwningContext(this); d->versionFunctions.insert(vp, funcs); } } else { - funcs = d->versionFunctions.value(vp); + funcs = it.value(); } if (funcs && QOpenGLContext::currentContext() == this) diff --git a/src/gui/text/qfontengine_ft.cpp b/src/gui/text/qfontengine_ft.cpp index 09b0475a84..6d575e0e88 100644 --- a/src/gui/text/qfontengine_ft.cpp +++ b/src/gui/text/qfontengine_ft.cpp @@ -320,8 +320,9 @@ void QFreetypeFace::release(const QFontEngine::FaceId &face_id) cleanup(); - if (freetypeData->faces.contains(face_id)) - freetypeData->faces.take(face_id); + auto it = freetypeData->faces.constFind(face_id); + if (it != freetypeData->faces.constEnd()) + freetypeData->faces.erase(it); if (freetypeData->faces.isEmpty()) { FT_Done_FreeType(freetypeData->library); diff --git a/src/gui/text/qtextformat.cpp b/src/gui/text/qtextformat.cpp index 8adeb3e659..39fec032dc 100644 --- a/src/gui/text/qtextformat.cpp +++ b/src/gui/text/qtextformat.cpp @@ -3409,8 +3409,7 @@ int QTextFormatCollection::indexForFormat(const QTextFormat &format) f.d = new QTextFormatPrivate; f.d->resolveFont(defaultFnt); - if (!hashes.contains(hash, idx)) - hashes.insert(hash, idx); + hashes.insert(hash, idx); } QT_CATCH(...) { formats.pop_back(); diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index f5827bb683..28a5eb6a89 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -1059,9 +1059,10 @@ QList QTextLayout::glyphRuns(int from, int length) const QGlyphRun::GlyphRunFlags flags = glyphRun.flags(); QPair key(fontEngine, int(flags)); // merge the glyph runs using the same font - if (glyphRunHash.contains(key)) { - QGlyphRun &oldGlyphRun = glyphRunHash[key]; - + QGlyphRun &oldGlyphRun = glyphRunHash[key]; + if (oldGlyphRun.isEmpty()) { + oldGlyphRun = glyphRun; + } else { QVector indexes = oldGlyphRun.glyphIndexes(); QVector positions = oldGlyphRun.positions(); QRectF boundingRect = oldGlyphRun.boundingRect(); @@ -1073,8 +1074,6 @@ QList QTextLayout::glyphRuns(int from, int length) const oldGlyphRun.setGlyphIndexes(indexes); oldGlyphRun.setPositions(positions); oldGlyphRun.setBoundingRect(boundingRect); - } else { - glyphRunHash[key] = glyphRun; } } } diff --git a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp index ad4dd3c944..b8d997bc35 100644 --- a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp +++ b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp @@ -524,10 +524,11 @@ namespace { const void *key = *reinterpret_cast(fontFileReferenceKey); *fontFileStream = NULL; - if (!m_fontDatas.contains(key)) + auto it = m_fontDatas.constFind(key); + if (it == m_fontDatas.constEnd()) return E_FAIL; - QByteArray fontData = m_fontDatas.value(key); + QByteArray fontData = it.value(); DirectWriteFontFileStream *stream = new DirectWriteFontFileStream(fontData); stream->AddRef(); *fontFileStream = stream; diff --git a/src/widgets/accessible/itemviews.cpp b/src/widgets/accessible/itemviews.cpp index 796d13487b..09eba76fbd 100644 --- a/src/widgets/accessible/itemviews.cpp +++ b/src/widgets/accessible/itemviews.cpp @@ -486,10 +486,9 @@ QAccessibleInterface *QAccessibleTable::child(int logicalIndex) const if (!view()->model()) return 0; - if (childToId.contains(logicalIndex)) { - QAccessible::Id id = childToId.value(logicalIndex); - return QAccessible::accessibleInterface(id); - } + auto id = childToId.constFind(logicalIndex); + if (id != childToId.constEnd()) + return QAccessible::accessibleInterface(id.value()); int vHeader = verticalHeader() ? 1 : 0; int hHeader = horizontalHeader() ? 1 : 0; diff --git a/src/widgets/styles/qpixmapstyle.cpp b/src/widgets/styles/qpixmapstyle.cpp index b51860045d..e973a96a91 100644 --- a/src/widgets/styles/qpixmapstyle.cpp +++ b/src/widgets/styles/qpixmapstyle.cpp @@ -628,10 +628,10 @@ void QPixmapStyle::drawCachedPixmap(QPixmapStyle::ControlDescriptor control, con QPainter *p) const { Q_D(const QPixmapStyle); - if (!d->descriptors.contains(control)) + auto descriptor = d->descriptors.constFind(control); + if (descriptor == d->descriptors.constEnd()) return; - const QPixmapStyleDescriptor &desc = d->descriptors.value(control); - const QPixmap pix = d->getCachedPixmap(control, desc, rect.size()); + const QPixmap pix = d->getCachedPixmap(control, descriptor.value(), rect.size()); Q_ASSERT(!pix.isNull()); p->drawPixmap(rect, pix); } -- cgit v1.2.3