summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qiodevice.cpp
diff options
context:
space:
mode:
authorJoão Abecasis <joao@abecasis.name>2009-11-16 12:07:53 (GMT)
committerJoão Abecasis <joao@abecasis.name>2009-11-17 13:00:51 (GMT)
commit98a05681851db9d88b1364af52be543715fbe306 (patch)
treeea6822c15c8fee57288369cc5745616e63a88d84 /src/corelib/io/qiodevice.cpp
parentd5eb850f8c2524aa5573bb4e672b195724e5b5ad (diff)
downloadQt-98a05681851db9d88b1364af52be543715fbe306.zip
Qt-98a05681851db9d88b1364af52be543715fbe306.tar.gz
Qt-98a05681851db9d88b1364af52be543715fbe306.tar.bz2
Fix QIODevice::getChar optimization
In Text mode there would be a huge penalty on each '\r' found even if the internal buffer was not exhausted, because we would repeatedly remove the '\r' from the buffer and put it back it. Before following through to the unoptimized code, anyway. Instead, we now loop over the internal buffer until we find a suitable character. Reduced code duplication by having QIODevice::getChar directly call QIODevice::read and letting compilers do their job. Reviewed-by: Markus Goetz
Diffstat (limited to 'src/corelib/io/qiodevice.cpp')
-rw-r--r--src/corelib/io/qiodevice.cpp58
1 files changed, 12 insertions, 46 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index 856d737..7ee65e1 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -757,22 +757,20 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
// Short circuit for getChar()
if (maxSize == 1) {
- int chint = d->buffer.getChar();
- if (chint != -1) {
+ int chint;
+ while ((chint = d->buffer.getChar()) != -1) {
+ if (!sequential)
+ ++d->pos;
+
char c = char(uchar(chint));
- if (c == '\r' && (d->openMode & Text)) {
- d->buffer.ungetChar(c);
- } else {
- if (data)
- *data = c;
- if (!sequential)
- ++d->pos;
+ if (c == '\r' && (d->openMode & Text))
+ continue;
+ *data = c;
#if defined QIODEVICE_DEBUG
- printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
- int(c), isprint(c) ? c : '?');
+ printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
+ int(c), isprint(c) ? c : '?');
#endif
- return qint64(1);
- }
+ return qint64(1);
}
}
@@ -1379,40 +1377,8 @@ bool QIODevicePrivate::putCharHelper(char c)
*/
bool QIODevice::getChar(char *c)
{
- Q_D(QIODevice);
- const OpenMode openMode = d->openMode;
- if (!(openMode & ReadOnly)) {
- if (openMode == NotOpen)
- qWarning("QIODevice::getChar: Closed device");
- else
- qWarning("QIODevice::getChar: WriteOnly device");
- return false;
- }
-
- // Shortcut for QIODevice::read(c, 1)
- QRingBuffer *buffer = &d->buffer;
- const int chint = buffer->getChar();
- if (chint != -1) {
- char ch = char(uchar(chint));
- if ((openMode & Text) && ch == '\r') {
- buffer->ungetChar(ch);
- } else {
- if (c)
- *c = ch;
- if (!d->isSequential())
- ++d->pos;
- return true;
- }
- }
-
- // Fall back to read().
char ch;
- if (read(&ch, 1) == 1) {
- if (c)
- *c = ch;
- return true;
- }
- return false;
+ return (1 == read(c ? c : &ch, 1));
}
/*!