Skip to content

Replace OffsetBufferBuilder with Vec in scalar byte-array zip - #10913

Merged
Jefffrey merged 2 commits into
apache:mainfrom
yashkuceriya:optimize-zip-offsets
Sep 17, 2026
Merged

Jefffrey merged 2 commits into
apache:mainfrom
yashkuceriya:optimize-zip-offsets

Conversation

@yashkuceriya

@yashkuceriya yashkuceriya commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Part of #10245.

Rationale for this change

The byte-array scalar zip path is one of the remaining OffsetBufferBuilder callsites in #10245. Building cumulative offsets directly in a Vec avoids checking each offset through the builder, which benefits masks containing long runs of the same value.

What changes are included in this PR?

BytesScalarImpl::create_output_on_non_nulls builds offsets with Vec<T::Offset>. The mixed-mask path checks the total byte size and offset range before allocating the output. An overflowing size returns MemoryError; a size outside the offset type returns OffsetOverflowError. Every generated offset is bounded by that checked total.

The existing all-true, all-false, and nullable-scalar fast paths are unchanged.

Are these changes tested?

  • cargo test -p arrow-select --quiet: 418 unit tests and 17 documentation tests passed.
  • cargo clippy -p arrow-select --all-targets --all-features -- -D warnings: passed.
  • cargo +stable fmt --all -- --check: passed.

The new regression test uses a 64 KiB binary scalar and a mixed mask whose output would require 2 GiB. It checks that the call returns OffsetOverflowError before allocating that output, with the large scalar on either side of the mask. Existing tests cover normal and large byte arrays, nulls, fragmented masks, and one-sided scalar paths.

Benchmarks

The project runner measured the previous revision, 4829c8f6d, here. Those results do not include the review fix.

Local measurements using the existing arrow/benches/zip_kernels.rs benchmark, on macOS arm64 with Rust 1.97.1:

Case, 8,192 rows Upstream cbbb56bba Previous PR 4829c8f6d Revised 1ec832648
Short strings, 50% true 40.00 us 38.78 us 38.40 us
Short strings, true then false 7.04 us 2.24 us 2.24 us
Long strings, 50% true 65.12 us 64.14 us 63.88 us
Long strings, true then false 41.90 us 35.69 us 36.84 us
Short bytes, 50% true 36.66 us 34.84 us 32.96 us
Short bytes, true then false 7.01 us 2.08 us 1.99 us
Long bytes, 50% true 54.93 us 52.28 us 53.47 us
Long bytes, true then false 32.06 us 25.53 us 26.71 us

These are short, sequential local runs, not a controlled performance study. The revision remains faster than the upstream baseline in these cases, but some long-value cases are slower than the previous PR revision. A project-runner rerun would help confirm the impact of the added checks.

Command, run for each revision with separate Criterion baselines:

cargo bench -p arrow --features test_utils --bench zip_kernels -- \
  'zip_8192_from_(long|short).+non_nulls_scalars/(50pct_true|true_then_false)$' \
  --sample-size 20 --warm-up-time 0.5 --measurement-time 1

Are there any user-facing changes?

Mixed-mask scalar byte-array output that exceeds the offset range now returns an error before output allocation instead of panicking. No public API changes.

Automated assistance

AI assistance was used to prepare the refactor, review fix, regression test, and validation. The commands and measurements above were run locally.

@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-select labels Aug 29, 2026
@Rich-T-kid

Copy link
Copy Markdown
Contributor

run benchmark zip_kernels

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5465529064-2039-b98lq 6.12.85+ #1 SMP Sat Jun 27 09:31:30 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 optimize-zip-offsets (4829c8f) to cbbb56b (merge-base) diff

Run configuration
run benchmark zip_kernels

BENCH_COMMAND=cargo bench --features=arrow,async,test_common,experimental,object_store --bench zip_kernels
Results will be posted here when complete


File an issue against this benchmark runner

@Rich-T-kid Rich-T-kid 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.

This PR looks good.
we should return errors instead of panicking on overflows. I outlined one place but the same idea applies to all call sites in the PR

Comment thread arrow-select/src/zip.rs Outdated
offset_buffer_builder.push_length(falsy_len)
}
let start_offset = current_offset;
let added = falsy_len.checked_mul(false_repeat_count).expect("overflow");

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.

we should avoid panicking here. return an error

@yashkuceriya yashkuceriya Sep 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1ec8326. The mixed-mask path now checks the total output size and offset range before allocating. The expect("overflow") calls introduced in this PR are gone; the run offsets are bounded by the validated total.

Added a regression test for an output exceeding i32::MAX, with the large scalar on either side. It returns OffsetOverflowError without allocating the 2 GiB output. All 418 unit tests, 17 doc tests, Clippy, and formatting pass locally.

The PR description now includes measurements from the existing zip benchmark. The revised code remains faster than the upstream baseline in the eight tested cases, but some long-value cases were slower than the previous revision locally. Could you rerun the project benchmark on this commit to confirm that tradeoff? GitHub also reports action_required for the new CI runs; could you approve those runs?

