Skip to content

perf: copy bitmaps a word at a time in pre_selection_scatter - #25494

Open
abokhalill wants to merge 1 commit into
apache:mainfrom
abokhalill:perf-pre-selection-scatter-bulk-copy
Open

abokhalill wants to merge 1 commit into
apache:mainfrom
abokhalill:perf-pre-selection-scatter-bulk-copy

Conversation

@abokhalill

@abokhalill abokhalill commented Sep 19, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

  • N/A

Rationale for this change

pre_selection_scatter puts the short-circuited RHS result back at full batch length. For each run of selected rows it slices the RHS and appends one Option<bool> at a time, even though both sides are bit-packed, and every slice clones and drops an Arc.

Since #25771, AND chains of cheap comparisons no longer reach this function, so TPC-H is unchanged. It still runs for OR, and for AND with a conjunct that is not cheap.

TPC-H SF30 lineitem, 24 threads, l_quantity > K or l_discount > 0.05, by share of rows that reach the RHS:

  • 4%: 86.0ms -> 82.0ms
  • 8%: 96.5ms -> 89.5ms
  • 12%: 107.5ms -> 96.0ms
  • 16%: 115.0ms -> 102.0ms
  • 20%: 102.0ms -> 92.0ms

l_quantity < K and l_comment like '%special%' is 1-2% faster. The 22 TPC-H queries are within noise.

What changes are included in this PR?

Borrow the RHS buffers once, copy each run with append_packed_range, and only build a validity bitmap when the RHS has nulls.

Are these changes tested?

Yes, by the existing tests, including test_pre_selection_scatter.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added the physical-expr Changes to the physical-expr crates label Sep 19, 2026
@github-actions github-actions Bot added the auto detected api change Auto detected API change label Oct 7, 2026
@kosiew

kosiew commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@abokhalill
Thanks for your contribution.
Can you resolve the merge conflicts?

@codecov-commenter

codecov-commenter commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.73%. Comparing base (f9b7f34) to head (7a60002).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25494      +/-   ##
==========================================
- Coverage   82.73%   82.73%   -0.01%     
==========================================
  Files        1147     1147              
  Lines      449459   449412      -47     
  Branches   449459   449412      -47     
==========================================
- Hits       371864   371812      -52     
- Misses      54936    54946      +10     
+ Partials    22659    22654       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.
@abokhalill
abokhalill force-pushed the perf-pre-selection-scatter-bulk-copy branch from b1a7913 to 7a60002 Compare October 7, 2026 17:57
@abokhalill

Copy link
Copy Markdown
Author

@kosiew done. the speedups are actually even more prominent now!

@github-actions github-actions Bot removed the auto detected api change Auto detected API change label Oct 9, 2026
@kosiew

kosiew commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

run benchmark binary_op

