Skip to content

Commit

Permalink
Switch zlib streams to lazy init
Browse files Browse the repository at this point in the history
  • Loading branch information
puzpuzpuz committed Jun 23, 2020
1 parent b8ea471 commit b4be58d
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/node_zlib.cc
Expand Up @@ -157,7 +157,9 @@ class ZlibContext : public MemoryRetainer {
private:
CompressionError ErrorForMessage(const char* message) const;
CompressionError SetDictionary();
void LazyInit();

bool init_done_ = false;
int err_ = 0;
int flush_ = 0;
int level_ = 0;
Expand Down Expand Up @@ -724,6 +726,12 @@ using BrotliEncoderStream = BrotliCompressionStream<BrotliEncoderContext>;
using BrotliDecoderStream = BrotliCompressionStream<BrotliDecoderContext>;

void ZlibContext::Close() {
if (!init_done_) {
dictionary_.clear();
mode_ = NONE;
return;
}

CHECK_LE(mode_, UNZIP);

int status = Z_OK;
Expand All @@ -742,6 +750,11 @@ void ZlibContext::Close() {


void ZlibContext::DoThreadPoolWork() {
LazyInit();
if (err_ != Z_OK) {
return;
}

const Bytef* next_expected_header_byte = nullptr;

// If the avail_out is left at 0, then it means that it ran out
Expand Down Expand Up @@ -974,6 +987,17 @@ CompressionError ZlibContext::Init(
window_bits_ *= -1;
}

dictionary_ = std::move(dictionary);

return CompressionError {};
}

void ZlibContext::LazyInit() {
if (init_done_) {
return;
}
init_done_ = true;

switch (mode_) {
case DEFLATE:
case GZIP:
Expand All @@ -995,15 +1019,12 @@ CompressionError ZlibContext::Init(
UNREACHABLE();
}

dictionary_ = std::move(dictionary);

if (err_ != Z_OK) {
dictionary_.clear();
mode_ = NONE;
return ErrorForMessage("zlib error");
}

return SetDictionary();
SetDictionary();
}


Expand Down

0 comments on commit b4be58d

Please sign in to comment.