From e933d71a610a951cc6af3c2ac40b97037a14bf9c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 2 Aug 2021 14:00:12 +0200 Subject: [PATCH] QFsFileEngine: avoid triple(quadruple) lookup of the same key Instead of contains(), 1-2x operator[](), and remove(), equalling 3-4 separate lookups, use find() + erase(), which does just one lookup. Since our erase() function is C++11-compliant these days and takes const_iterator instead of (mutable) iterator, we can use the const find() overload to delay a detach (attempt) until we actually erase(). Change-Id: I8e67a48e221e548528049fa093ab7ef2f1802f7e Reviewed-by: Thiago Macieira --- src/corelib/io/qfsfileengine_unix.cpp | 9 +++++---- src/corelib/io/qfsfileengine_win.cpp | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/corelib/io/qfsfileengine_unix.cpp b/src/corelib/io/qfsfileengine_unix.cpp index a57d924c12d..0b3e401be8e 100644 --- a/src/corelib/io/qfsfileengine_unix.cpp +++ b/src/corelib/io/qfsfileengine_unix.cpp @@ -646,18 +646,19 @@ bool QFSFileEnginePrivate::unmap(uchar *ptr) { #if !defined(Q_OS_INTEGRITY) Q_Q(QFSFileEngine); - if (!maps.contains(ptr)) { + const auto it = std::as_const(maps).find(ptr); + if (it == maps.cend()) { q->setError(QFile::PermissionsError, qt_error_string(EACCES)); return false; } - uchar *start = ptr - maps[ptr].first; - size_t len = maps[ptr].second; + uchar *start = ptr - it->first; + size_t len = it->second; if (-1 == munmap(start, len)) { q->setError(QFile::UnspecifiedError, qt_error_string(errno)); return false; } - maps.remove(ptr); + maps.erase(it); return true; #else return false; diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp index e95eef225b4..a0af2cf176a 100644 --- a/src/corelib/io/qfsfileengine_win.cpp +++ b/src/corelib/io/qfsfileengine_win.cpp @@ -871,17 +871,18 @@ uchar *QFSFileEnginePrivate::map(qint64 offset, qint64 size, bool QFSFileEnginePrivate::unmap(uchar *ptr) { Q_Q(QFSFileEngine); - if (!maps.contains(ptr)) { + const auto it = std::as_const(maps).find(ptr); + if (it == maps.cend()) { q->setError(QFile::PermissionsError, qt_error_string(ERROR_ACCESS_DENIED)); return false; } - uchar *start = ptr - maps[ptr]; + uchar *start = ptr - *it; if (!UnmapViewOfFile(start)) { q->setError(QFile::PermissionsError, qt_error_string()); return false; } - maps.remove(ptr); + maps.erase(it); if (maps.isEmpty()) { ::CloseHandle(mapHandle); mapHandle = NULL;