This is an automated email from the ASF dual-hosted git repository.

pitrou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new bb1aba270e0 GH-51308: [C++] Initialize gzip codec state lazily (#51483)
bb1aba270e0 is described below

commit bb1aba270e02837ad4ed5277428dfd6b24ce1798
Author: Lorenz Hübschle <[email protected]>
AuthorDate: Tue Oct 6 10:13:42 2026 +0200

    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  [...]
    
    ### 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 <[email protected]>
    Signed-off-by: Antoine Pitrou <[email protected]>
---
 cpp/src/arrow/util/compression_test.cc | 4 +++-
 cpp/src/arrow/util/compression_zlib.cc | 8 ++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/cpp/src/arrow/util/compression_test.cc 
b/cpp/src/arrow/util/compression_test.cc
index 5ba93cc291d..4166b1240f2 100644
--- a/cpp/src/arrow/util/compression_test.cc
+++ b/cpp/src/arrow/util/compression_test.cc
@@ -479,9 +479,11 @@ TEST(TestCodecMisc, SpecifyCodecOptionsGZip) {
   };
   const std::pair<arrow::util::GZipCodecOptions, bool> options[]{
       {make_option(5, GZipFormat::GZIP, 15), true},
+      {make_option(0, GZipFormat::GZIP, 15), true},
       {make_option(9, GZipFormat::ZLIB, 12), true},
       {make_option(-1, GZipFormat::DEFLATE, 10), true},
-      {make_option(10, GZipFormat::GZIP, 25), false},
+      {make_option(10, GZipFormat::GZIP, 15), false},
+      {make_option(5, GZipFormat::GZIP, 25), false},
       {make_option(-992, GZipFormat::GZIP, 15), false},
   };
   CheckSpecifyCodecOptions<arrow::util::GZipCodecOptions>(Compression::GZIP, 
options);
diff --git a/cpp/src/arrow/util/compression_zlib.cc 
b/cpp/src/arrow/util/compression_zlib.cc
index b06cf2d2243..96f2e0c0268 100644
--- a/cpp/src/arrow/util/compression_zlib.cc
+++ b/cpp/src/arrow/util/compression_zlib.cc
@@ -494,11 +494,11 @@ class GZipCodec : public Codec {
       return Status::Invalid("GZip window_bits should be between ", 
kGZipMinWindowBits,
                              " and ", kGZipMaxWindowBits);
     }
-    const Status init_compressor_status = InitCompressor();
-    if (!init_compressor_status.ok()) {
-      return init_compressor_status;
+    if (compression_level_ < Z_DEFAULT_COMPRESSION ||
+        compression_level_ > Z_BEST_COMPRESSION) {
+      return Status::Invalid("Invalid GZip compression level: ", 
compression_level_);
     }
-    return InitDecompressor();
+    return Status::OK();
   }
 
   Compression::type compression_type() const override { return 
Compression::GZIP; }

Reply via email to