Repository navigation
Replace OffsetBufferBuilder with Vec in scalar byte-array zip - #10913
Conversation
|
run benchmark zip_kernels |
|
🤖 Arrow criterion benchmark running (GKE) | trigger CPU Details (lscpu)Comparing optimize-zip-offsets (4829c8f) to cbbb56b (merge-base) diff Run configurationrun benchmark zip_kernelsBENCH_COMMAND=cargo bench --features=arrow,async,test_common,experimental,object_store --bench zip_kernels File an issue against this benchmark runner |
Rich-T-kid
left a comment
There was a problem hiding this comment.
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
| offset_buffer_builder.push_length(falsy_len) | ||
| } | ||
| let start_offset = current_offset; | ||
| let added = falsy_len.checked_mul(false_repeat_count).expect("overflow"); |
There was a problem hiding this comment.
we should avoid panicking here. return an error
There was a problem hiding this comment.
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?
This comment was marked as outdated.
This comment was marked as outdated.
|
zip generally takes too long, you'll need to run with specific bench filters @Rich-T-kid |
|
run benchmark zip_kernels env: |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing optimize-zip-offsets (4829c8f) to cbbb56b (merge-base) diff Run configurationrun benchmark zip_kernels
env:
BENCH_FILTER: "non_nulls_scalars"CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
run benchmark zip_kernels |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing optimize-zip-offsets (4829c8f) to cbbb56b (merge-base) diff Run configurationrun benchmark zip_kernels
env:
BENCH_FILTER: "zip_8192_from_(long|short).+non_nulls_scalars"CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
@Jefffrey I wouldn't expect there to be a 4x speed up from this PR. are the zip benchmarks usually this noisy? |
|
im able to reproduce the results locally. maybe because its a scalar path the optimization has a larger effect |
) # 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.
|
thanks @yashkuceriya & @Rich-T-kid |
…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.
…#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]>
…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.
…#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]>
Which issue does this PR close?
Part of #10245.
Rationale for this change
The byte-array scalar zip path is one of the remaining
OffsetBufferBuildercallsites in #10245. Building cumulative offsets directly in aVecavoids 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_nullsbuilds offsets withVec<T::Offset>. The mixed-mask path checks the total byte size and offset range before allocating the output. An overflowing size returnsMemoryError; a size outside the offset type returnsOffsetOverflowError. 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
OffsetOverflowErrorbefore 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.rsbenchmark, on macOS arm64 with Rust 1.97.1:cbbb56bba4829c8f6d1ec832648These 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 1Are 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.