OSDN Git Service

always read data without intermediate buffer from QIODevice::read()
authorIvailo Monev <xakepa10@gmail.com>
Tue, 1 Nov 2022 19:34:16 +0000 (21:34 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Tue, 1 Nov 2022 19:34:16 +0000 (21:34 +0200)
the main user of QIODevice is QFile and it uses O_DSYNC/O_SYNC for
unbuffered I/O meaning the QIODevice buffer does not benefit it because the
filesystem cache already does that

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/core/io/qiodevice.cpp
tests/auto/qfile/tst_qfile.cpp

index 3040d12..58cd4d6 100644 (file)
@@ -797,37 +797,20 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
             // Make sure the device is positioned correctly.
             if (sequential || d->pos == d->devicePos || seek(d->pos)) {
                 madeBufferReadsOnly = false; // fix readData attempt
-                if (maxSize >= QIODEVICE_BUFFERSIZE || (d->openMode & Unbuffered)) {
-                    // Read big chunk directly to output buffer
-                    readFromDevice = readData(data, maxSize);
-                    deviceAtEof = (readFromDevice != maxSize);
+                // Read big chunk directly to output buffer
+                readFromDevice = readData(data, maxSize);
+                deviceAtEof = (readFromDevice != maxSize);
 #if defined QIODEVICE_DEBUG
-                    printf("%p \treading %lld bytes from device (total %lld)\n", this,
-                           readFromDevice, readSoFar);
+                printf("%p \treading %lld bytes from device (total %lld)\n", this,
+                        readFromDevice, readSoFar);
 #endif
-                    if (readFromDevice > 0) {
-                        readSoFar += readFromDevice;
-                        data += readFromDevice;
-                        maxSize -= readFromDevice;
-                        if (!sequential) {
-                            d->pos += readFromDevice;
-                            d->devicePos += readFromDevice;
-                        }
-                    }
-                } else {
-                    const qint64 bytesToBuffer = QIODEVICE_BUFFERSIZE;
-                    // Try to fill QIODevice buffer by single read
-                    readFromDevice = readData(d->buffer.reserve(bytesToBuffer), bytesToBuffer);
-                    deviceAtEof = (readFromDevice != bytesToBuffer);
-                    d->buffer.chop(bytesToBuffer - qMax(Q_INT64_C(0), readFromDevice));
-                    if (readFromDevice > 0) {
-                        if (!sequential)
-                            d->devicePos += readFromDevice;
-#if defined QIODEVICE_DEBUG
-                       printf("%p \treading %lld from device into buffer\n", this,
-                               readFromDevice);
-#endif
-                        continue;
+                if (readFromDevice > 0) {
+                    readSoFar += readFromDevice;
+                    data += readFromDevice;
+                    maxSize -= readFromDevice;
+                    if (!sequential) {
+                        d->pos += readFromDevice;
+                        d->devicePos += readFromDevice;
                     }
                 }
             } else {
@@ -1168,8 +1151,9 @@ QByteArray QIODevice::readLine(qint64 maxSize)
                 readBytes += readResult;
         } while (readResult == QIODEVICE_BUFFERSIZE
                 && result[int(readBytes - 1)] != '\n');
-    } else
+    } else {
         readBytes = readLine(result.data(), result.size());
+    }
 
     if (readBytes <= 0)
         result.clear();
index 8983212..0b01451 100644 (file)
@@ -824,19 +824,19 @@ void tst_QFile::readAllBuffer()
     QVERIFY( writer.open(QIODevice::ReadWrite | QIODevice::Unbuffered) );
     QVERIFY( reader.open(QIODevice::ReadOnly) );
 
-    QCOMPARE( writer.write(data1), qint64(data1.size()) );
-    QVERIFY( writer.seek(0) );
+    QCOMPARE(writer.write(data1), qint64(data1.size()));
+    QVERIFY(writer.seek(0));
 
     QByteArray result;
-    result = reader.read(18);
-    QCOMPARE( result.size(), 18 );
+    result = reader.read(data1.size());
+    QCOMPARE(result.size(), data1.size());
 
-    QCOMPARE( writer.write(data2), qint64(data2.size()) ); // new data, old version buffered in reader
-    QCOMPARE( writer.write(data2), qint64(data2.size()) ); // new data, unbuffered in reader
+    QCOMPARE(writer.write(data2), qint64(data2.size()));
+    QCOMPARE(writer.write(data2), qint64(data2.size()));
 
     result += reader.readAll();
 
-    QCOMPARE( result, data1 + data2 );
+    QCOMPARE(result, data1 + data2);
 
     QFile::remove(fileName);
 }