Browse Source

Avoid redundant buffer copying

PR #19272.
adaptive-webui-19844
Chocobo1 1 year ago committed by GitHub
parent
commit
66e533f505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      src/base/utils/gzip.cpp

60
src/base/utils/gzip.cpp

@ -31,6 +31,7 @@
#include <vector> #include <vector>
#include <QtGlobal>
#include <QByteArray> #include <QByteArray>
#ifndef ZLIB_CONST #ifndef ZLIB_CONST
@ -40,63 +41,46 @@
QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *ok) QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *ok)
{ {
if (ok) *ok = false; if (ok)
*ok = false;
if (data.isEmpty()) if (data.isEmpty())
return {}; return {};
const int BUFSIZE = 128 * 1024;
std::vector<char> tmpBuf(BUFSIZE);
z_stream strm {}; z_stream strm {};
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
strm.next_in = reinterpret_cast<const Bytef *>(data.constData()); strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
strm.avail_in = uInt(data.size()); strm.avail_in = static_cast<uInt>(data.size());
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
strm.avail_out = BUFSIZE;
// windowBits = 15 + 16 to enable gzip // windowBits = 15 + 16 to enable gzip
// From the zlib manual: windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits // From the zlib manual: windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits
// to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper. // to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
int result = deflateInit2(&strm, level, Z_DEFLATED, (15 + 16), 9, Z_DEFAULT_STRATEGY); const int initResult = deflateInit2(&strm, level, Z_DEFLATED, (15 + 16), 9, Z_DEFAULT_STRATEGY);
if (result != Z_OK) if (initResult != Z_OK)
return {}; return {};
QByteArray output; #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
output.reserve(deflateBound(&strm, data.size())); QByteArray ret {static_cast<qsizetype>(deflateBound(&strm, data.size())), Qt::Uninitialized};
#else
// feed to deflate QByteArray ret {static_cast<int>(deflateBound(&strm, data.size())), Qt::Uninitialized};
while (strm.avail_in > 0) #endif
{ strm.next_out = reinterpret_cast<Bytef *>(ret.data());
result = deflate(&strm, Z_NO_FLUSH); strm.avail_out = ret.size();
if (result != Z_OK)
{
deflateEnd(&strm);
return {};
}
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
strm.avail_out = BUFSIZE;
}
// flush the rest from deflate
while (result != Z_STREAM_END)
{
result = deflate(&strm, Z_FINISH);
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out)); // From the zlib manual: Z_FINISH can be used in the first deflate call after deflateInit if all the compression
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data()); // is to be done in a single step. In order to complete in one call, avail_out must be at least the value
strm.avail_out = BUFSIZE; // returned by deflateBound (see below). Then deflate is guaranteed to return Z_STREAM_END.
} const int deflateResult = deflate(&strm, Z_FINISH);
Q_ASSERT(deflateResult == Z_STREAM_END);
deflateEnd(&strm); deflateEnd(&strm);
ret.truncate(strm.total_out);
if (ok) *ok = true; if (ok)
return output; *ok = true;
return ret;
} }
QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok) QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok)

Loading…
Cancel
Save