From 612417ea701b5276abd392af383fff3133b95a47 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 Dec 2021 23:34:36 +0100 Subject: [PATCH] QRingBuffer: optimize and simplify QRingChunk::toByteArray() futher - use sliced() instead of mid(), since we know we call sliced() in-contract - replace the manual memmove() with calls to QBA::remove(), but first resize() to tailOffset, and only then remove(0, headOffset), so we only move Chunk::size() bytes, as the old code did - don't bother updating the offset members, as we're restricted to rvalues now, and we only need to leave behind a partially-formed object This code shows that we desperately need QBA::slice() and/or an rvalue overload of QBA::sliced(), cf. QTBUG-99218. Add a comment to use these functions when they become available. Change-Id: I5d8c7363d443dff69338f6f6a7b4bff9957917ec Reviewed-by: Lars Knoll Reviewed-by: Alex Trotsenko Reviewed-by: Oswald Buddenhagen --- src/corelib/tools/qringbuffer.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp index 96ba5c2b44..d3d71dd9e2 100644 --- a/src/corelib/tools/qringbuffer.cpp +++ b/src/corelib/tools/qringbuffer.cpp @@ -71,18 +71,13 @@ void QRingChunk::detach() QByteArray QRingChunk::toByteArray() && { + // ### Replace with std::move(chunk).sliced(head(), size()) once sliced()&& is available if (headOffset != 0 || tailOffset != chunk.size()) { if (isShared()) - return chunk.mid(headOffset, size()); - - if (headOffset != 0) { - char *ptr = chunk.data(); - ::memmove(ptr, ptr + headOffset, size()); - tailOffset -= headOffset; - headOffset = 0; - } + return chunk.sliced(head(), size()); chunk.resize(tailOffset); + chunk.remove(0, headOffset); } return std::move(chunk);