ryanvickr commented on code in PR #12294:
URL: https://github.com/apache/arrow-rs/pull/12294#discussion_r4798535314


##########
parquet/src/compression.rs:
##########
@@ -518,31 +518,46 @@ pub use lz4_codec::*;
 mod zstd_codec {
     use crate::compression::{Codec, ZstdLevel};
     use crate::errors::Result;
+    use std::cell::RefCell;
     use std::io::Cursor;
 
+    thread_local! {
+        // One compressor per level and one decompressor per thread, created 
on first use.
+        //
+        // `create_codec` builds a codec per column chunk, so a context owned 
by the codec is
+        // created (and its workspace allocated and freed) once per column 
chunk read or written.
+        // A zstd context serves one call at a time and every bulk call resets 
its session, so a
+        // context reused across codecs on one thread produces the same bytes 
as a fresh one.
+        static COMPRESSORS: RefCell<Vec<(i32, 
zstd::bulk::Compressor<'static>)>> =

Review Comment:
   Storing a separate compressor per compression level in an unbounded `Vec` 
has two downsides:
   
   - A thread that touches many different levels will keep accumulating 
contexts without bound, and `0` and `3` end up allocating two separate contexts 
even though they map to the same default level.
   - More importantly, libzstd normally shrinks an oversized workspace back 
down after a run of smaller or lower-level calls. If a thread compresses once 
at a high level like 19 and then goes back to level 3, a separate per-level 
entry is never reused again and stays pinned at peak size until the thread 
exits.
   
   Reusing a single compressor per thread and updating its compression level 
when it changes avoids both issues without changing the output bytes.



##########
parquet/src/compression.rs:
##########
@@ -564,12 +579,22 @@ mod zstd_codec {
 
             let mut cursor = Cursor::new(output_buf);
             cursor.set_position(offset as u64);
-            let len = self
-                .decompressor
-                .get_or_insert_with(|| {
-                    zstd::bulk::Decompressor::new().expect("can create zstd 
decompressor")
-                })
-                .decompress_to_buffer(input_buf, &mut cursor)?;
+            let len = DECOMPRESSOR.with(|cell| match cell.try_borrow_mut() {

Review Comment:
   Since this already falls back to a fresh context on re-entrant borrows to 
avoid panicking, it's worth using `try_with` here (and for the compressor 
below) instead of `with`. If a reader or writer gets flushed or dropped from 
another thread-local destructor during thread exit, `with` will panic on 
access-after-destruction, whereas `try_with` can cleanly fall back to a fresh 
context.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to