QIODevice: remove ineffective caching members

Most of the QIODevice functions already have a locally cached
"sequential" flag. Make the rest of them follow this strategy.
This eliminates the need to use private caching members.

Change-Id: I0edb2c9b7c5f411c5bee25c425e7b40e3c9021d3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: hjk <hjk@theqtcompany.com>
This commit is contained in:
Alex Trotsenko 2014-10-08 17:34:33 +03:00
parent bdcb3f9aab
commit a373ffcda9
4 changed files with 20 additions and 21 deletions

View File

@ -37,7 +37,7 @@ QT_BEGIN_NAMESPACE
// Only add to the end, and bump version if you do. // Only add to the end, and bump version if you do.
quintptr Q_CORE_EXPORT qtHookData[] = { quintptr Q_CORE_EXPORT qtHookData[] = {
1, // hook data version 2, // hook data version
QHooks::LastHookIndex, // size of qtHookData QHooks::LastHookIndex, // size of qtHookData
QT_VERSION, QT_VERSION,

View File

@ -117,8 +117,7 @@ void debugBinaryString(const char *data, qint64 maxlen)
*/ */
QIODevicePrivate::QIODevicePrivate() QIODevicePrivate::QIODevicePrivate()
: openMode(QIODevice::NotOpen), buffer(QIODEVICE_BUFFERSIZE), : openMode(QIODevice::NotOpen), buffer(QIODEVICE_BUFFERSIZE),
pos(0), devicePos(0), seqDumpPos(0) pos(0), devicePos(0)
, pPos(&pos), pDevicePos(&devicePos)
, baseReadLineDataCalled(false) , baseReadLineDataCalled(false)
, firstRead(true) , firstRead(true)
, accessMode(Unset) , accessMode(Unset)
@ -566,7 +565,6 @@ void QIODevice::close()
d->openMode = NotOpen; d->openMode = NotOpen;
d->errorString.clear(); d->errorString.clear();
d->pos = 0; d->pos = 0;
d->seqDumpPos = 0;
d->buffer.clear(); d->buffer.clear();
d->firstRead = true; d->firstRead = true;
} }
@ -755,11 +753,14 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
this, data, int(maxSize), int(d->pos), int(d->buffer.size())); this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
#endif #endif
const bool sequential = d->isSequential();
// Short circuit for getChar() // Short circuit for getChar()
if (maxSize == 1) { if (maxSize == 1) {
int chint; int chint;
while ((chint = d->buffer.getChar()) != -1) { while ((chint = d->buffer.getChar()) != -1) {
++(*d->pPos); if (!sequential)
++d->pos;
char c = char(uchar(chint)); char c = char(uchar(chint));
if (c == '\r' && (d->openMode & Text)) if (c == '\r' && (d->openMode & Text))
@ -784,7 +785,8 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
// Try reading from the buffer. // Try reading from the buffer.
qint64 bufferReadChunkSize = d->buffer.read(data, maxSize); qint64 bufferReadChunkSize = d->buffer.read(data, maxSize);
if (bufferReadChunkSize > 0) { if (bufferReadChunkSize > 0) {
*d->pPos += bufferReadChunkSize; if (!sequential)
d->pos += bufferReadChunkSize;
readSoFar += bufferReadChunkSize; readSoFar += bufferReadChunkSize;
data += bufferReadChunkSize; data += bufferReadChunkSize;
maxSize -= bufferReadChunkSize; maxSize -= bufferReadChunkSize;
@ -798,17 +800,13 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
// for fast pos updates. // for fast pos updates.
CHECK_READABLE(read, qint64(-1)); CHECK_READABLE(read, qint64(-1));
d->firstRead = false; d->firstRead = false;
if (d->isSequential()) {
d->pPos = &d->seqDumpPos;
d->pDevicePos = &d->seqDumpPos;
}
} }
} }
if (maxSize > 0 && !deviceAtEof) { if (maxSize > 0 && !deviceAtEof) {
qint64 readFromDevice = 0; qint64 readFromDevice = 0;
// Make sure the device is positioned correctly. // Make sure the device is positioned correctly.
if (d->pos == d->devicePos || d->isSequential() || seek(d->pos)) { if (sequential || d->pos == d->devicePos || seek(d->pos)) {
madeBufferReadsOnly = false; // fix readData attempt madeBufferReadsOnly = false; // fix readData attempt
if (maxSize >= QIODEVICE_BUFFERSIZE || (d->openMode & Unbuffered)) { if (maxSize >= QIODEVICE_BUFFERSIZE || (d->openMode & Unbuffered)) {
// Read big chunk directly to output buffer // Read big chunk directly to output buffer
@ -822,8 +820,10 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
readSoFar += readFromDevice; readSoFar += readFromDevice;
data += readFromDevice; data += readFromDevice;
maxSize -= readFromDevice; maxSize -= readFromDevice;
*d->pPos += readFromDevice; if (!sequential) {
*d->pDevicePos += readFromDevice; d->pos += readFromDevice;
d->devicePos += readFromDevice;
}
} }
} else { } else {
const int bytesToBuffer = QIODEVICE_BUFFERSIZE; const int bytesToBuffer = QIODEVICE_BUFFERSIZE;
@ -832,7 +832,8 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
deviceAtEof = (readFromDevice != bytesToBuffer); deviceAtEof = (readFromDevice != bytesToBuffer);
d->buffer.chop(bytesToBuffer - qMax(0, int(readFromDevice))); d->buffer.chop(bytesToBuffer - qMax(0, int(readFromDevice)));
if (readFromDevice > 0) { if (readFromDevice > 0) {
*d->pDevicePos += readFromDevice; if (!sequential)
d->devicePos += readFromDevice;
#if defined QIODEVICE_DEBUG #if defined QIODEVICE_DEBUG
printf("%p \treading %d from device into buffer\n", this, printf("%p \treading %d from device into buffer\n", this,
int(readFromDevice)); int(readFromDevice));
@ -1414,7 +1415,8 @@ qint64 QIODevicePrivate::peek(char *data, qint64 maxSize)
return readBytes; return readBytes;
buffer.ungetBlock(data, readBytes); buffer.ungetBlock(data, readBytes);
*pPos -= readBytes; if (!isSequential())
pos -= readBytes;
return readBytes; return readBytes;
} }
@ -1429,7 +1431,8 @@ QByteArray QIODevicePrivate::peek(qint64 maxSize)
return result; return result;
buffer.ungetBlock(result.constData(), result.size()); buffer.ungetBlock(result.constData(), result.size());
*pPos -= result.size(); if (!isSequential())
pos -= result.size();
return result; return result;
} }

View File

@ -209,10 +209,6 @@ public:
QIODevicePrivateLinearBuffer buffer; QIODevicePrivateLinearBuffer buffer;
qint64 pos; qint64 pos;
qint64 devicePos; qint64 devicePos;
// these three are for fast position updates during read, avoiding isSequential test
qint64 seqDumpPos;
qint64 *pPos;
qint64 *pDevicePos;
bool baseReadLineDataCalled; bool baseReadLineDataCalled;
bool firstRead; bool firstRead;

View File

@ -114,7 +114,7 @@ void tst_toolsupport::offsets_data()
if (sizeof(void *) == 8) { if (sizeof(void *) == 8) {
QTestData &data = QTest::newRow("QFilePrivate::fileName") QTestData &data = QTest::newRow("QFilePrivate::fileName")
<< pmm_to_offsetof(&QFilePrivate::fileName); << pmm_to_offsetof(&QFilePrivate::fileName);
data << -1 << 272; data << -1 << 248;
} }
#endif #endif