diff options
author | Anton Kudryavtsev <a.kudryavtsev@netris.ru> | 2016-03-31 12:01:59 +0300 |
---|---|---|
committer | Anton Kudryavtsev <a.kudryavtsev@netris.ru> | 2016-07-02 07:39:06 +0000 |
commit | 6662919ecde901771d9641fd732aa0735ebb39e6 (patch) | |
tree | 449752f7d9582cd685d81829e0e2245b0f9a0ece /src/corelib/io | |
parent | 2185b2f054ddae0fc67b8799b3561c57729f1cd7 (diff) |
CoreLib: use QStringRef to optimize memory allocation
Replace substring functions that return QString with
corresponding functions that return QStringRef where
it's possible.
Create QString from QStringRef only where necessary.
Change-Id: Id9ea11b16947220cd27787c0b529de62d10b6c26
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/io')
-rw-r--r-- | src/corelib/io/qdir.cpp | 8 | ||||
-rw-r--r-- | src/corelib/io/qfilesystemengine.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qfilesystemengine_unix.cpp | 5 | ||||
-rw-r--r-- | src/corelib/io/qfilesystemengine_win.cpp | 10 | ||||
-rw-r--r-- | src/corelib/io/qfilesystemiterator_win.cpp | 3 | ||||
-rw-r--r-- | src/corelib/io/qresource.cpp | 4 | ||||
-rw-r--r-- | src/corelib/io/qstandardpaths_unix.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qurl.cpp | 10 |
8 files changed, 25 insertions, 19 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index af07b52cdb..8197ea63e7 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -146,9 +146,11 @@ inline QStringList QDirPrivate::splitFilters(const QString &nameFilter, QChar se { if (sep.isNull()) sep = getFilterSepChar(nameFilter); - QStringList ret = nameFilter.split(sep); - for (int i = 0; i < ret.count(); ++i) - ret[i] = ret[i].trimmed(); + const QVector<QStringRef> split = nameFilter.splitRef(sep); + QStringList ret; + ret.reserve(split.size()); + for (const auto &e : split) + ret.append(e.trimmed().toString()); return ret; } diff --git a/src/corelib/io/qfilesystemengine.cpp b/src/corelib/io/qfilesystemengine.cpp index fd8f251ccb..febfdbb9d4 100644 --- a/src/corelib/io/qfilesystemengine.cpp +++ b/src/corelib/io/qfilesystemengine.cpp @@ -91,7 +91,7 @@ QString QFileSystemEngine::slowCanonicalized(const QString &path) if (separatorPos != -1) { if (fi.isDir() && !target.endsWith(slash)) target.append(slash); - target.append(tmpPath.mid(separatorPos)); + target.append(tmpPath.midRef(separatorPos)); } tmpPath = QDir::cleanPath(target); separatorPos = 0; diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index eebe5f1f94..6aaaa4c05a 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -182,8 +182,9 @@ QFileSystemEntry QFileSystemEngine::getLinkTarget(const QFileSystemEntry &link, #endif if (!ret.startsWith(QLatin1Char('/'))) { - if (link.filePath().startsWith(QLatin1Char('/'))) { - ret.prepend(link.filePath().left(link.filePath().lastIndexOf(QLatin1Char('/'))) + const QString linkFilePath = link.filePath(); + if (linkFilePath.startsWith(QLatin1Char('/'))) { + ret.prepend(linkFilePath.leftRef(linkFilePath.lastIndexOf(QLatin1Char('/'))) + QLatin1Char('/')); } else { ret.prepend(QDir::currentPath() + QLatin1Char('/')); diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp index 8375dc9b1b..abf346a717 100644 --- a/src/corelib/io/qfilesystemengine_win.cpp +++ b/src/corelib/io/qfilesystemengine_win.cpp @@ -52,6 +52,7 @@ #include "qvarlengtharray.h" #include "qdatetime.h" #include "qt_windows.h" +#include "qvector.h" #include <sys/types.h> #include <direct.h> @@ -407,11 +408,11 @@ static QString readLink(const QFileSystemEntry &link) static bool uncShareExists(const QString &server) { // This code assumes the UNC path is always like \\?\UNC\server... - QStringList parts = server.split(QLatin1Char('\\'), QString::SkipEmptyParts); + const QVector<QStringRef> parts = server.splitRef(QLatin1Char('\\'), QString::SkipEmptyParts); if (parts.count() >= 3) { QStringList shares; if (QFileSystemEngine::uncListSharesOnServer(QLatin1String("\\\\") + parts.at(2), &shares)) - return parts.count() >= 4 ? shares.contains(parts.at(3), Qt::CaseInsensitive) : true; + return parts.count() < 4 || shares.contains(parts.at(3).toString(), Qt::CaseInsensitive); } return false; } @@ -1106,9 +1107,10 @@ bool QFileSystemEngine::removeDirectory(const QFileSystemEntry &entry, bool remo if (removeEmptyParents) { dirName = QDir::toNativeSeparators(QDir::cleanPath(dirName)); for (int oldslash = 0, slash=dirName.length(); slash > 0; oldslash = slash) { - QString chunk = dirName.left(slash); - if (chunk.length() == 2 && chunk.at(0).isLetter() && chunk.at(1) == QLatin1Char(':')) + const QStringRef chunkRef = dirName.leftRef(slash); + if (chunkRef.length() == 2 && chunkRef.at(0).isLetter() && chunkRef.at(1) == QLatin1Char(':')) break; + const QString chunk = chunkRef.toString(); if (!isDirPath(chunk, 0)) return false; if (!rmDir(chunk)) diff --git a/src/corelib/io/qfilesystemiterator_win.cpp b/src/corelib/io/qfilesystemiterator_win.cpp index 9e40a41b4c..2ce7bd7a4b 100644 --- a/src/corelib/io/qfilesystemiterator_win.cpp +++ b/src/corelib/io/qfilesystemiterator_win.cpp @@ -40,6 +40,7 @@ #include "qfilesystemiterator_p.h" #include "qfilesystemengine_p.h" #include "qplatformdefs.h" +#include "qvector.h" #include <QtCore/qt_windows.h> @@ -103,7 +104,7 @@ bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaDa FINDEX_SEARCH_OPS(searchOps), 0, dwAdditionalFlags); if (findFileHandle == INVALID_HANDLE_VALUE) { if (nativePath.startsWith(QLatin1String("\\\\?\\UNC\\"))) { - QStringList parts = nativePath.split(QLatin1Char('\\'), QString::SkipEmptyParts); + const QVector<QStringRef> parts = nativePath.splitRef(QLatin1Char('\\'), QString::SkipEmptyParts); if (parts.count() == 4 && QFileSystemEngine::uncListSharesOnServer( QLatin1String("\\\\") + parts.at(2), &uncShares)) { if (uncShares.isEmpty()) diff --git a/src/corelib/io/qresource.cpp b/src/corelib/io/qresource.cpp index 96957ac11d..7fe3753da4 100644 --- a/src/corelib/io/qresource.cpp +++ b/src/corelib/io/qresource.cpp @@ -303,12 +303,12 @@ QResourcePrivate::ensureInitialized() const if(!that->absoluteFilePath.startsWith(QLatin1Char(':'))) that->absoluteFilePath.prepend(QLatin1Char(':')); - QString path = fileName; + QStringRef path(&fileName); if(path.startsWith(QLatin1Char(':'))) path = path.mid(1); if(path.startsWith(QLatin1Char('/'))) { - that->load(path); + that->load(path.toString()); } else { QMutexLocker lock(resourceMutex()); QStringList searchPaths = *resourceSearchPaths(); diff --git a/src/corelib/io/qstandardpaths_unix.cpp b/src/corelib/io/qstandardpaths_unix.cpp index bcbc9664ef..0561e5833f 100644 --- a/src/corelib/io/qstandardpaths_unix.cpp +++ b/src/corelib/io/qstandardpaths_unix.cpp @@ -223,7 +223,7 @@ QString QStandardPaths::writableLocation(StandardLocation type) if (!value.isEmpty()) { // value can start with $HOME if (value.startsWith(QLatin1String("$HOME"))) - value = QDir::homePath() + value.mid(5); + value = QDir::homePath() + value.midRef(5); if (value.length() > 1 && value.endsWith(QLatin1Char('/'))) value.chop(1); return value; diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 40fc492d91..e20a98fbf1 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -3784,13 +3784,13 @@ QUrl QUrl::fromLocalFile(const QString &localFile) } else if (deslashified.startsWith(QLatin1String("//"))) { // magic for shared drive on windows int indexOfPath = deslashified.indexOf(QLatin1Char('/'), 2); - QString hostSpec = deslashified.mid(2, indexOfPath - 2); + QStringRef hostSpec = deslashified.midRef(2, indexOfPath - 2); // Check for Windows-specific WebDAV specification: "//host@SSL/path". if (hostSpec.endsWith(webDavSslTag(), Qt::CaseInsensitive)) { - hostSpec.chop(4); + hostSpec.truncate(hostSpec.size() - 4); scheme = webDavScheme(); } - url.setHost(hostSpec); + url.setHost(hostSpec.toString()); if (indexOfPath > 2) deslashified = deslashified.right(deslashified.length() - indexOfPath); @@ -4255,8 +4255,8 @@ QUrl QUrl::fromUserInput(const QString &userInput) if (urlPrepended.isValid() && (!urlPrepended.host().isEmpty() || !urlPrepended.path().isEmpty())) { int dotIndex = trimmedString.indexOf(QLatin1Char('.')); - const QString hostscheme = trimmedString.left(dotIndex).toLower(); - if (hostscheme == ftpScheme()) + const QStringRef hostscheme = trimmedString.leftRef(dotIndex); + if (hostscheme.compare(ftpScheme(), Qt::CaseInsensitive) == 0) urlPrepended.setScheme(ftpScheme()); return adjustFtpPath(urlPrepended); } |