@adriangbot

This comment was marked as outdated.

@Jefffrey

Copy link
Copy Markdown
Contributor

zip generally takes too long, you'll need to run with specific bench filters @Rich-T-kid

@Rich-T-kid

Copy link
Copy Markdown
Contributor

run benchmark zip_kernels

env:
BENCH_FILTER: non_nulls_scalars

@adriangbot

This comment was marked as duplicate.

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

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

Comparing optimize-zip-offsets (4829c8f) to cbbb56b (merge-base) diff

Run configuration
run benchmark zip_kernels
env:
  BENCH_FILTER: "non_nulls_scalars"
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                                                                          main                                   optimize-zip-offsets
-----                                                                          ----                                   --------------------
zip_8192_from_i32/non_nulls_scalars/10pct_true                                 1.00      6.8±0.00µs        ? ?/sec    1.04      7.0±0.01µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/1pct_true                                  1.00      6.9±0.00µs        ? ?/sec    1.07      7.4±0.02µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/50pct_nulls                                1.00      7.6±0.02µs        ? ?/sec    1.03      7.9±0.01µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/50pct_true                                 1.64     14.8±0.27µs        ? ?/sec    1.00      9.0±0.02µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/90pct_true                                 1.00      6.6±0.03µs        ? ?/sec    1.33      8.9±0.00µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/99pct_true                                 1.00      6.5±0.00µs        ? ?/sec    1.35      8.8±0.01µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/all_false                                  1.00      6.7±0.00µs        ? ?/sec    1.07      7.1±0.01µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/all_true                                   1.00      6.2±0.36µs        ? ?/sec    1.36      8.4±0.01µs        ? ?/sec
zip_8192_from_i32/non_nulls_scalars/true_then_false                            1.00      6.4±0.00µs        ? ?/sec    1.22      7.9±0.01µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/10pct_true               1.17     50.4±0.16µs        ? ?/sec    1.00     43.2±0.22µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/1pct_true                1.47     38.8±0.04µs        ? ?/sec    1.00     26.3±0.15µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/50pct_nulls              1.09     58.9±0.17µs        ? ?/sec    1.00     54.0±0.26µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/50pct_true               1.03     66.5±0.16µs        ? ?/sec    1.00     64.7±0.22µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/90pct_true               1.13     60.7±0.12µs        ? ?/sec    1.00     53.9±0.25µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/99pct_true               1.37     49.9±0.06µs        ? ?/sec    1.00     36.4±0.09µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/all_false                1.00     23.9±0.03µs        ? ?/sec    1.00     24.0±0.05µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/all_true                 1.04     33.9±0.08µs        ? ?/sec    1.00     32.4±0.15µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/true_then_false          1.50     41.1±0.07µs        ? ?/sec    1.00     27.4±0.14µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/10pct_true             1.10     80.7±0.15µs        ? ?/sec    1.00     73.4±0.26µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/1pct_true              1.27     65.0±0.16µs        ? ?/sec    1.00     51.3±0.24µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/50pct_nulls            1.02     89.3±0.19µs        ? ?/sec    1.00     87.2±0.61µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/50pct_true             1.01     88.7±0.15µs        ? ?/sec    1.00     87.4±0.35µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/90pct_true             1.16     66.1±0.14µs        ? ?/sec    1.00     56.9±0.24µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/99pct_true             1.40     50.6±0.11µs        ? ?/sec    1.00     36.3±0.09µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/all_false              1.00     48.5±0.13µs        ? ?/sec    1.04     50.4±0.25µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/all_true               1.04     33.4±0.06µs        ? ?/sec    1.00     32.2±0.05µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/true_then_false        1.41     55.0±0.51µs        ? ?/sec    1.00     38.9±0.30µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/10pct_true                 1.58     24.7±0.07µs        ? ?/sec    1.00     15.6±0.03µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/1pct_true                  3.19     19.1±0.03µs        ? ?/sec    1.00      6.0±0.08µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/50pct_nulls                1.21     30.5±0.07µs        ? ?/sec    1.00     25.3±0.07µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/50pct_true                 1.10     36.3±0.10µs        ? ?/sec    1.00     33.1±0.15µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/90pct_true                 1.59     24.9±0.08µs        ? ?/sec    1.00     15.7±0.07µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/99pct_true                 3.75     19.8±0.02µs        ? ?/sec    1.00      5.3±0.14µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/all_false                  1.05      3.9±0.09µs        ? ?/sec    1.00      3.7±0.00µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/all_true                   1.00      3.9±0.00µs        ? ?/sec    1.00      3.9±0.01µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/true_then_false            4.29     17.6±0.01µs        ? ?/sec    1.00      4.1±0.14µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/10pct_true               1.52     25.5±0.09µs        ? ?/sec    1.00     16.8±0.05µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/1pct_true                3.03     19.5±0.02µs        ? ?/sec    1.00      6.4±0.11µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/50pct_nulls              1.13     29.5±0.07µs        ? ?/sec    1.00     26.1±0.12µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/50pct_true               1.02     35.4±0.11µs        ? ?/sec    1.00     34.6±0.12µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/90pct_true               1.53     24.9±0.08µs        ? ?/sec    1.00     16.3±0.11µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/99pct_true               3.68     19.7±0.02µs        ? ?/sec    1.00      5.4±0.15µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/all_false                1.00      3.8±0.00µs        ? ?/sec    1.00      3.8±0.01µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/all_true                 1.00      4.0±0.01µs        ? ?/sec    1.00      4.0±0.01µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/true_then_false          4.21     17.6±0.02µs        ? ?/sec    1.00      4.2±0.14µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/10pct_true         1.00     12.2±0.10µs        ? ?/sec    1.00     12.1±0.08µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/1pct_true          1.00      4.0±0.05µs        ? ?/sec    1.02      4.1±0.15µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/50pct_nulls        1.00     19.4±0.12µs        ? ?/sec    1.00     19.4±0.11µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/50pct_true         1.00     25.9±0.19µs        ? ?/sec    1.00     26.0±0.16µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/90pct_true         1.00     11.4±0.14µs        ? ?/sec    1.00     11.4±0.14µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/99pct_true         1.00      4.1±0.03µs        ? ?/sec    1.02      4.2±0.17µs        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/all_false          1.00   1796.4±5.35ns        ? ?/sec    1.00   1792.5±7.05ns        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/all_true           1.08      2.1±0.01µs        ? ?/sec    1.00  1915.5±11.85ns        ? ?/sec
zip_8192_from_string_views size (10..100)/non_nulls_scalars/true_then_false    1.00      2.2±0.00µs        ? ?/sec    1.04      2.2±0.14µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/10pct_true           1.00     12.1±0.10µs        ? ?/sec    1.00     12.1±0.09µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/1pct_true            1.00      4.0±0.04µs        ? ?/sec    1.01      4.0±0.16µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/50pct_nulls          1.00     19.2±0.13µs        ? ?/sec    1.00     19.3±0.10µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/50pct_true           1.00     25.8±0.16µs        ? ?/sec    1.02     26.1±0.16µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/90pct_true           1.00     11.4±0.14µs        ? ?/sec    1.00     11.3±0.14µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/99pct_true           1.00      4.0±0.03µs        ? ?/sec    1.01      4.1±0.16µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/all_false            1.00   1754.7±6.40ns        ? ?/sec    1.02   1781.6±6.10ns        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/all_true             1.00   1926.8±9.45ns        ? ?/sec    1.11      2.1±0.01µs        ? ?/sec
zip_8192_from_string_views size (3..10)/non_nulls_scalars/true_then_false      1.00      2.1±0.01µs        ? ?/sec    1.04      2.2±0.14µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 600.1s
Peak memory 16.1 MiB
Avg memory 14.5 MiB
CPU user 595.5s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 605.1s
Peak memory 31.4 MiB
Avg memory 19.5 MiB
CPU user 599.6s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

