Skip to content

Commit 65f6a1d

Browse files
committed
Simplify prepared build admission and validate reuse
1 parent ff8fe3a commit 65f6a1d

9 files changed

Lines changed: 744 additions & 196 deletions

File tree

‎datafusion/physical-plan/Cargo.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ harness = false
177177
name = "hash_join_semi_anti"
178178
required-features = ["test_utils"]
179179

180+
[[bench]]
181+
harness = false
182+
name = "prepared_hash_join"
183+
required-features = ["test_utils"]
184+
180185
[[bench]]
181186
harness = false
182187
name = "multi_group_by"
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
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+
//! Independent INNER joins over the same in-memory build snapshot.
19+
//!
20+
//! SQL planning does not expose prepared builds, so these benchmarks exercise
21+
//! the public execution API. Cases vary build size, integer/string keys, and
22+
//! concurrent consumers. Every consumer probes 4096 rows with 50% matches.
23+
//! `cold` includes preparation; `warm` retains a build across iterations.
24+
//! Input generation is excluded, but plan construction and draining outputs are
25+
//! timed. This does not measure Comet decoding, its cache, or its single-flight
26+
//! wait. Printed memory samples are DataFusion pool reservations, not RSS or
27+
//! total allocation. They do not separately charge the pre-created input
28+
//! fixture or output batches. Sparse integer keys avoid perfect-hash selection.
29+
30+
use std::hint::black_box;
31+
use std::sync::Arc;
32+
use std::time::Duration;
33+
34+
use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
35+
use arrow::datatypes::{DataType, Field, Schema};
36+
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
37+
use datafusion_common::{JoinType, Result};
38+
use datafusion_common_runtime::SpawnedTask;
39+
use datafusion_execution::TaskContext;
40+
use datafusion_execution::memory_pool::{
41+
MemoryPool, PeakRecordingPool, UnboundedMemoryPool,
42+
};
43+
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
44+
use datafusion_physical_expr::expressions::col;
45+
use datafusion_physical_plan::ExecutionPlan;
46+
use datafusion_physical_plan::joins::{
47+
HashJoinExec, HashJoinExecBuilder, PartitionMode, PreparedHashJoinBuild,
48+
};
49+
use datafusion_physical_plan::memory::MemoryStream;
50+
use datafusion_physical_plan::test::TestMemoryExec;
51+
use futures::TryStreamExt;
52+
use tokio::runtime::Builder;
53+
use tokio::sync::Barrier;
54+
55+
const PROBE_ROWS: usize = 4096;
56+
57+
struct Workload {
58+
build: Vec<RecordBatch>,
59+
probe: RecordBatch,
60+
expected_sum: i64,
61+
}
62+
63+
impl Workload {
64+
fn new(build_rows: usize, strings: bool) -> Self {
65+
let batch = |keys: Vec<usize>| {
66+
let array: ArrayRef = if strings {
67+
Arc::new(StringArray::from_iter_values(
68+
keys.iter().map(|key| format!("key-{key:08}")),
69+
))
70+
} else {
71+
Arc::new(Int64Array::from_iter_values(
72+
keys.iter().map(|key| (key * 17) as i64),
73+
))
74+
};
75+
RecordBatch::try_new(
76+
Arc::new(Schema::new(vec![
77+
Field::new("key", array.data_type().clone(), false),
78+
Field::new("payload", DataType::Int64, false),
79+
])),
80+
vec![
81+
array,
82+
Arc::new(Int64Array::from_iter_values(
83+
keys.iter().map(|key| *key as i64),
84+
)),
85+
],
86+
)
87+
.unwrap()
88+
};
89+
let build = batch((0..build_rows).collect());
90+
let probe = batch(
91+
(0..PROBE_ROWS)
92+
.map(|i| (i / 2) % build_rows + (i % 2) * build_rows)
93+
.collect(),
94+
);
95+
Self {
96+
build: (0..build_rows)
97+
.step_by(8192)
98+
.map(|offset| build.slice(offset, (build_rows - offset).min(8192)))
99+
.collect(),
100+
probe,
101+
expected_sum: (0..PROBE_ROWS / 2).map(|i| (i % build_rows) as i64).sum(),
102+
}
103+
}
104+
105+
fn join(&self) -> Result<HashJoinExec> {
106+
let schema = self.probe.schema();
107+
HashJoinExecBuilder::new(
108+
TestMemoryExec::try_new_exec(
109+
std::slice::from_ref(&self.build),
110+
Arc::clone(&schema),
111+
None,
112+
)?,
113+
TestMemoryExec::try_new_exec(
114+
&[vec![self.probe.clone()]],
115+
Arc::clone(&schema),
116+
None,
117+
)?,
118+
vec![(col("key", &schema)?, col("key", &schema)?)],
119+
JoinType::Inner,
120+
)
121+
.with_partition_mode(PartitionMode::CollectLeft)
122+
.build()
123+
}
124+
125+
async fn prepare(
126+
&self,
127+
join: &HashJoinExec,
128+
context: &TaskContext,
129+
) -> Result<Arc<PreparedHashJoinBuild>> {
130+
join.prepare_build(
131+
Box::pin(MemoryStream::try_new(
132+
self.build.clone(),
133+
self.probe.schema(),
134+
None,
135+
)?),
136+
Arc::clone(context.memory_pool()),
137+
Arc::clone(context.session_config().options()),
138+
)
139+
.await
140+
}
141+
}
142+
143+
#[derive(Clone, Copy)]
144+
enum Mode {
145+
Rebuild,
146+
Cold,
147+
Warm,
148+
}
149+
150+
impl Mode {
151+
fn name(self) -> &'static str {
152+
match self {
153+
Self::Rebuild => "rebuild",
154+
Self::Cold => "cold",
155+
Self::Warm => "warm",
156+
}
157+
}
158+
}
159+
160+
async fn run(
161+
workload: &Workload,
162+
consumers: usize,
163+
mode: Mode,
164+
warm: Option<Arc<PreparedHashJoinBuild>>,
165+
context: Arc<TaskContext>,
166+
) -> Result<(usize, i64)> {
167+
let joins = (0..consumers)
168+
.map(|_| workload.join())
169+
.collect::<Result<Vec<_>>>()?;
170+
let prepared = match mode {
171+
Mode::Rebuild => None,
172+
Mode::Cold => Some(workload.prepare(&joins[0], &context).await?),
173+
Mode::Warm => warm,
174+
};
175+
let start = Arc::new(Barrier::new(consumers));
176+
let tasks = joins
177+
.into_iter()
178+
.map(|join| {
179+
let join = match &prepared {
180+
Some(build) => join
181+
.builder()
182+
.with_prepared_build(Arc::clone(build))
183+
.build()?,
184+
None => join,
185+
};
186+
let context = Arc::clone(&context);
187+
let start = Arc::clone(&start);
188+
Ok(SpawnedTask::spawn(async move {
189+
start.wait().await;
190+
let mut stream = join.execute(0, context)?;
191+
let mut result = (0, 0);
192+
while let Some(batch) = stream.try_next().await? {
193+
result.0 += batch.num_rows();
194+
let payload = batch
195+
.column(1)
196+
.as_any()
197+
.downcast_ref::<Int64Array>()
198+
.unwrap();
199+
result.1 += payload.values().iter().sum::<i64>();
200+
}
201+
Ok::<_, datafusion_common::DataFusionError>(result)
202+
}))
203+
})
204+
.collect::<Result<Vec<_>>>()?;
205+
let mut result = (0, 0);
206+
for task in tasks {
207+
let (rows, sum) = task.await.unwrap()?;
208+
result.0 += rows;
209+
result.1 += sum;
210+
}
211+
Ok(result)
212+
}
213+
214+
fn benchmark(c: &mut Criterion) {
215+
let runtime = Builder::new_multi_thread()
216+
.worker_threads(4)
217+
.enable_all()
218+
.build()
219+
.unwrap();
220+
let mut group = c.benchmark_group("prepared_hash_join");
221+
group.sample_size(20);
222+
group.warm_up_time(Duration::from_millis(500));
223+
group.measurement_time(Duration::from_secs(2));
224+
225+
for (key_type, strings) in [("int64", false), ("utf8", true)] {
226+
for rows in [64, 65536] {
227+
let workload = Workload::new(rows, strings);
228+
for consumers in [1, 4] {
229+
for mode in [Mode::Rebuild, Mode::Cold, Mode::Warm] {
230+
let pool = Arc::new(PeakRecordingPool::new(Arc::new(
231+
UnboundedMemoryPool::default(),
232+
)));
233+
let context = Arc::new(
234+
TaskContext::default().with_runtime(
235+
RuntimeEnvBuilder::new()
236+
.with_memory_pool(Arc::clone(&pool) as _)
237+
.build_arc()
238+
.unwrap(),
239+
),
240+
);
241+
let warm = matches!(mode, Mode::Warm).then(|| {
242+
runtime
243+
.block_on(
244+
workload.prepare(&workload.join().unwrap(), &context),
245+
)
246+
.unwrap()
247+
});
248+
pool.reset_peak();
249+
let output = runtime
250+
.block_on(run(
251+
&workload,
252+
consumers,
253+
mode,
254+
warm.clone(),
255+
Arc::clone(&context),
256+
))
257+
.unwrap();
258+
assert_eq!(
259+
output,
260+
(
261+
consumers * PROBE_ROWS / 2,
262+
consumers as i64 * workload.expected_sum
263+
)
264+
);
265+
let case = format!("{key_type}/{rows}/{consumers}_consumers");
266+
eprintln!(
267+
"pool reservation {case}/{}: peak={} bytes, retained={} bytes (not RSS)",
268+
mode.name(),
269+
pool.peak_reserved(),
270+
pool.reserved()
271+
);
272+
drop(warm);
273+
assert_eq!(pool.reserved(), 0);
274+
// Keep recording overhead out of the timing measurement.
275+
let context = Arc::new(TaskContext::default());
276+
let warm = matches!(mode, Mode::Warm).then(|| {
277+
runtime
278+
.block_on(
279+
workload.prepare(&workload.join().unwrap(), &context),
280+
)
281+
.unwrap()
282+
});
283+
group.bench_function(BenchmarkId::new(mode.name(), case), |b| {
284+
b.iter(|| {
285+
black_box(
286+
runtime
287+
.block_on(run(
288+
&workload,
289+
consumers,
290+
mode,
291+
warm.clone(),
292+
Arc::clone(&context),
293+
))
294+
.unwrap(),
295+
)
296+
});
297+
});
298+
drop(warm);
299+
assert_eq!(context.memory_pool().reserved(), 0);
300+
}
301+
}
302+
}
303+
}
304+
group.finish();
305+
}
306+
307+
criterion_group!(benches, benchmark);
308+
criterion_main!(benches);

0 commit comments

Comments
 (0)