From 154f2ba23c87a0d316e3a9c263efa7dd3bbef6ce Mon Sep 17 00:00:00 2001 From: Markus Goetz Date: Tue, 22 Sep 2009 13:15:56 +0200 Subject: QRingBuffer: Try to minimize memory consumption while it is empty Try not to hold an unused 4k QByteArray. This may introduce a performance regression e.g. for QNetworkReply in exchange for lower memory usage. We will see if this is really the case. Reviewed-by: Thiago --- src/corelib/tools/qringbuffer_p.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index b2df054..c44346c 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -147,9 +147,20 @@ public: --tailBuffer; head = 0; } + + if (isEmpty()) + clear(); // try to minify/squeeze us } inline char *reserve(int bytes) { + // if this is a fresh empty QRingBuffer + if (bufferSize == 0) { + buffers[0].resize(qMax(basicBlockSize, bytes)); + bufferSize += bytes; + tail = bytes; + return buffers[tailBuffer].data(); + } + bufferSize += bytes; // if there is already enough space, simply return. @@ -208,6 +219,9 @@ public: --tailBuffer; tail = buffers.at(tailBuffer).size(); } + + if (isEmpty()) + clear(); // try to minify/squeeze us } inline bool isEmpty() const { @@ -244,11 +258,10 @@ public: } inline void clear() { - if(!buffers.isEmpty()) { - buffers.erase(buffers.begin() + 1, buffers.end()); - if (buffers.at(0).size() != basicBlockSize) - buffers[0].resize(basicBlockSize); - } + buffers.erase(buffers.begin() + 1, buffers.end()); + buffers[0].resize(0); + buffers[0].squeeze(); + head = tail = 0; tailBuffer = 0; bufferSize = 0; -- cgit v0.12