From 360b00618368ff6f6f1bfd5bfe1385e778f35723 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 31 Mar 2021 15:21:58 +0300 Subject: [PATCH] optimal buffer usage in qFastCompress() and qFastUncompress() Signed-off-by: Ivailo Monev --- src/core/tools/qbytearray.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/core/tools/qbytearray.cpp b/src/core/tools/qbytearray.cpp index 466d50082..d31e2d427 100644 --- a/src/core/tools/qbytearray.cpp +++ b/src/core/tools/qbytearray.cpp @@ -538,15 +538,14 @@ QByteArray qFastCompress(const char* data, int nbytes, int compressionLevel) return QByteArray(); } - QByteArray result(bndresult, Qt::Uninitialized); - const size_t cmpresult = ZSTD_compress(result.data(), result.size(), data, nbytes, compressionLevel); + char cmpbuffer[bndresult]; + const size_t cmpresult = ZSTD_compress(cmpbuffer, bndresult, data, nbytes, compressionLevel); if (Q_UNLIKELY(ZSTD_isError(cmpresult))) { qWarning("qFastCompress: Could not compress data (%s)", ZSTD_getErrorString(ZSTD_getErrorCode(cmpresult))); return QByteArray(); } - result.resize(cmpresult); - return result; + return QByteArray(cmpbuffer, cmpresult); } /*! @@ -591,14 +590,14 @@ QByteArray qFastUncompress(const char* data, int nbytes) return QByteArray(); } - QByteArray result(uncompressedsize, Qt::Uninitialized); - const size_t decresult = ZSTD_decompress(result.data(), result.size(), data, nbytes); + char decbuffer[uncompressedsize]; + const size_t decresult = ZSTD_decompress(decbuffer, uncompressedsize, data, nbytes); if (Q_UNLIKELY(ZSTD_isError(decresult))) { qWarning("qFastCompress: Could not uncompress data (%s)", ZSTD_getErrorString(ZSTD_getErrorCode(decresult))); return QByteArray(); } - return result; + return QByteArray(decbuffer, uncompressedsize); } #endif // QT_NO_COMPRESS -- 2.11.0