Skip to content

Commit d21b24e

Browse files
committed
perf: release the shuffle decoder at EOF and fold it into the scan input state
ShuffleScanExec now keeps the input batch and its decoder in one ScanInputState behind a single mutex, still shared by plan and stream clones, and releases the decoder's retained zstd workspace when the input reaches EOF instead of holding it until the plan drops. The ipc_decode bench adds a 400-row case, zstd levels 1 and 3, an LZ4 control, a per-reader-lifetime variant that counts context startup and cleanup, and a mutex-locked variant mirroring the operator's lock. The decode-context reuse test now decodes its reference with a new ShuffleDecodeContext per frame; read_ipc_compressed uses the thread-local cached context, so the old reference was itself a reused context.
1 parent 3fb42e1 commit d21b24e

4 files changed

Lines changed: 141 additions & 43 deletions

File tree

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

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ pub struct ShuffleScanExec {
6262
pub data_types: Vec<DataType>,
6363
/// Schema of the shuffle output.
6464
pub schema: SchemaRef,
65-
/// The current input batch, populated by get_next_batch() before poll_next().
66-
pub batch: Arc<Mutex<Option<InputBatch>>>,
67-
/// Decode state (zstd workspace) reused across this operator's shuffle blocks. Owning it
68-
/// here scopes any retained workspace to the operator: it is freed when the plan's
69-
/// execution context drops, instead of persisting for the life of a decoding thread.
70-
decode_context: Arc<Mutex<ShuffleDecodeContext>>,
65+
/// Input state shared with the streams cloned from this plan node.
66+
input: Arc<Mutex<ScanInputState>>,
7167
/// Cache of plan properties.
7268
cache: Arc<PlanProperties>,
7369
/// Metrics collector.
@@ -113,8 +109,7 @@ impl ShuffleScanExec {
113109
exec_context_id,
114110
input_source,
115111
data_types,
116-
batch: Arc::new(Mutex::new(None)),
117-
decode_context: Arc::new(Mutex::new(ShuffleDecodeContext::default())),
112+
input: Arc::new(Mutex::new(ScanInputState::default())),
118113
cache,
119114
metrics: metrics_set,
120115
baseline_metrics,
@@ -126,7 +121,7 @@ impl ShuffleScanExec {
126121

127122
/// Feeds input batch into this scan. Only used in unit tests.
128123
pub fn set_input_batch(&mut self, input: InputBatch) {
129-
*self.batch.try_lock().unwrap() = Some(input);
124+
self.input.try_lock().unwrap().receive(input);
130125
}
131126

132127
/// Pull next input batch from JVM. Called externally before poll_next()
@@ -138,17 +133,17 @@ impl ShuffleScanExec {
138133
}
139134
let mut timer = self.baseline_metrics.elapsed_compute().timer();
140135

141-
let mut current_batch = self.batch.try_lock().unwrap();
142-
if current_batch.is_none() {
136+
let mut input = self.input.try_lock().unwrap();
137+
if input.batch.is_none() {
143138
let next_batch = Self::get_next(
144139
self.exec_context_id,
145140
self.input_source.as_ref().unwrap().as_obj(),
146141
&self.data_types,
147-
&mut self.decode_context.try_lock().unwrap(),
142+
&mut input.decoder,
148143
&self.decode_time,
149144
self.requires_validation,
150145
)?;
151-
*current_batch = Some(next_batch);
146+
input.receive(next_batch);
152147
}
153148

154149
timer.stop();
@@ -238,6 +233,29 @@ impl ShuffleScanExec {
238233
}
239234
}
240235

236+
/// The batch that get_next_batch() hands to poll_next(), together with the decoder that
237+
/// produced it. Plan and stream clones share one instance behind a mutex.
238+
#[derive(Debug, Default)]
239+
struct ScanInputState {
240+
/// The current input batch, populated by get_next_batch() before poll_next().
241+
batch: Option<InputBatch>,
242+
/// Decode state (zstd workspace) reused across this operator's shuffle blocks. Owning it
243+
/// here scopes any retained workspace to the operator: it is released at EOF, or when the
244+
/// plan's execution context drops, instead of persisting for the life of a decoding thread.
245+
decoder: ShuffleDecodeContext,
246+
}
247+
248+
impl ScanInputState {
249+
/// Stores the next input for poll_next(). EOF is final for this input, so the decoder's
250+
/// retained workspace is released here instead of living until the plan drops.
251+
fn receive(&mut self, batch: InputBatch) {
252+
if matches!(batch, InputBatch::EOF) {
253+
self.decoder = ShuffleDecodeContext::default();
254+
}
255+
self.batch = Some(batch);
256+
}
257+
}
258+
241259
fn decode_shuffle_batch(
242260
decode_context: &mut ShuffleDecodeContext,
243261
bytes: &[u8],
@@ -370,12 +388,9 @@ impl Stream for ShuffleScanStream {
370388

371389
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
372390
let mut timer = self.baseline_metrics.elapsed_compute().timer();
373-
let mut scan_batch = self.shuffle_scan.batch.try_lock().unwrap();
391+
let mut input = self.shuffle_scan.input.try_lock().unwrap();
374392

375-
let input_batch = &*scan_batch;
376-
let input_batch = if let Some(batch) = input_batch {
377-
batch
378-
} else {
393+
let Some(input_batch) = &input.batch else {
379394
timer.stop();
380395
return Poll::Pending;
381396
};
@@ -398,7 +413,7 @@ impl Stream for ShuffleScanStream {
398413
}
399414
};
400415

401-
*scan_batch = None;
416+
input.batch = None;
402417

403418
timer.stop();
404419

@@ -428,7 +443,11 @@ mod tests {
428443
use crate::execution::shuffle::ipc::read_ipc_compressed;
429444

430445
fn uncompressed_shuffle_payload(batch: &RecordBatch) -> Vec<u8> {
431-
let writer = ShuffleBlockWriter::try_new(&batch.schema(), CompressionCodec::None).unwrap();
446+
shuffle_payload(batch, CompressionCodec::None)
447+
}
448+
449+
fn shuffle_payload(batch: &RecordBatch, codec: CompressionCodec) -> Vec<u8> {
450+
let writer = ShuffleBlockWriter::try_new(&batch.schema(), codec).unwrap();
432451
let mut output = Cursor::new(Vec::new());
433452
writer
434453
.write_batch(
@@ -503,6 +522,35 @@ mod tests {
503522
);
504523
}
505524

525+
/// EOF is final for a shuffle input, so the decoder workspace that served its blocks must
526+
/// not stay retained for as long as the plan and its stream clones live.
527+
#[test]
528+
#[cfg_attr(miri, ignore)] // Miri cannot call FFI functions (zstd)
529+
fn eof_releases_retained_decoder_workspace() {
530+
use super::*;
531+
532+
let schema = Arc::new(Schema::new(vec![Field::new("id", DataType::Int32, false)]));
533+
let batch =
534+
RecordBatch::try_new(schema, vec![Arc::new(Int32Array::from(vec![1, 2, 3]))]).unwrap();
535+
let payload = shuffle_payload(&batch, CompressionCodec::Zstd(1));
536+
537+
let mut state = ScanInputState::default();
538+
let decoded =
539+
super::decode_shuffle_batch(&mut state.decoder, &payload, &[DataType::Int32], false)
540+
.unwrap();
541+
assert!(state.decoder.holds_zstd_dctx());
542+
543+
// A decoded block keeps the workspace for the next one.
544+
state.receive(InputBatch::new(decoded.columns().to_vec(), Some(3)));
545+
assert!(state.decoder.holds_zstd_dctx());
546+
assert!(matches!(state.batch, Some(InputBatch::Batch(_, 3))));
547+
548+
state.batch = None;
549+
state.receive(InputBatch::EOF);
550+
assert!(!state.decoder.holds_zstd_dctx());
551+
assert!(matches!(state.batch, Some(InputBatch::EOF)));
552+
}
553+
506554
#[test]
507555
fn remote_shuffle_preserves_rows_for_zero_column_batches() {
508556
let batch = RecordBatch::try_new_with_options(

‎native/shuffle/benches/ipc_decode.rs‎

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// under the License.
1717

1818
//! Measures shuffle frame decoding with a reused [`ShuffleDecodeContext`] against a fresh
19-
//! context per frame (the cost profile of creating decode state for every block).
19+
//! context per frame (the cost profile of creating decode state for every block) and a
20+
//! context created per reader lifetime, so its startup and cleanup are part of the measure.
2021
2122
use arrow::array::{Int64Array, RecordBatch, StringArray};
2223
use arrow::datatypes::{DataType, Field, Schema};
@@ -28,15 +29,21 @@ use datafusion_comet_shuffle::{
2829
};
2930
use std::hint::black_box;
3031
use std::io::{Cursor, Write};
31-
use std::sync::Arc;
32+
use std::sync::{Arc, Mutex};
3233

3334
const SMALL_ROWS: usize = 8192;
35+
/// Rows per frame for the shuffle blocks a narrow partition yields.
36+
const TINY_ROWS: usize = 400;
37+
/// Frames one reader decodes over its lifetime.
3438
const SMALL_FRAMES: usize = 64;
3539
const LARGE_ROWS: usize = SMALL_ROWS * SMALL_FRAMES;
3640
/// One wide-window frame is inserted after every this many small frames in the
3741
/// over-cap recovery scenario.
3842
const WIDE_FRAME_INTERVAL: usize = 16;
3943

44+
/// Encoded frames paired with the batches they decode to.
45+
type Frames = Vec<(Vec<u8>, RecordBatch)>;
46+
4047
fn test_schema() -> Arc<Schema> {
4148
Arc::new(Schema::new(vec![
4249
Field::new("id", DataType::Int64, false),
@@ -81,25 +88,44 @@ fn make_wide_window_frame(batch: &RecordBatch) -> Vec<u8> {
8188
bytes
8289
}
8390

84-
fn small_frames(codec: CompressionCodec) -> Vec<(Vec<u8>, RecordBatch)> {
91+
fn frames_of(rows: usize, codec: CompressionCodec) -> Frames {
8592
(0..SMALL_FRAMES)
8693
.map(|i| {
87-
let batch = make_batch(i * SMALL_ROWS, SMALL_ROWS);
94+
let batch = make_batch(i * rows, rows);
8895
(make_frame(&batch, codec.clone()), batch)
8996
})
9097
.collect()
9198
}
9299

93-
fn scenario_small_zstd() -> Vec<(Vec<u8>, RecordBatch)> {
94-
small_frames(CompressionCodec::Zstd(3))
100+
fn scenario_small_zstd() -> Frames {
101+
frames_of(SMALL_ROWS, CompressionCodec::Zstd(3))
102+
}
103+
104+
/// Small and tiny frames at the configurable zstd levels, with LZ4 and uncompressed
105+
/// frames as controls that never touch the zstd context.
106+
fn per_frame_scenarios() -> Vec<(String, Frames)> {
107+
let codecs = [
108+
("zstd1", CompressionCodec::Zstd(1)),
109+
("zstd3", CompressionCodec::Zstd(3)),
110+
("lz4", CompressionCodec::Lz4Frame),
111+
("none", CompressionCodec::None),
112+
];
113+
[TINY_ROWS, SMALL_ROWS]
114+
.into_iter()
115+
.flat_map(|rows| {
116+
codecs.iter().map(move |(name, codec)| {
117+
(format!("{name}_{rows}rows"), frames_of(rows, codec.clone()))
118+
})
119+
})
120+
.collect()
95121
}
96122

97-
fn scenario_large_frame() -> Vec<(Vec<u8>, RecordBatch)> {
123+
fn scenario_large_frame() -> Frames {
98124
let batch = make_batch(0, LARGE_ROWS);
99125
vec![(make_frame(&batch, CompressionCodec::Zstd(3)), batch)]
100126
}
101127

102-
fn scenario_over_cap_recovery() -> Vec<(Vec<u8>, RecordBatch)> {
128+
fn scenario_over_cap_recovery() -> Frames {
103129
let wide_batch = make_batch(0, SMALL_ROWS);
104130
let wide_frame = make_wide_window_frame(&wide_batch);
105131
let mut frames = Vec::new();
@@ -112,10 +138,6 @@ fn scenario_over_cap_recovery() -> Vec<(Vec<u8>, RecordBatch)> {
112138
frames
113139
}
114140

115-
fn scenario_none() -> Vec<(Vec<u8>, RecordBatch)> {
116-
small_frames(CompressionCodec::None)
117-
}
118-
119141
/// Both decode variants must produce the exact batches the frames were built from.
120142
fn assert_variants_decode_identically(frames: &[(Vec<u8>, RecordBatch)]) {
121143
let mut reused = ShuffleDecodeContext::default();
@@ -133,17 +155,18 @@ fn assert_variants_decode_identically(frames: &[(Vec<u8>, RecordBatch)]) {
133155
}
134156

135157
fn criterion_benchmark(c: &mut Criterion) {
136-
let scenarios = [
137-
("small_frames_zstd3", scenario_small_zstd()),
138-
("large_frame_zstd3", scenario_large_frame()),
139-
("over_cap_recovery_zstd", scenario_over_cap_recovery()),
140-
("small_frames_none", scenario_none()),
141-
];
158+
let mut scenarios = per_frame_scenarios();
159+
scenarios.push(("large_frame_zstd3".to_owned(), scenario_large_frame()));
160+
scenarios.push((
161+
"over_cap_recovery_zstd".to_owned(),
162+
scenario_over_cap_recovery(),
163+
));
142164

143165
let mut group = c.benchmark_group("ipc_decode");
144166
for (name, frames) in &scenarios {
145167
assert_variants_decode_identically(frames);
146168

169+
// The context outlives every iteration: steady-state reuse with no setup on the path.
147170
group.bench_function(format!("{name}/reused"), |b| {
148171
let mut context = ShuffleDecodeContext::default();
149172
b.iter(|| {
@@ -152,6 +175,27 @@ fn criterion_benchmark(c: &mut Criterion) {
152175
}
153176
});
154177
});
178+
// The shuffle scan operator keeps its decoder behind a mutex shared by plan and
179+
// stream clones and locks it once per block; this mirrors that lock on the decode
180+
// path without the operator's JNI-driven block fetch.
181+
group.bench_function(format!("{name}/reused_locked"), |b| {
182+
let context = Arc::new(Mutex::new(ShuffleDecodeContext::default()));
183+
b.iter(|| {
184+
for (frame, _) in frames {
185+
let mut context = context.try_lock().unwrap();
186+
black_box(read_ipc_compressed_with(&mut context, frame).unwrap());
187+
}
188+
});
189+
});
190+
// One context per reader lifetime: created before its frames, dropped after them.
191+
group.bench_function(format!("{name}/per_reader"), |b| {
192+
b.iter(|| {
193+
let mut context = ShuffleDecodeContext::default();
194+
for (frame, _) in frames {
195+
black_box(read_ipc_compressed_with(&mut context, frame).unwrap());
196+
}
197+
});
198+
});
155199
group.bench_function(format!("{name}/fresh"), |b| {
156200
b.iter(|| {
157201
for (frame, _) in frames {

‎native/shuffle/src/codec_context.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ impl ShuffleDecodeContext {
179179
}
180180
}
181181

182-
/// Test hook for the retained-workspace bound on the decode path.
183-
#[cfg(test)]
184-
pub(crate) fn holds_zstd_dctx(&self) -> bool {
182+
/// Whether a zstd workspace is currently cached, so owners can check their retention
183+
/// contract.
184+
pub fn holds_zstd_dctx(&self) -> bool {
185185
self.zstd.is_some()
186186
}
187187

‎native/shuffle/src/ipc.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,22 +320,28 @@ mod tests {
320320
}
321321

322322
/// One context across many frames, codecs changing between them, must decode exactly
323-
/// like fresh per-frame decoders.
323+
/// like a context created for each frame.
324324
#[test]
325325
#[cfg_attr(miri, ignore)] // Miri cannot call Zstd's C FFI.
326326
fn decode_context_reused_across_frames_and_codecs() {
327327
let mut ctx = ShuffleDecodeContext::default();
328328
for _ in 0..3 {
329329
for codec in [b"ZSTD", b"NONE", b"SNAP", b"ZSTD", b"LZ4_", b"ZSTD"] {
330330
let frame = encode(codec, &ipc_stream(1));
331-
let fresh = read_ipc_compressed(&frame).unwrap();
331+
let fresh =
332+
read_ipc_compressed_with(&mut ShuffleDecodeContext::default(), &frame).unwrap();
332333
let reused = read_ipc_compressed_with(&mut ctx, &frame).unwrap();
333334
let reused_validated =
334335
read_ipc_compressed_validated_with(&mut ctx, &frame).unwrap();
335336
assert_eq!(reused, fresh);
336337
assert_eq!(reused_validated, fresh);
337338
}
338339
}
340+
assert_eq!(
341+
ctx.creation_count(),
342+
1,
343+
"every zstd frame must share one context"
344+
);
339345
}
340346

341347
/// ZSTD shuffle frame written by a streaming encode with no pledged source size, so its

0 commit comments

Comments
 (0)