env:
  BENCH_FILTER: short_circuit

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highcpu-48 (12 vCPU / 65 GiB) | Linux bench-c6076368077-3230-btb2m 6.12.94+ #1 SMP Fri Aug 21 08:00:16 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  48
On-line CPU(s) list:                     0-47
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     48
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               3 MiB (48 instances)
L1i cache:                               3 MiB (48 instances)
L2 cache:                                96 MiB (48 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-47
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf-pre-selection-scatter-bulk-copy (7a60002) to f9b7f34 (merge-base) diff

Run configuration
run benchmark binary_op
env:
  BENCH_FILTER: "short_circuit"

Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highcpu-48 (12 vCPU / 65 GiB)

Comparing perf-pre-selection-scatter-bulk-copy (7a60002) to f9b7f34 (merge-base) diff

Run configuration
run benchmark binary_op
env:
  BENCH_FILTER: "short_circuit"
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  48
On-line CPU(s) list:                     0-47
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     48
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               3 MiB (48 instances)
L1i cache:                               3 MiB (48 instances)
L2 cache:                                96 MiB (48 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-47
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                      HEAD                                   perf-pre-selection-scatter-bulk-copy
-----                                      ----                                   ------------------------------------
short_circuit/and/all_false                1.04     66.4±1.61ns        ? ?/sec    1.00     63.8±0.68ns        ? ?/sec
short_circuit/and/all_true_in_and          1.00    693.7±2.92µs        ? ?/sec    1.00    691.5±2.74µs        ? ?/sec
short_circuit/and/one_true_first           1.00    298.1±0.91µs        ? ?/sec    1.00    299.4±1.27µs        ? ?/sec
short_circuit/and/one_true_last            1.00    269.9±1.46µs        ? ?/sec    1.00    269.6±1.26µs        ? ?/sec
short_circuit/and/one_true_middle          1.00    269.0±1.12µs        ? ?/sec    1.01    271.7±1.27µs        ? ?/sec
short_circuit/and/one_true_middle_left     1.00    265.8±0.66µs        ? ?/sec    1.00    264.9±1.07µs        ? ?/sec
short_circuit/and/one_true_middle_right    1.00    270.2±0.79µs        ? ?/sec    1.00    269.3±1.06µs        ? ?/sec
short_circuit/or/all_false_in_or           1.00    693.3±2.84µs        ? ?/sec    1.08   749.3±85.43µs        ? ?/sec
short_circuit/or/all_true                  1.06     74.5±3.31ns        ? ?/sec    1.00     70.5±0.35ns        ? ?/sec
short_circuit/or/one_false_first           1.00    270.8±0.83µs        ? ?/sec    1.00    269.8±0.72µs        ? ?/sec
short_circuit/or/one_false_last            1.00    297.4±0.69µs        ? ?/sec    1.00    297.4±0.77µs        ? ?/sec
short_circuit/or/one_false_middle          1.00    296.7±0.69µs        ? ?/sec    1.00    297.6±0.73µs        ? ?/sec
short_circuit/or/one_false_middle_left     1.00    293.5±0.69µs        ? ?/sec    1.00    292.1±0.73µs        ? ?/sec
short_circuit/or/one_false_middle_right    1.00    296.7±0.78µs        ? ?/sec    1.00    297.5±0.79µs        ? ?/sec

Resource Usage

binary_op — base (merge-base)

Metric Value
Wall time 310.1s
Peak memory 84.4 MiB
Avg memory 39.6 MiB
CPU user 187.4s
CPU sys 0.2s
Peak spill 0 B

binary_op — branch

Metric Value
Wall time 305.1s
Peak memory 84.1 MiB
Avg memory 39.9 MiB
CPU user 184.8s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abokhalill,

Thanks for working on this optimization. The packed bitmap copying addresses the unnecessary RHS slicing and per-row Boolean processing while preserving the existing scatter behavior. I found no blocking issues, just two minor suggestions to avoid an unnecessary allocation and improve regression coverage.

let right_bytes = right_values.values();
let right_nulls = right_result.nulls();

let mut values = BooleanBufferBuilder::new(result_len);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move result_array_builder initialization into the None branch? The new Some branch allocates its own bitmap builder and returns without using the original one, so this would avoid an unnecessary allocation and free.

.iter()
.for_each(|v| result_array_builder.append_option(v));
let from = right_offset + right_array_pos;
values.append_packed_range(from..from + len, right_bytes);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a scatter regression test covering nullable RHS values, non-byte-aligned offsets, runs crossing byte and 64-bit boundaries, and leading, intermediate, and trailing gaps with both fill values? Please include independently offset value and validity buffers, since ordinary slices usually share the same offset, and compare the output against a simple row-wise reference.

@alamb

alamb commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

run benchmark binary_op

env:
  BENCH_FILTER: short_circuit

@alamb

alamb commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Trying to see if performance changes are reproducable

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c6084992423-3245-29csd 6.12.94+ #1 SMP Fri Aug 21 08:00:16 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf-pre-selection-scatter-bulk-copy (7a60002) to f9b7f34 (merge-base) diff

Run configuration
run benchmark binary_op
env:
  BENCH_FILTER: "short_circuit"

Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

Comparing perf-pre-selection-scatter-bulk-copy (7a60002) to f9b7f34 (merge-base) diff

Run configuration
run benchmark binary_op
env:
  BENCH_FILTER: "short_circuit"
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                      HEAD                                   perf-pre-selection-scatter-bulk-copy
-----                                      ----                                   ------------------------------------
short_circuit/and/all_false                1.04     66.6±1.82ns        ? ?/sec    1.00     63.9±0.72ns        ? ?/sec
short_circuit/and/all_true_in_and          1.01    696.0±3.04µs        ? ?/sec    1.00    689.2±2.87µs        ? ?/sec
short_circuit/and/one_true_first           1.00    298.2±1.55µs        ? ?/sec    1.01    299.9±0.87µs        ? ?/sec
short_circuit/and/one_true_last            1.00    270.3±1.18µs        ? ?/sec    1.00    269.8±0.93µs        ? ?/sec
short_circuit/and/one_true_middle          1.00    270.5±0.95µs        ? ?/sec    1.00    269.7±0.80µs        ? ?/sec
short_circuit/and/one_true_middle_left     1.01    266.8±0.82µs        ? ?/sec    1.00    265.5±0.68µs        ? ?/sec
short_circuit/and/one_true_middle_right    1.01    272.3±1.12µs        ? ?/sec    1.00    269.9±0.98µs        ? ?/sec
short_circuit/or/all_false_in_or           1.00    693.3±3.13µs        ? ?/sec    1.00    691.6±3.55µs        ? ?/sec
short_circuit/or/all_true                  1.00     68.9±0.28ns        ? ?/sec    1.03     70.9±0.36ns        ? ?/sec
short_circuit/or/one_false_first           1.00    269.9±1.19µs        ? ?/sec    1.00    269.6±0.69µs        ? ?/sec
short_circuit/or/one_false_last            1.00    297.0±1.30µs        ? ?/sec    1.00    296.9±0.66µs        ? ?/sec
short_circuit/or/one_false_middle          1.00    296.1±0.80µs        ? ?/sec    1.01    298.0±1.44µs        ? ?/sec
short_circuit/or/one_false_middle_left     1.00    293.6±0.72µs        ? ?/sec    1.00    293.9±1.63µs        ? ?/sec
short_circuit/or/one_false_middle_right    1.01    299.5±0.74µs        ? ?/sec    1.00    296.2±0.59µs        ? ?/sec

Resource Usage

binary_op — base (merge-base)

Metric Value
Wall time 220.1s
Peak memory 83.5 MiB
Avg memory 54.9 MiB
CPU user 185.5s
CPU sys 0.1s
Peak spill 0 B

binary_op — branch

Metric Value
Wall time 215.0s
Peak memory 84.7 MiB
Avg memory 56.8 MiB
CPU user 184.1s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants