Skip to content

Commit

Permalink
Add compression buffer size customization (#644)
Browse files Browse the repository at this point in the history
* add compression buffer size customization and small brotli refactor

* allocat brotli buffer once

* add init to brotli decoder buffer
  • Loading branch information
Fefer-Ivan authored Sep 8, 2020
1 parent 9d12b3f commit 3da4a0a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
#endif

#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ
#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u)
#endif

#ifndef CPPHTTPLIB_THREAD_POOL_COUNT
#define CPPHTTPLIB_THREAD_POOL_COUNT \
((std::max)(8u, std::thread::hardware_concurrency() > 0 \
Expand Down Expand Up @@ -2226,7 +2230,7 @@ class gzip_compressor : public compressor {

int ret = Z_OK;

std::array<char, 16384> buff{};
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
do {
strm_.avail_out = buff.size();
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
Expand Down Expand Up @@ -2277,7 +2281,7 @@ class gzip_decompressor : public decompressor {
strm_.avail_in = static_cast<decltype(strm_.avail_in)>(data_length);
strm_.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));

std::array<char, 16384> buff{};
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
while (strm_.avail_in > 0) {
strm_.avail_out = buff.size();
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
Expand Down Expand Up @@ -2315,7 +2319,7 @@ class brotli_compressor : public compressor {

bool compress(const char *data, size_t data_length, bool last,
Callback callback) override {
std::array<uint8_t, 16384> buff{};
std::array<uint8_t, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};

auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS;
auto available_in = data_length;
Expand Down Expand Up @@ -2377,18 +2381,18 @@ class brotli_decompressor : public decompressor {

decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;

std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
char output[1024];
char *next_out = output;
size_t avail_out = sizeof(output);
char *next_out = buff.data();
size_t avail_out = buff.size();

decoder_r = BrotliDecoderDecompressStream(
decoder_s, &avail_in, &next_in, &avail_out,
reinterpret_cast<unsigned char **>(&next_out), &total_out);
reinterpret_cast<uint8_t **>(&next_out), &total_out);

if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; }

if (!callback((const char *)output, sizeof(output) - avail_out)) {
if (!callback(buff.data(), buff.size() - avail_out)) {
return false;
}
}
Expand Down

0 comments on commit 3da4a0a

Please sign in to comment.