diff options
| author | Marc Mutz <marc.mutz@qt.io> | 2022-08-17 13:34:40 +0200 |
|---|---|---|
| committer | Marc Mutz <marc.mutz@qt.io> | 2022-08-20 21:22:53 +0200 |
| commit | 52c5f286950a56a8714a0be9b8ecb088f07d7eb8 (patch) | |
| tree | fbbe69c6dd10a71e20b94175b23000a65590ad48 /src | |
| parent | 817e47fbcde21ab54d353efd647685159cb437a9 (diff) | |
QBuffer: optimize setData(ptr, n)
The old code always created a new QByteArray, always allocating
memory.
The new call assigns the data to the existing QByteArray, enabling
potential re-use of the internal QByteArray's buffer. Since QByteArray
is missing the STL-style assign() function, abuse replace() for this
task.
Change-Id: I357f11bad0a976d4d0fb2faeb93f8b2262fa5a65
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/io/qbuffer.cpp | 12 | ||||
| -rw-r--r-- | src/corelib/io/qbuffer.h | 3 |
2 files changed, 11 insertions, 4 deletions
diff --git a/src/corelib/io/qbuffer.cpp b/src/corelib/io/qbuffer.cpp index 0dcc02459da..ba84428275e 100644 --- a/src/corelib/io/qbuffer.cpp +++ b/src/corelib/io/qbuffer.cpp @@ -267,8 +267,6 @@ void QBuffer::setData(const QByteArray &data) } /*! - \fn void QBuffer::setData(const char *data, qsizetype size) - \overload Sets the contents of the internal buffer to be the first \a size @@ -277,6 +275,16 @@ void QBuffer::setData(const QByteArray &data) \note In Qt versions prior to 6.5, this function took the length as an \c{int} parameter, potentially truncating sizes. */ +void QBuffer::setData(const char *data, qsizetype size) +{ + Q_D(QBuffer); + if (isOpen()) { + qWarning("QBuffer::setData: Buffer is open"); + return; + } + d->buf->replace(qsizetype(0), d->buf->size(), // ### QByteArray lacks assign(ptr, n) + data, size); +} /*! \reimp diff --git a/src/corelib/io/qbuffer.h b/src/corelib/io/qbuffer.h index a7c6bde562a..4cbbfe7c52d 100644 --- a/src/corelib/io/qbuffer.h +++ b/src/corelib/io/qbuffer.h @@ -36,8 +36,7 @@ public: #if QT_CORE_REMOVED_SINCE(6, 5) && QT_POINTER_SIZE != 4 void setData(const char *data, int len) { setData(data, qsizetype(len)); } #endif - void setData(const char *data, qsizetype len) - { setData(QByteArray(data, len)); } + void setData(const char *data, qsizetype len); const QByteArray &data() const; bool open(OpenMode openMode) override; |