@Jefffrey

Copy link
Copy Markdown
Contributor

run benchmark zip_kernels
env:
BENCH_FILTER: zip_8192_from_(long|short).+non_nulls_scalars

@Jefffrey Jefffrey added the performance Speed or memory optimizations label Aug 30, 2026
@adriangbot

This comment was marked as duplicate.

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

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

Comparing optimize-zip-offsets (4829c8f) to cbbb56b (merge-base) diff

Run configuration
run benchmark zip_kernels
env:
  BENCH_FILTER: "zip_8192_from_(long|short).+non_nulls_scalars"
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                                                                      main                                   optimize-zip-offsets
-----                                                                      ----                                   --------------------
zip_8192_from_long bytes (100..400)/non_nulls_scalars/10pct_true           1.15     48.3±0.14µs        ? ?/sec    1.00     42.0±0.11µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/1pct_true            1.42     37.6±0.05µs        ? ?/sec    1.00     26.4±0.04µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/50pct_nulls          1.03     56.7±0.24µs        ? ?/sec    1.00     55.1±0.15µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/50pct_true           1.00     65.4±0.18µs        ? ?/sec    1.00     65.3±0.12µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/90pct_true           1.13     61.2±0.16µs        ? ?/sec    1.00     54.0±0.16µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/99pct_true           1.38     50.9±0.09µs        ? ?/sec    1.00     36.8±0.06µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/all_false            1.00     22.9±0.02µs        ? ?/sec    1.03     23.6±0.14µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/all_true             1.00     33.5±0.41µs        ? ?/sec    1.01     33.7±0.16µs        ? ?/sec
zip_8192_from_long bytes (100..400)/non_nulls_scalars/true_then_false      1.48     40.7±0.17µs        ? ?/sec    1.00     27.5±0.04µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/10pct_true         1.13     85.2±0.27µs        ? ?/sec    1.00     75.4±0.18µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/1pct_true          1.27     65.9±0.19µs        ? ?/sec    1.00     51.8±0.09µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/50pct_nulls        1.08     96.0±0.44µs        ? ?/sec    1.00     88.9±0.22µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/50pct_true         1.04     93.2±0.28µs        ? ?/sec    1.00     89.8±0.22µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/90pct_true         1.18     69.2±0.13µs        ? ?/sec    1.00     58.9±0.15µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/99pct_true         1.40     51.4±0.13µs        ? ?/sec    1.00     36.6±0.04µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/all_false          1.00     49.7±0.46µs        ? ?/sec    1.00     49.8±0.17µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/all_true           1.00     33.8±0.19µs        ? ?/sec    1.02     34.6±0.14µs        ? ?/sec
zip_8192_from_long strings (100..400)/non_nulls_scalars/true_then_false    1.31     52.8±0.63µs        ? ?/sec    1.00     40.1±0.79µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/10pct_true             1.59     24.7±0.06µs        ? ?/sec    1.00     15.5±0.02µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/1pct_true              3.22     19.1±0.03µs        ? ?/sec    1.00      5.9±0.00µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/50pct_nulls            1.21     30.6±0.08µs        ? ?/sec    1.00     25.2±0.11µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/50pct_true             1.09     36.1±0.11µs        ? ?/sec    1.00     33.2±0.13µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/90pct_true             1.59     24.9±0.08µs        ? ?/sec    1.00     15.6±0.03µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/99pct_true             3.81     19.8±0.02µs        ? ?/sec    1.00      5.2±0.01µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/all_false              1.00      3.6±0.00µs        ? ?/sec    1.00      3.6±0.00µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/all_true               1.00      3.9±0.00µs        ? ?/sec    1.00      3.9±0.01µs        ? ?/sec
zip_8192_from_short bytes (3..10)/non_nulls_scalars/true_then_false        4.36     17.6±0.02µs        ? ?/sec    1.00      4.0±0.00µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/10pct_true           1.52     25.5±0.08µs        ? ?/sec    1.00     16.7±0.02µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/1pct_true            3.06     19.4±0.02µs        ? ?/sec    1.00      6.3±0.01µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/50pct_nulls          1.12     29.4±0.08µs        ? ?/sec    1.00     26.2±0.10µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/50pct_true           1.01     35.2±0.12µs        ? ?/sec    1.00     34.8±0.13µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/90pct_true           1.54     24.8±0.07µs        ? ?/sec    1.00     16.2±0.03µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/99pct_true           3.74     19.7±0.02µs        ? ?/sec    1.00      5.3±0.01µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/all_false            1.00      3.8±0.01µs        ? ?/sec    1.00      3.8±0.00µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/all_true             1.01      3.9±0.01µs        ? ?/sec    1.00      3.9±0.01µs        ? ?/sec
zip_8192_from_short strings (3..10)/non_nulls_scalars/true_then_false      4.32     17.6±0.03µs        ? ?/sec    1.00      4.1±0.00µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 345.1s
Peak memory 19.5 MiB
Avg memory 13.6 MiB
CPU user 339.2s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 345.1s
Peak memory 20.4 MiB
Avg memory 14.4 MiB
CPU user 343.4s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@Rich-T-kid

