Skip to content

Commit

Permalink
Use QIODevice::readLineInto() instead of readLine() in loops
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
Rym Bouabid committed Sep 20, 2024
1 parent 69e6ae1 commit f0aa391
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/corelib/mimetypes/qmimeprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/corelib/time/qtimezoneprivate_tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/network/kernel/qhostinfo_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/testlib/qbenchmarkvalgrind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ qint64 QBenchmarkValgrindUtils::extractResult(const QString &fileName)
Q_UNUSED(openOk);

std::optional<qint64> 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();
Expand Down
4 changes: 2 additions & 2 deletions src/testlib/qtestblacklist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/tools/androiddeployqt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<QByteArray> prop(line.split('='));
if (prop.size() > 1) {
GradleProperties::iterator it = properties.find(prop.at(0).trimmed());
Expand Down
7 changes: 3 additions & 4 deletions src/tools/windeployqt/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,9 @@ QMap<QString, QString> 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) {
Expand Down

0 comments on commit f0aa391

Please sign in to comment.