From 98a05681851db9d88b1364af52be543715fbe306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Mon, 16 Nov 2009 13:07:53 +0100 Subject: 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 --- src/corelib/io/qiodevice.cpp | 58 +++++++++----------------------------------- 1 file 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)); } /*! -- cgit v0.12