Skip to content

Commit 7a60002

Browse files
committed
perf: copy bitmaps a word at a time in pre_selection_scatter
Slicing the RHS array per selected run clones and drops an Arc on every iteration, and iterating the slice sets one bit per row while both sides are already bit-packed. Borrow the RHS buffers once and copy each run with append_packed_range. Only build a validity bitmap when the RHS has nulls, so a null-free result does not pay a second pass per run. TPC-H SF30 on 24 threads: q6 -8.3%, q12 -4.1%, q19 -2.0%, others within noise.
1 parent f9b7f34 commit 7a60002

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

  • datafusion/physical-expr/src/expressions

‎datafusion/physical-expr/src/expressions/binary.rs‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::hash::Hash;
2727
use std::sync::Arc;
2828

2929
use arrow::array::*;
30+
use arrow::buffer::NullBuffer;
3031
use arrow::compute::kernels::boolean::{and_kleene, or_kleene};
3132
use arrow::compute::kernels::concat_elements::concat_elements_dyn;
3233
use arrow::compute::{SlicesIterator, cast, filter, filter_record_batch};
@@ -1360,21 +1361,56 @@ fn pre_selection_scatter(
13601361
let mut last_end = 0;
13611362
match right_result {
13621363
Some(right_result) => {
1364+
// Borrow the RHS buffers once. `BooleanArray::slice` clones and
1365+
// drops an `Arc` for every run, which dominates this loop when the
1366+
// mask selects many short runs.
1367+
let right_values = right_result.values();
1368+
let right_offset = right_values.offset();
1369+
let right_bytes = right_values.values();
1370+
let right_nulls = right_result.nulls();
1371+
1372+
let mut values = BooleanBufferBuilder::new(result_len);
1373+
// Only build a validity bitmap when the RHS has nulls. Otherwise
1374+
// every run pays a second pass to write an all-ones mask that is
1375+
// then discarded.
1376+
let mut validity = right_nulls.map(|_| BooleanBufferBuilder::new(result_len));
1377+
13631378
SlicesIterator::new(mask).for_each(|(start, end)| {
13641379
if start > last_end {
1365-
result_array_builder.append_n(start - last_end, fill_value);
1380+
let gap = start - last_end;
1381+
values.append_n(gap, fill_value);
1382+
if let Some(v) = validity.as_mut() {
1383+
v.append_n(gap, true);
1384+
}
13661385
}
13671386

1368-
// Copy the selected RHS slice.
1387+
// Both sides are bit-packed, so copy the run a word at a
1388+
// time. Iterating the RHS would yield one `Option<bool>` and
1389+
// set one bit per row.
13691390
let len = end - start;
1370-
right_result
1371-
.slice(right_array_pos, len)
1372-
.iter()
1373-
.for_each(|v| result_array_builder.append_option(v));
1391+
let from = right_offset + right_array_pos;
1392+
values.append_packed_range(from..from + len, right_bytes);
1393+
if let (Some(v), Some(nulls)) = (validity.as_mut(), right_nulls) {
1394+
let nb = nulls.inner();
1395+
let nfrom = nb.offset() + right_array_pos;
1396+
v.append_packed_range(nfrom..nfrom + len, nb.values());
1397+
}
13741398

13751399
right_array_pos += len;
13761400
last_end = end;
13771401
});
1402+
1403+
if last_end < result_len {
1404+
let gap = result_len - last_end;
1405+
values.append_n(gap, fill_value);
1406+
if let Some(v) = validity.as_mut() {
1407+
v.append_n(gap, true);
1408+
}
1409+
}
1410+
1411+
let nulls = validity.map(|mut v| NullBuffer::new(v.finish()));
1412+
let boolean_result = BooleanArray::new(values.finish(), nulls);
1413+
return Ok(ColumnarValue::Array(Arc::new(boolean_result)));
13781414
}
13791415
None => SlicesIterator::new(mask).for_each(|(start, end)| {
13801416
if start > last_end {

0 commit comments

Comments
 (0)