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 <marc.mutz@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Rym Bouabid 2024-08-13 12:42:01 +02:00
parent 69e6ae1670
commit f0aa391ef8
8 changed files with 24 additions and 23 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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))

View File

@ -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;

View File

@ -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();

View File

@ -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);

View File

@ -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<QByteArray> prop(line.split('='));
if (prop.size() > 1) {
GradleProperties::iterator it = properties.find(prop.at(0).trimmed());

View File

@ -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) {