Skip to content

Commit 8da6e6b

Browse files
committed
perf: reuse zstd compression contexts across shuffle blocks
Every shuffle block previously created and destroyed its own zstd context: a fresh CCtx per encoded block and a fresh DCtx per decoded frame. Context setup is pure overhead that scales with block count, so high-partition shuffles with small blocks pay the most. Encode paths now share one context per task, threaded from the task-level owner so codec memory stays bounded regardless of partition count. The remote shuffle path still frees the zstd workspace with each admitted encode, keeping its memory accounting accurate. Decode reuses a per-thread context behind the existing entry points. Wire format is unchanged. On a 4M-row hash shuffle with 10,000 partitions at zstd level 3, encode time drops ~10% and wall time ~7%; larger-block shapes are within noise. lz4 and snappy keep per-block encoders: no reset API in the pinned crates and much smaller setup cost. Part of #5002.
1 parent 918bc7d commit 8da6e6b

13 files changed

Lines changed: 533 additions & 101 deletions

File tree

‎native/core/src/execution/operators/shuffle_scan.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,9 @@ impl RecordBatchStream for ShuffleScanStream {
398398

399399
#[cfg(test)]
400400
mod tests {
401-
use crate::execution::shuffle::{CompressionCodec, ShuffleBlockWriter};
401+
use crate::execution::shuffle::{CompressionCodec, ShuffleBlockWriter, ShuffleCodecContext};
402402
use arrow::array::{Int32Array, RecordBatchOptions, StringArray, UInt32Array};
403403
use arrow::datatypes::{DataType, Field, Schema};
404-
use arrow::ipc::writer::CompressionContext;
405404
use arrow::record_batch::RecordBatch;
406405
use datafusion::physical_plan::metrics::Time;
407406
use std::io::Cursor;
@@ -416,7 +415,7 @@ mod tests {
416415
.write_batch(
417416
batch,
418417
&mut output,
419-
&mut CompressionContext::default(),
418+
&mut ShuffleCodecContext::default(),
420419
&Time::new(),
421420
)
422421
.unwrap();
@@ -535,7 +534,7 @@ mod tests {
535534
.write_batch(
536535
&batch,
537536
&mut buf,
538-
&mut CompressionContext::default(),
537+
&mut ShuffleCodecContext::default(),
539538
&ipc_time,
540539
)
541540
.unwrap();
@@ -607,7 +606,7 @@ mod tests {
607606
.write_batch(
608607
&dict_batch,
609608
&mut buf,
610-
&mut CompressionContext::default(),
609+
&mut ShuffleCodecContext::default(),
611610
&ipc_time,
612611
)
613612
.unwrap();

‎native/shuffle/benches/shuffle_writer.rs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use arrow::array::builder::{Date32Builder, Decimal128Builder, Int32Builder};
1919
use arrow::array::{builder::StringBuilder, Array, Int32Array, RecordBatch};
2020
use arrow::datatypes::{DataType, Field, Schema};
21-
use arrow::ipc::writer::CompressionContext;
2221
use arrow::row::{RowConverter, SortField};
2322
use criterion::{criterion_group, criterion_main, Criterion};
2423
use datafusion::datasource::memory::MemorySourceConfig;
@@ -31,7 +30,7 @@ use datafusion::{
3130
prelude::SessionContext,
3231
};
3332
use datafusion_comet_shuffle::{
34-
CometPartitioning, CompressionCodec, ShuffleBlockWriter, ShuffleWriterExec,
33+
CometPartitioning, CompressionCodec, ShuffleBlockWriter, ShuffleCodecContext, ShuffleWriterExec,
3534
};
3635
use itertools::Itertools;
3736
use std::io::Cursor;
@@ -54,11 +53,11 @@ fn criterion_benchmark(c: &mut Criterion) {
5453
let ipc_time = Time::default();
5554
let w =
5655
ShuffleBlockWriter::try_new(&batch.schema(), compression_codec.clone()).unwrap();
57-
let mut compression_context = CompressionContext::default();
56+
let mut codec_context = ShuffleCodecContext::default();
5857
b.iter(|| {
5958
buffer.clear();
6059
let mut cursor = Cursor::new(&mut buffer);
61-
w.write_batch(&batch, &mut cursor, &mut compression_context, &ipc_time)
60+
w.write_batch(&batch, &mut cursor, &mut codec_context, &ipc_time)
6261
.unwrap();
6362
});
6463
});
@@ -256,14 +255,14 @@ fn schema_encoding_benchmark(c: &mut Criterion) {
256255
let writer =
257256
ShuffleBlockWriter::try_new(batch.schema().as_ref(), CompressionCodec::None).unwrap();
258257
let ipc_time = Time::default();
259-
let mut compression_context = CompressionContext::default();
258+
let mut codec_context = ShuffleCodecContext::default();
260259
group.bench_function(format!("write_batch ({name} schema)"), |b| {
261260
let mut buffer = vec![];
262261
b.iter(|| {
263262
buffer.clear();
264263
let mut cursor = Cursor::new(&mut buffer);
265264
writer
266-
.write_batch(&batch, &mut cursor, &mut compression_context, &ipc_time)
265+
.write_batch(&batch, &mut cursor, &mut codec_context, &ipc_time)
267266
.unwrap();
268267
});
269268
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use arrow::ipc::writer::CompressionContext;
19+
use std::io;
20+
use zstd::zstd_safe::{CCtx, CParameter, DCtx, ResetDirective};
21+
22+
/// Reusable compression state for encoding shuffle blocks.
23+
///
24+
/// A zstd context costs about a megabyte and real setup time, so a task shares one across all
25+
/// the blocks it encodes instead of paying per block. Keep ownership task-scoped, never
26+
/// per-output-partition -- a shuffle can have thousands of partitions. Local shuffle holds the
27+
/// zstd context for the whole task; the remote (RSS) path frees it after each admitted encode
28+
/// via [`Self::release_zstd`], since its memory accounting only reserves the workspace per
29+
/// invocation.
30+
#[derive(Default)]
31+
pub struct ShuffleCodecContext {
32+
/// Arrow's per-message IPC compression scratch, reused across encodes.
33+
pub(crate) arrow_ipc: CompressionContext,
34+
/// Lazily created, reused across blocks.
35+
zstd: Option<CCtx<'static>>,
36+
}
37+
38+
impl ShuffleCodecContext {
39+
/// The shared zstd context primed for one frame at `level`, plus the Arrow IPC scratch
40+
/// (returned together because the encoder borrows the context for the whole frame).
41+
///
42+
/// The session reset and level re-apply happen on every call: writers with different
43+
/// levels can share one context, and a failed encode must not leave state behind.
44+
pub(crate) fn zstd_cctx(
45+
&mut self,
46+
level: i32,
47+
) -> io::Result<(&mut CCtx<'static>, &mut CompressionContext)> {
48+
let cctx =
49+
match &mut self.zstd {
50+
Some(cctx) => cctx,
51+
none => none.insert(CCtx::try_create().ok_or_else(|| {
52+
io::Error::other("failed to allocate zstd compression context")
53+
})?),
54+
};
55+
cctx.reset(ResetDirective::SessionOnly)
56+
.map_err(map_zstd_error)?;
57+
cctx.set_parameter(CParameter::CompressionLevel(level))
58+
.map_err(map_zstd_error)?;
59+
Ok((cctx, &mut self.arrow_ipc))
60+
}
61+
62+
/// Drops the cached zstd context, freeing its native workspace. The remote encode path
63+
/// calls this after every admitted encode so the memory lives and dies inside that
64+
/// invocation's reservation; the next zstd encode re-creates it lazily.
65+
pub(crate) fn release_zstd(&mut self) {
66+
self.zstd = None;
67+
}
68+
69+
/// Test hook for the release-vs-retain contract of the two encode paths.
70+
#[cfg(test)]
71+
pub(crate) fn holds_zstd_cctx(&self) -> bool {
72+
self.zstd.is_some()
73+
}
74+
}
75+
76+
/// Decode-side counterpart of [`ShuffleCodecContext`]: one context serves every frame a
77+
/// reader decodes instead of allocating a fresh zstd context per frame.
78+
#[derive(Default)]
79+
pub struct ShuffleDecodeContext {
80+
/// Lazily created, reused across frames.
81+
zstd: Option<DCtx<'static>>,
82+
}
83+
84+
impl ShuffleDecodeContext {
85+
/// The shared zstd decompression context primed for one frame. The session reset clears
86+
/// anything a failed decode (e.g. a truncated fetch) left mid-frame.
87+
pub(crate) fn zstd_dctx(&mut self) -> io::Result<&mut DCtx<'static>> {
88+
let dctx = match &mut self.zstd {
89+
Some(dctx) => dctx,
90+
none => none.insert(DCtx::try_create().ok_or_else(|| {
91+
io::Error::other("failed to allocate zstd decompression context")
92+
})?),
93+
};
94+
dctx.reset(ResetDirective::SessionOnly)
95+
.map_err(map_zstd_error)?;
96+
Ok(dctx)
97+
}
98+
}
99+
100+
fn map_zstd_error(code: usize) -> io::Error {
101+
io::Error::other(zstd::zstd_safe::get_error_name(code))
102+
}

‎native/shuffle/src/ipc.rs‎

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,52 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use crate::codec_context::ShuffleDecodeContext;
1819
use arrow::array::RecordBatch;
1920
use arrow::ipc::reader::StreamReader;
2021
use datafusion::common::DataFusionError;
2122
use datafusion::error::Result;
23+
use std::cell::RefCell;
2224
use std::io::{Error, ErrorKind, Read};
2325

26+
thread_local! {
27+
/// Backs the entry points below. They're called from many JVM task threads; a
28+
/// thread-local gets each thread context reuse without changing any caller.
29+
static DECODE_CONTEXT: RefCell<ShuffleDecodeContext> =
30+
RefCell::new(ShuffleDecodeContext::default());
31+
}
32+
2433
/// Decode trusted local Comet output without revalidating every Arrow array value or offset.
2534
pub fn read_ipc_compressed(bytes: &[u8]) -> Result<RecordBatch> {
26-
read_ipc_compressed_impl(bytes, false)
35+
DECODE_CONTEXT.with(|context| read_ipc_compressed_impl(&mut context.borrow_mut(), bytes, false))
2736
}
2837

2938
/// Decode remotely fetched Comet output, including Arrow buffer and offset validation.
3039
pub fn read_ipc_compressed_validated(bytes: &[u8]) -> Result<RecordBatch> {
31-
read_ipc_compressed_impl(bytes, true)
40+
DECODE_CONTEXT.with(|context| read_ipc_compressed_impl(&mut context.borrow_mut(), bytes, true))
41+
}
42+
43+
/// [`read_ipc_compressed`] with a caller-owned decode context.
44+
pub fn read_ipc_compressed_with(
45+
decode_context: &mut ShuffleDecodeContext,
46+
bytes: &[u8],
47+
) -> Result<RecordBatch> {
48+
read_ipc_compressed_impl(decode_context, bytes, false)
49+
}
50+
51+
/// [`read_ipc_compressed_validated`] with a caller-owned decode context.
52+
pub fn read_ipc_compressed_validated_with(
53+
decode_context: &mut ShuffleDecodeContext,
54+
bytes: &[u8],
55+
) -> Result<RecordBatch> {
56+
read_ipc_compressed_impl(decode_context, bytes, true)
3257
}
3358

34-
fn read_ipc_compressed_impl(bytes: &[u8], validate: bool) -> Result<RecordBatch> {
59+
fn read_ipc_compressed_impl(
60+
decode_context: &mut ShuffleDecodeContext,
61+
bytes: &[u8],
62+
validate: bool,
63+
) -> Result<RecordBatch> {
3564
let codec = bytes.get(..4).ok_or_else(|| {
3665
DataFusionError::Execution("Failed to decode batch: truncated compression codec".to_owned())
3766
})?;
@@ -44,7 +73,10 @@ fn read_ipc_compressed_impl(bytes: &[u8], validate: bool) -> Result<RecordBatch>
4473
)?,
4574
// The slice already implements BufRead. Adding another BufReader would let read-ahead
4675
// conceal compressed bytes left over after the decoder reaches its end marker.
47-
b"ZSTD" => read_single_batch(zstd::Decoder::with_buffer(&mut encoded)?, validate)?,
76+
b"ZSTD" => read_single_batch(
77+
zstd::Decoder::with_context(&mut encoded, decode_context.zstd_dctx()?),
78+
validate,
79+
)?,
4880
b"NONE" => read_single_batch(&mut encoded, validate)?,
4981
other => {
5082
return Err(DataFusionError::Execution(format!(
@@ -114,7 +146,11 @@ fn read_single_batch<R: Read>(input: R, validate: bool) -> Result<RecordBatch> {
114146

115147
#[cfg(test)]
116148
mod tests {
117-
use super::{read_ipc_compressed, read_ipc_compressed_validated};
149+
use super::{
150+
read_ipc_compressed, read_ipc_compressed_validated, read_ipc_compressed_validated_with,
151+
read_ipc_compressed_with,
152+
};
153+
use crate::codec_context::ShuffleDecodeContext;
118154
use arrow::array::{Int32Array, RecordBatch, StringArray};
119155
use arrow::datatypes::{DataType, Field, Schema};
120156
use arrow::ipc::writer::StreamWriter;
@@ -267,4 +303,35 @@ mod tests {
267303
assert_eq!(batch, validated);
268304
}
269305
}
306+
307+
/// One context across many frames, codecs changing between them, must decode exactly
308+
/// like fresh per-frame decoders.
309+
#[test]
310+
#[cfg_attr(miri, ignore)] // Miri cannot call Zstd's C FFI.
311+
fn decode_context_reused_across_frames_and_codecs() {
312+
let mut ctx = ShuffleDecodeContext::default();
313+
for _ in 0..3 {
314+
for codec in [b"ZSTD", b"NONE", b"SNAP", b"ZSTD", b"LZ4_", b"ZSTD"] {
315+
let frame = encode(codec, &ipc_stream(1));
316+
let fresh = read_ipc_compressed(&frame).unwrap();
317+
let reused = read_ipc_compressed_with(&mut ctx, &frame).unwrap();
318+
let reused_validated =
319+
read_ipc_compressed_validated_with(&mut ctx, &frame).unwrap();
320+
assert_eq!(reused, fresh);
321+
assert_eq!(reused_validated, fresh);
322+
}
323+
}
324+
}
325+
326+
/// A truncated frame must not poison the context for the next valid one.
327+
#[test]
328+
#[cfg_attr(miri, ignore)] // Miri cannot call Zstd's C FFI.
329+
fn decode_context_usable_after_error() {
330+
let mut ctx = ShuffleDecodeContext::default();
331+
let good = encode(b"ZSTD", &ipc_stream(1));
332+
let truncated = &good[..good.len() - 7];
333+
assert!(read_ipc_compressed_with(&mut ctx, truncated).is_err());
334+
let batch = read_ipc_compressed_with(&mut ctx, &good).unwrap();
335+
assert_eq!(batch.num_rows(), 3);
336+
}
270337
}

‎native/shuffle/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
mod codec_context;
1819
pub(crate) mod comet_partitioning;
1920
pub mod ipc;
2021
pub(crate) mod metrics;
@@ -28,8 +29,12 @@ mod spark_crc32c_hasher;
2829
pub mod spark_unsafe;
2930
pub(crate) mod writers;
3031

32+
pub use codec_context::{ShuffleCodecContext, ShuffleDecodeContext};
3133
pub use comet_partitioning::CometPartitioning;
32-
pub use ipc::{read_ipc_compressed, read_ipc_compressed_validated};
34+
pub use ipc::{
35+
read_ipc_compressed, read_ipc_compressed_validated, read_ipc_compressed_validated_with,
36+
read_ipc_compressed_with,
37+
};
3338
pub use remote_schema::validate_remote_schema;
3439
pub use schema_align::SchemaAlignExec;
3540
pub use shuffle_writer::{ShuffleWriterDestination, ShuffleWriterExec};

0 commit comments

Comments
 (0)