From f0aa391ef89a393221d77d5ad3c1616a4727f11a Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Tue, 13 Aug 2024 12:42:01 +0200 Subject: [PATCH] Use QIODevice::readLineInto() instead of readLine() in loops Most of the callers of QIODevice::readLine() are reading a device line by line in a loop. Instead, use one QByteArray and modify it in every iteration using QIODevice::readLineInto(). Use a QByteArrayView instead of QByteArray when calling trimmed() as it's an expensive operation. Fixes: QTBUG-103108 Change-Id: Ic1af487a2fbf352cc21d76a41717944d034d3709 Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira --- src/corelib/mimetypes/qmimeprovider.cpp | 4 ++-- src/corelib/time/qtimezoneprivate_tz.cpp | 8 ++++---- src/network/kernel/qhostinfo_unix.cpp | 4 ++-- .../ibus/qibusplatforminputcontext.cpp | 7 ++++--- src/testlib/qbenchmarkvalgrind.cpp | 4 ++-- src/testlib/qtestblacklist.cpp | 4 ++-- src/tools/androiddeployqt/main.cpp | 9 +++++---- src/tools/windeployqt/utils.cpp | 7 +++---- 8 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp index 0a3d858486c..d12449c3988 100644 --- a/src/corelib/mimetypes/qmimeprovider.cpp +++ b/src/corelib/mimetypes/qmimeprovider.cpp @@ -458,8 +458,8 @@ void QMimeBinaryProvider::loadMimeTypeList() // So we have to parse the plain-text files called "types". QFile file(m_directory + QStringView(u"/types")); if (file.open(QIODevice::ReadOnly)) { - while (!file.atEnd()) { - const QByteArray line = file.readLine(); + QByteArray line; + while (file.readLineInto(&line)) { auto lineView = QByteArrayView(line); if (lineView.endsWith('\n')) lineView.chop(1); diff --git a/src/corelib/time/qtimezoneprivate_tz.cpp b/src/corelib/time/qtimezoneprivate_tz.cpp index 23916be1792..4a238110e9d 100644 --- a/src/corelib/time/qtimezoneprivate_tz.cpp +++ b/src/corelib/time/qtimezoneprivate_tz.cpp @@ -88,12 +88,12 @@ static QTzTimeZoneHash loadTzTimeZones() return QTzTimeZoneHash(); QTzTimeZoneHash zonesHash; - while (!tzif.atEnd()) { - const QByteArray line = tzif.readLine().trimmed(); - if (line.isEmpty() || line.at(0) == '#') // Ignore empty or comment + QByteArray line; + while (tzif.readLineInto(&line)) { + QByteArrayView text = QByteArrayView(line).trimmed(); + if (text.isEmpty() || text.at(0) == '#') // Ignore empty or comment continue; // Data rows are tab-separated columns Region, Coordinates, ID, Optional Comments - QByteArrayView text(line); int cut = text.indexOf('\t'); if (Q_LIKELY(cut > 0)) { QTzTimeZone zone; diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index 80d386a13d9..3b0e2d333aa 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -123,8 +123,8 @@ QString QHostInfo::localDomainName() return QString(); // failure QString domainName; - while (!resolvconf.atEnd()) { - const QByteArray lineArray = resolvconf.readLine(); + QByteArray lineArray; + while (resolvconf.readLineInto(&lineArray)) { QByteArrayView line = QByteArrayView(lineArray).trimmed(); constexpr QByteArrayView domainWithSpace = "domain "; if (line.startsWith(domainWithSpace)) diff --git a/src/plugins/platforminputcontexts/ibus/qibusplatforminputcontext.cpp b/src/plugins/platforminputcontexts/ibus/qibusplatforminputcontext.cpp index 00c7884cda2..c38cb289c19 100644 --- a/src/plugins/platforminputcontexts/ibus/qibusplatforminputcontext.cpp +++ b/src/plugins/platforminputcontexts/ibus/qibusplatforminputcontext.cpp @@ -776,11 +776,12 @@ void QIBusPlatformInputContextPrivate::createConnection() if (!file.open(QFile::ReadOnly)) return; - QByteArray address; + QByteArrayView address; int pid = -1; + QByteArray lineArray; - while (!file.atEnd()) { - QByteArray line = file.readLine().trimmed(); + while (file.readLineInto(&lineArray)) { + QByteArrayView line = QByteArrayView(lineArray).trimmed(); if (line.startsWith('#')) continue; diff --git a/src/testlib/qbenchmarkvalgrind.cpp b/src/testlib/qbenchmarkvalgrind.cpp index 1663c5d28d2..33479f135d7 100644 --- a/src/testlib/qbenchmarkvalgrind.cpp +++ b/src/testlib/qbenchmarkvalgrind.cpp @@ -60,8 +60,8 @@ qint64 QBenchmarkValgrindUtils::extractResult(const QString &fileName) Q_UNUSED(openOk); std::optional val = std::nullopt; - while (!file.atEnd()) { - const QByteArray line = file.readLine(); + QByteArray line; + while (file.readLineInto(&line)) { constexpr QByteArrayView tag = "summary: "; if (line.startsWith(tag)) { const auto maybeNumber = line.data() + tag.size(); diff --git a/src/testlib/qtestblacklist.cpp b/src/testlib/qtestblacklist.cpp index 7d848b3f1c3..7567e467469 100644 --- a/src/testlib/qtestblacklist.cpp +++ b/src/testlib/qtestblacklist.cpp @@ -252,9 +252,9 @@ void parseBlackList() return; QByteArray function; + QByteArray line; - while (!ignored.atEnd()) { - QByteArray line = ignored.readLine(); + while (ignored.readLineInto(&line)) { const int commentPosition = line.indexOf('#'); if (commentPosition >= 0) line.truncate(commentPosition); diff --git a/src/tools/androiddeployqt/main.cpp b/src/tools/androiddeployqt/main.cpp index 98324ab5b25..cf13c5e781a 100644 --- a/src/tools/androiddeployqt/main.cpp +++ b/src/tools/androiddeployqt/main.cpp @@ -2837,8 +2837,9 @@ QStringList getLibraryProjectsInOutputFolder(const Options &options) QFile file(options.outputDirectory + "/project.properties"_L1); if (file.open(QIODevice::ReadOnly)) { - while (!file.atEnd()) { - QByteArray line = file.readLine().trimmed(); + QByteArray lineArray; + while (file.readLineInto(&lineArray)) { + QByteArrayView line = QByteArrayView(lineArray).trimmed(); if (line.startsWith("android.library.reference")) { int equalSignIndex = line.indexOf('='); if (equalSignIndex >= 0) { @@ -2913,8 +2914,8 @@ static bool mergeGradleProperties(const QString &path, GradleProperties properti QFile oldFile(oldPathStr); if (oldFile.open(QIODevice::ReadOnly)) { - while (!oldFile.atEnd()) { - QByteArray line(oldFile.readLine()); + QByteArray line; + while (oldFile.readLineInto(&line)) { QList prop(line.split('=')); if (prop.size() > 1) { GradleProperties::iterator it = properties.find(prop.at(0).trimmed()); diff --git a/src/tools/windeployqt/utils.cpp b/src/tools/windeployqt/utils.cpp index 25b4a9788b2..ea32346c65f 100644 --- a/src/tools/windeployqt/utils.cpp +++ b/src/tools/windeployqt/utils.cpp @@ -225,10 +225,9 @@ QMap queryQtPaths(const QString &qtpathsBinary, QString *error } QFile qconfigPriFile(result.value(QStringLiteral("QT_HOST_DATA")) + QStringLiteral("/mkspecs/qconfig.pri")); if (qconfigPriFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - while (true) { - const QByteArray line = qconfigPriFile.readLine(); - if (line.isEmpty()) - break; + QByteArray lineArray; + while (qconfigPriFile.readLineInto(&lineArray)) { + QByteArrayView line = QByteArrayView(lineArray); if (line.startsWith("QT_LIBINFIX")) { const int pos = line.indexOf('='); if (pos >= 0) {