diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qringbuffer_p.h | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index 8fe56323a3..0066d3d1b6 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -91,11 +91,20 @@ public: int blockSize = buffers.first().size() - head; if (tailBuffer == 0 || blockSize > bytes) { - bufferSize -= bytes; - if (bufferSize <= 0) - clear(); // try to minify/squeeze us - else + // keep a single block around if it does not exceed + // the basic block size, to avoid repeated allocations + // between uses of the buffer + if (bufferSize <= bytes) { + if (buffers.first().size() <= basicBlockSize) { + bufferSize = 0; + head = tail = 0; + } else { + clear(); // try to minify/squeeze us + } + } else { head += bytes; + bufferSize -= bytes; + } return; } @@ -139,11 +148,20 @@ public: inline void chop(int bytes) { while (bytes > 0) { if (tailBuffer == 0 || tail > bytes) { - bufferSize -= bytes; - if (bufferSize <= 0) - clear(); // try to minify/squeeze us - else + // keep a single block around if it does not exceed + // the basic block size, to avoid repeated allocations + // between uses of the buffer + if (bufferSize <= bytes) { + if (buffers.first().size() <= basicBlockSize) { + bufferSize = 0; + head = tail = 0; + } else { + clear(); // try to minify/squeeze us + } + } else { tail -= bytes; + bufferSize -= bytes; + } return; } |