QRingBuffer: cache the last released block
A typical ring buffer usage is a sequence of reserve()->chop()->read() cycles. Usually, between these cycles, the buffer doesn't contain data and all blocks are released. To reduce reallocations, keep the most recently used block while the buffer is empty. Change-Id: I8128f1f04649ae005fd0a480f17f95de01a9a135 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
parent
bb4625f472
commit
e06f4c2558
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user