Copy link
Copy Markdown
Contributor

run benchmark zip_kernels env: BENCH_FILTER: zip_8192_from_(long|short).+non_nulls_scalars

@Jefffrey I wouldn't expect there to be a 4x speed up from this PR. are the zip benchmarks usually this noisy?

@Jefffrey

Copy link
Copy Markdown
Contributor

im able to reproduce the results locally. maybe because its a scalar path the optimization has a larger effect

Jefffrey pushed a commit that referenced this pull request Sep 15, 2026
)

# Which issue does this PR close?

Part of #10245.

# Rationale for this change

`UnionBuilder`'s `FieldDataValues` trait is implemented for
`BufferBuilder<T>` and used purely as a growable, type-erased per-field
value buffer: `append`/`push` one value at a time, then
`finish()`/`finish_cloned()` into a `Buffer`. This is exactly the
scratch-buffer shape #10245 is tracking, and it was still on the
"remaining callsites" list (the two other listed sites, arity.rs and
zip.rs, already have PRs open: #10518 and #10913).

# What changes are included in this PR?

`impl<T: ArrowNativeType> FieldDataValues for BufferBuilder<T>` becomes
`impl<T: ArrowNativeType> FieldDataValues for Vec<T>`:

- `append_null` (`self.advance(1)`) becomes `self.push(T::default())`.
`BufferBuilder::advance` zero-pads; `ArrowNativeType: Default` is a
sealed, primitive-only supertrait bound, and zero is its default for
every implementor, so this is the same padding value.
- `finish` (`self.finish()`, which resets the builder via `mem::take`)
becomes `Buffer::from_vec(std::mem::take(self))` — same reset-and-return
shape. `finish` is only reached through `UnionBuilder::build(self)`,
which consumes the whole builder, so the reset value is never observed.
- `finish_cloned` keeps the existing
`Buffer::from_slice_ref(self.as_slice())` call unchanged — `Vec<T>` and
`BufferBuilder<T>` both expose `as_slice()`.
- `FieldData::new` and `FieldData::append_value`'s `downcast_mut` switch
from `BufferBuilder::<T::Native>` to `Vec::<T::Native>`, and
`.append(v)` becomes `.push(v)`.

`type_id_builder`/`value_offset_builder`
(`Int8BufferBuilder`/`Int32BufferBuilder`) are untouched — they're a
different pair of fields, not on #10245's callsite list, and out of
scope for this targeted change.

# Are these changes tested?

Yes, by the file's existing tests — no behavior changes, so no new test
was added (matching #10851, the other merged PR in this epic that also
didn't add one):

- `cargo test -p arrow-array --lib union`: 31 passed, 0 failed (includes
both `union_builder` tests plus every `union_array` test — dense/sparse,
with/without nulls, offsets — since `UnionArray` is built through
`UnionBuilder` or checked against its output in several of these).
- `cargo test -p arrow-array --doc union_builder`: 2 passed, 0 failed
(the dense/sparse doctests on `UnionBuilder` itself).
- `cargo clippy -p arrow-array --lib -- -D warnings`: no diagnostics on
`union_builder.rs`. (Unrelated pre-existing clippy findings in
`arrow-data/src/data.rs` reproduce identically on an unmodified `main`
under my clippy version and are not part of this PR.)
- `cargo fmt -p arrow-array -- --check`: clean.

I also ran a throwaway, uncommitted `cargo run --release` microbenchmark
(200,000 `UnionBuilder::append` + one `finish`, 5 runs, median reported)
to see whether this site shows the same kind of win as the zip PR. It
doesn't, and I want to report that honestly rather than imply a bigger
effect than exists: main (`BufferBuilder`) median 22.12ms (110.6 ns/row)
vs. this branch (`Vec`) median 22.04ms (110.2 ns/row) — roughly 0.3%,
inside run-to-run noise. `UnionBuilder::append_option` removes and
reinserts a `BTreeMap` entry and allocates a `String` key on every call,
which dominates the per-row cost far more than the value-buffer push
does, so this change doesn't move the needle on its own the way the zip
kernel's tighter loop did. I'm including it anyway because it still
removes one more `BufferBuilder` usage per #10245's stated goal, with no
behavior or performance regression.

# Are there any user-facing changes?

No. `FieldDataValues`/`FieldData` are private to this module;
`UnionBuilder`'s public API is unchanged.

# Automated assistance

This PR — the code change, the verification, and this description — was
drafted by an AI coding agent (Claude), reviewed and run by me before
opening. Per CONTRIBUTING.md's AI Generated Submissions guidance: I read
the whole diff and the surrounding `FieldDataValues`/`UnionBuilder` code
to confirm the `finish`-only-reachable-through-`build(self)` reasoning
above, ran every check listed under "Are these changes tested?" myself,
and the microbenchmark numbers are from an actual local run (not
fabricated) — reported as inconclusive/noise-level rather than rounded
up to look like a win.
@Jefffrey Jefffrey changed the title Replace OffsetBufferBuilder with Vec in byte-array zip Replace OffsetBufferBuilder with Vec in scalar byte-array zip Sep 17, 2026
@Jefffrey
Jefffrey merged commit 0a59795 into apache:main Sep 17, 2026
43 checks passed
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks @yashkuceriya & @Rich-T-kid

Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…che#11040)

# Which issue does this PR close?

Part of apache#10245.

# Rationale for this change

`UnionBuilder`'s `FieldDataValues` trait is implemented for
`BufferBuilder<T>` and used purely as a growable, type-erased per-field
value buffer: `append`/`push` one value at a time, then
`finish()`/`finish_cloned()` into a `Buffer`. This is exactly the
scratch-buffer shape apache#10245 is tracking, and it was still on the
"remaining callsites" list (the two other listed sites, arity.rs and
zip.rs, already have PRs open: apache#10518 and apache#10913).

# What changes are included in this PR?

`impl<T: ArrowNativeType> FieldDataValues for BufferBuilder<T>` becomes
`impl<T: ArrowNativeType> FieldDataValues for Vec<T>`:

- `append_null` (`self.advance(1)`) becomes `self.push(T::default())`.
`BufferBuilder::advance` zero-pads; `ArrowNativeType: Default` is a
sealed, primitive-only supertrait bound, and zero is its default for
every implementor, so this is the same padding value.
- `finish` (`self.finish()`, which resets the builder via `mem::take`)
becomes `Buffer::from_vec(std::mem::take(self))` — same reset-and-return
shape. `finish` is only reached through `UnionBuilder::build(self)`,
which consumes the whole builder, so the reset value is never observed.
- `finish_cloned` keeps the existing
`Buffer::from_slice_ref(self.as_slice())` call unchanged — `Vec<T>` and
`BufferBuilder<T>` both expose `as_slice()`.
- `FieldData::new` and `FieldData::append_value`'s `downcast_mut` switch
from `BufferBuilder::<T::Native>` to `Vec::<T::Native>`, and
`.append(v)` becomes `.push(v)`.

`type_id_builder`/`value_offset_builder`
(`Int8BufferBuilder`/`Int32BufferBuilder`) are untouched — they're a
different pair of fields, not on apache#10245's callsite list, and out of
scope for this targeted change.

# Are these changes tested?

Yes, by the file's existing tests — no behavior changes, so no new test
was added (matching apache#10851, the other merged PR in this epic that also
didn't add one):

- `cargo test -p arrow-array --lib union`: 31 passed, 0 failed (includes
both `union_builder` tests plus every `union_array` test — dense/sparse,
with/without nulls, offsets — since `UnionArray` is built through
`UnionBuilder` or checked against its output in several of these).
- `cargo test -p arrow-array --doc union_builder`: 2 passed, 0 failed
(the dense/sparse doctests on `UnionBuilder` itself).
- `cargo clippy -p arrow-array --lib -- -D warnings`: no diagnostics on
`union_builder.rs`. (Unrelated pre-existing clippy findings in
`arrow-data/src/data.rs` reproduce identically on an unmodified `main`
under my clippy version and are not part of this PR.)
- `cargo fmt -p arrow-array -- --check`: clean.

I also ran a throwaway, uncommitted `cargo run --release` microbenchmark
(200,000 `UnionBuilder::append` + one `finish`, 5 runs, median reported)
to see whether this site shows the same kind of win as the zip PR. It
doesn't, and I want to report that honestly rather than imply a bigger
effect than exists: main (`BufferBuilder`) median 22.12ms (110.6 ns/row)
vs. this branch (`Vec`) median 22.04ms (110.2 ns/row) — roughly 0.3%,
inside run-to-run noise. `UnionBuilder::append_option` removes and
reinserts a `BTreeMap` entry and allocates a `String` key on every call,
which dominates the per-row cost far more than the value-buffer push
does, so this change doesn't move the needle on its own the way the zip
kernel's tighter loop did. I'm including it anyway because it still
removes one more `BufferBuilder` usage per apache#10245's stated goal, with no
behavior or performance regression.

# Are there any user-facing changes?

No. `FieldDataValues`/`FieldData` are private to this module;
`UnionBuilder`'s public API is unchanged.

# Automated assistance

This PR — the code change, the verification, and this description — was
drafted by an AI coding agent (Claude), reviewed and run by me before
opening. Per CONTRIBUTING.md's AI Generated Submissions guidance: I read
the whole diff and the surrounding `FieldDataValues`/`UnionBuilder` code
to confirm the `finish`-only-reachable-through-`build(self)` reasoning
above, ran every check listed under "Are these changes tested?" myself,
and the microbenchmark numbers are from an actual local run (not
fabricated) — reported as inconclusive/noise-level rather than rounded
up to look like a win.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…#10913)

# Which issue does this PR close?

Part of apache#10245.

# Rationale for this change

The byte-array scalar zip path is one of the remaining
`OffsetBufferBuilder` callsites in apache#10245. Building cumulative offsets
directly in a `Vec` avoids checking each offset through the builder,
which benefits masks containing long runs of the same value.

# What changes are included in this PR?

`BytesScalarImpl::create_output_on_non_nulls` builds offsets with
`Vec<T::Offset>`. The mixed-mask path checks the total byte size and
offset range before allocating the output. An overflowing size returns
`MemoryError`; a size outside the offset type returns
`OffsetOverflowError`. Every generated offset is bounded by that checked
total.

The existing all-true, all-false, and nullable-scalar fast paths are
unchanged.

# Are these changes tested?

- `cargo test -p arrow-select --quiet`: 418 unit tests and 17
documentation tests passed.
- `cargo clippy -p arrow-select --all-targets --all-features -- -D
warnings`: passed.
- `cargo +stable fmt --all -- --check`: passed.

The new regression test uses a 64 KiB binary scalar and a mixed mask
whose output would require 2 GiB. It checks that the call returns
`OffsetOverflowError` before allocating that output, with the large
scalar on either side of the mask. Existing tests cover normal and large
byte arrays, nulls, fragmented masks, and one-sided scalar paths.

## Benchmarks

The project runner measured the previous revision, `4829c8f6d`,
[here](apache#10913 (comment)).
Those results do not include the review fix.

Local measurements using the existing `arrow/benches/zip_kernels.rs`
benchmark, on macOS arm64 with Rust 1.97.1:

| Case, 8,192 rows | Upstream `cbbb56bba` | Previous PR `4829c8f6d` |
Revised `1ec832648` |
| --- | ---: | ---: | ---: |
| Short strings, 50% true | 40.00 us | 38.78 us | 38.40 us |
| Short strings, true then false | 7.04 us | 2.24 us | 2.24 us |
| Long strings, 50% true | 65.12 us | 64.14 us | 63.88 us |
| Long strings, true then false | 41.90 us | 35.69 us | 36.84 us |
| Short bytes, 50% true | 36.66 us | 34.84 us | 32.96 us |
| Short bytes, true then false | 7.01 us | 2.08 us | 1.99 us |
| Long bytes, 50% true | 54.93 us | 52.28 us | 53.47 us |
| Long bytes, true then false | 32.06 us | 25.53 us | 26.71 us |

These are short, sequential local runs, not a controlled performance
study. The revision remains faster than the upstream baseline in these
cases, but some long-value cases are slower than the previous PR
revision. A project-runner rerun would help confirm the impact of the
added checks.

Command, run for each revision with separate Criterion baselines:

```sh
cargo bench -p arrow --features test_utils --bench zip_kernels -- \
  'zip_8192_from_(long|short).+non_nulls_scalars/(50pct_true|true_then_false)$' \
  --sample-size 20 --warm-up-time 0.5 --measurement-time 1
```

# Are there any user-facing changes?

Mixed-mask scalar byte-array output that exceeds the offset range now
returns an error before output allocation instead of panicking. No
public API changes.

# Automated assistance

AI assistance was used to prepare the refactor, review fix, regression
test, and validation. The commands and measurements above were run
locally.

---------

Co-authored-by: Yash Kuceriya <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…che#11040)

# Which issue does this PR close?

Part of apache#10245.

# Rationale for this change

`UnionBuilder`'s `FieldDataValues` trait is implemented for
`BufferBuilder<T>` and used purely as a growable, type-erased per-field
value buffer: `append`/`push` one value at a time, then
`finish()`/`finish_cloned()` into a `Buffer`. This is exactly the
scratch-buffer shape apache#10245 is tracking, and it was still on the
"remaining callsites" list (the two other listed sites, arity.rs and
zip.rs, already have PRs open: apache#10518 and apache#10913).

# What changes are included in this PR?

`impl<T: ArrowNativeType> FieldDataValues for BufferBuilder<T>` becomes
`impl<T: ArrowNativeType> FieldDataValues for Vec<T>`:

- `append_null` (`self.advance(1)`) becomes `self.push(T::default())`.
`BufferBuilder::advance` zero-pads; `ArrowNativeType: Default` is a
sealed, primitive-only supertrait bound, and zero is its default for
every implementor, so this is the same padding value.
- `finish` (`self.finish()`, which resets the builder via `mem::take`)
becomes `Buffer::from_vec(std::mem::take(self))` — same reset-and-return
shape. `finish` is only reached through `UnionBuilder::build(self)`,
which consumes the whole builder, so the reset value is never observed.
- `finish_cloned` keeps the existing
`Buffer::from_slice_ref(self.as_slice())` call unchanged — `Vec<T>` and
`BufferBuilder<T>` both expose `as_slice()`.
- `FieldData::new` and `FieldData::append_value`'s `downcast_mut` switch
from `BufferBuilder::<T::Native>` to `Vec::<T::Native>`, and
`.append(v)` becomes `.push(v)`.

`type_id_builder`/`value_offset_builder`
(`Int8BufferBuilder`/`Int32BufferBuilder`) are untouched — they're a
different pair of fields, not on apache#10245's callsite list, and out of
scope for this targeted change.

# Are these changes tested?

Yes, by the file's existing tests — no behavior changes, so no new test
was added (matching apache#10851, the other merged PR in this epic that also
didn't add one):

- `cargo test -p arrow-array --lib union`: 31 passed, 0 failed (includes
both `union_builder` tests plus every `union_array` test — dense/sparse,
with/without nulls, offsets — since `UnionArray` is built through
`UnionBuilder` or checked against its output in several of these).
- `cargo test -p arrow-array --doc union_builder`: 2 passed, 0 failed
(the dense/sparse doctests on `UnionBuilder` itself).
- `cargo clippy -p arrow-array --lib -- -D warnings`: no diagnostics on
`union_builder.rs`. (Unrelated pre-existing clippy findings in
`arrow-data/src/data.rs` reproduce identically on an unmodified `main`
under my clippy version and are not part of this PR.)
- `cargo fmt -p arrow-array -- --check`: clean.

I also ran a throwaway, uncommitted `cargo run --release` microbenchmark
(200,000 `UnionBuilder::append` + one `finish`, 5 runs, median reported)
to see whether this site shows the same kind of win as the zip PR. It
doesn't, and I want to report that honestly rather than imply a bigger
effect than exists: main (`BufferBuilder`) median 22.12ms (110.6 ns/row)
vs. this branch (`Vec`) median 22.04ms (110.2 ns/row) — roughly 0.3%,
inside run-to-run noise. `UnionBuilder::append_option` removes and
reinserts a `BTreeMap` entry and allocates a `String` key on every call,
which dominates the per-row cost far more than the value-buffer push
does, so this change doesn't move the needle on its own the way the zip
kernel's tighter loop did. I'm including it anyway because it still
removes one more `BufferBuilder` usage per apache#10245's stated goal, with no
behavior or performance regression.

# Are there any user-facing changes?

No. `FieldDataValues`/`FieldData` are private to this module;
`UnionBuilder`'s public API is unchanged.

# Automated assistance

This PR — the code change, the verification, and this description — was
drafted by an AI coding agent (Claude), reviewed and run by me before
opening. Per CONTRIBUTING.md's AI Generated Submissions guidance: I read
the whole diff and the surrounding `FieldDataValues`/`UnionBuilder` code
to confirm the `finish`-only-reachable-through-`build(self)` reasoning
above, ran every check listed under "Are these changes tested?" myself,
and the microbenchmark numbers are from an actual local run (not
fabricated) — reported as inconclusive/noise-level rather than rounded
up to look like a win.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…#10913)

# Which issue does this PR close?

Part of apache#10245.

# Rationale for this change

The byte-array scalar zip path is one of the remaining
`OffsetBufferBuilder` callsites in apache#10245. Building cumulative offsets
directly in a `Vec` avoids checking each offset through the builder,
which benefits masks containing long runs of the same value.

# What changes are included in this PR?

`BytesScalarImpl::create_output_on_non_nulls` builds offsets with
`Vec<T::Offset>`. The mixed-mask path checks the total byte size and
offset range before allocating the output. An overflowing size returns
`MemoryError`; a size outside the offset type returns
`OffsetOverflowError`. Every generated offset is bounded by that checked
total.

The existing all-true, all-false, and nullable-scalar fast paths are
unchanged.

# Are these changes tested?

- `cargo test -p arrow-select --quiet`: 418 unit tests and 17
documentation tests passed.
- `cargo clippy -p arrow-select --all-targets --all-features -- -D
warnings`: passed.
- `cargo +stable fmt --all -- --check`: passed.

The new regression test uses a 64 KiB binary scalar and a mixed mask
whose output would require 2 GiB. It checks that the call returns
`OffsetOverflowError` before allocating that output, with the large
scalar on either side of the mask. Existing tests cover normal and large
byte arrays, nulls, fragmented masks, and one-sided scalar paths.

## Benchmarks

The project runner measured the previous revision, `4829c8f6d`,
[here](apache#10913 (comment)).
Those results do not include the review fix.

Local measurements using the existing `arrow/benches/zip_kernels.rs`
benchmark, on macOS arm64 with Rust 1.97.1:

| Case, 8,192 rows | Upstream `cbbb56bba` | Previous PR `4829c8f6d` |
Revised `1ec832648` |
| --- | ---: | ---: | ---: |
| Short strings, 50% true | 40.00 us | 38.78 us | 38.40 us |
| Short strings, true then false | 7.04 us | 2.24 us | 2.24 us |
| Long strings, 50% true | 65.12 us | 64.14 us | 63.88 us |
| Long strings, true then false | 41.90 us | 35.69 us | 36.84 us |
| Short bytes, 50% true | 36.66 us | 34.84 us | 32.96 us |
| Short bytes, true then false | 7.01 us | 2.08 us | 1.99 us |
| Long bytes, 50% true | 54.93 us | 52.28 us | 53.47 us |
| Long bytes, true then false | 32.06 us | 25.53 us | 26.71 us |

These are short, sequential local runs, not a controlled performance
study. The revision remains faster than the upstream baseline in these
cases, but some long-value cases are slower than the previous PR
revision. A project-runner rerun would help confirm the impact of the
added checks.

Command, run for each revision with separate Criterion baselines:

```sh
cargo bench -p arrow --features test_utils --bench zip_kernels -- \
  'zip_8192_from_(long|short).+non_nulls_scalars/(50pct_true|true_then_false)$' \
  --sample-size 20 --warm-up-time 0.5 --measurement-time 1
```

# Are there any user-facing changes?

Mixed-mask scalar byte-array output that exceeds the offset range now
returns an error before output allocation instead of panicking. No
public API changes.

# Automated assistance

AI assistance was used to prepare the refactor, review fix, regression
test, and validation. The commands and measurements above were run
locally.

---------

Co-authored-by: Yash Kuceriya <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate arrow-select performance Speed or memory optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants