Skip to content

Commit bb1aba2

Browse files
authored
GH-51308: [C++] Initialize gzip codec state lazily (#51483)
### Rationale for this change `GZipCodec::Init()` initializes compressor state and then immediately initializes decompressor state, which tears the compressor state down again. Compressor state is fairly expensive to initialize (the `deflateInit2` call allocates ~256KiB), so this is rather wasteful. A caller that wants to compress has to do it twice (because decompressor initialization throws it away), and a caller that only wants to decompress still has to do the compressor setup. Whatever the caller wants to do with the GZipCodec, this is pure waste. ### What changes are included in this PR? `GZipCodec::Init()` now validates the compression level and window size without allocating codec state. Since all the compression and decompression methods have to check the mode anyway before doing any work, this is safe. ### Are these changes tested? Largely relying on existing test coverage; the interesting part is that `InitCompressor()` validated the compression level internally, so this check has to be extracted. Split the unit test that covered this into a few separate cases to make it clearer. ### Are there any user-facing changes? No. ### Was AI used for this PR? Yes, both to find the issue and to fix it (Codex). **PR code and description written by:** - [X] Human (PR desc) - [X] AI (code) **Reviewed before submission by:** - [X] Human - [ ] AI - [ ] Not reviewed # * GitHub Issue: #51308 Authored-by: Lorenz Hübschle <lorenz@firebolt.io> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent ab7a6af commit bb1aba2

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎cpp/src/arrow/util/compression_test.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,11 @@ TEST(TestCodecMisc, SpecifyCodecOptionsGZip) {
479479
};
480480
const std::pair<arrow::util::GZipCodecOptions, bool> options[]{
481481
{make_option(5, GZipFormat::GZIP, 15), true},
482+
{make_option(0, GZipFormat::GZIP, 15), true},
482483
{make_option(9, GZipFormat::ZLIB, 12), true},
483484
{make_option(-1, GZipFormat::DEFLATE, 10), true},
484-
{make_option(10, GZipFormat::GZIP, 25), false},
485+
{make_option(10, GZipFormat::GZIP, 15), false},
486+
{make_option(5, GZipFormat::GZIP, 25), false},
485487
{make_option(-992, GZipFormat::GZIP, 15), false},
486488
};
487489
CheckSpecifyCodecOptions<arrow::util::GZipCodecOptions>(Compression::GZIP, options);

‎cpp/src/arrow/util/compression_zlib.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,11 @@ class GZipCodec : public Codec {
494494
return Status::Invalid("GZip window_bits should be between ", kGZipMinWindowBits,
495495
" and ", kGZipMaxWindowBits);
496496
}
497-
const Status init_compressor_status = InitCompressor();
498-
if (!init_compressor_status.ok()) {
499-
return init_compressor_status;
497+
if (compression_level_ < Z_DEFAULT_COMPRESSION ||
498+
compression_level_ > Z_BEST_COMPRESSION) {
499+
return Status::Invalid("Invalid GZip compression level: ", compression_level_);
500500
}
501-
return InitDecompressor();
501+
return Status::OK();
502502
}
503503

504504
Compression::type compression_type() const override { return Compression::GZIP; }

0 commit comments

Comments
 (0)