Skip to content

(arrow-row) Replace BufferBuilder with Vec in decode_binary - #10851

Merged
Jefffrey merged 1 commit into
apache:mainfrom
zhangxinyao88:codex/arrow-rs-10245-decode-binary
Aug 26, 2026
Merged

Jefffrey merged 1 commit into
apache:mainfrom
zhangxinyao88:codex/arrow-rs-10245-decode-binary

Conversation

@zhangxinyao88

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

This replaces one of the remaining BufferBuilder callsites listed in #10245. Using Vec for the offsets avoids the builder overhead while preserving the resulting ScalarBuffer.

What changes are included in this PR?

decode_binary now constructs its offsets with Vec<I> instead of BufferBuilder<I>.

Are these changes tested?

Yes:

  • cargo fmt --all -- --check
  • cargo test -p arrow-row — 95 unit tests and 8 doc tests passed
  • cargo clippy -p arrow-row --all-targets --all-features -- -D warnings

Existing round-trip tests cover the changed path, so no new tests were added.

Benchmark: convert_rows 4096 string(100, 0)

Time
main 47.769 µs
This PR 45.488 µs

Criterion reported a 4.4% improvement.

Are there any user-facing changes?

No.

AI assistance

Codex was used to identify the callsite, prepare the focused refactor, run validation, and draft this description.

@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-row arrow-row crate labels Aug 26, 2026
@Jefffrey

Copy link
Copy Markdown
Contributor

run benchmark row_format
env:
BENCH_FILTER: convert_rows

@Jefffrey Jefffrey added the performance Speed or memory optimizations label Aug 26, 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 codex/arrow-rs-10245-decode-binary (1b9cd7a) to 2a82e59 (merge-base) diff

Run configuration
run benchmark row_format
env:
  BENCH_FILTER: "convert_rows"
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                                                                                                             codex_arrow-rs-10245-decode-binary     main
-----                                                                                                             ----------------------------------     ----
convert_rows 10 large_list(0) of u64(0)                                                                           1.00   923.6±16.50ns        ? ?/sec    1.01    933.6±6.21ns        ? ?/sec
convert_rows 10 list(0) of u64(0)                                                                                 1.00   987.8±12.90ns        ? ?/sec    1.00    989.4±6.02ns        ? ?/sec
convert_rows 4096 4096 string_dictionary(20, 0.5), string_dictionary(30, 0), string_dictionary(100, 0), i64(0)    1.00    226.7±0.93µs        ? ?/sec    1.01    228.8±1.32µs        ? ?/sec
convert_rows 4096 53 columns                                                                                      1.01  1807.8±13.49µs        ? ?/sec    1.00  1794.1±13.36µs        ? ?/sec
convert_rows 4096 bool(0, 0.5)                                                                                    1.00     18.5±0.42µs        ? ?/sec    1.02     18.8±1.16µs        ? ?/sec
convert_rows 4096 bool(0.3, 0.5)                                                                                  1.00     18.9±0.61µs        ? ?/sec    1.02     19.2±1.12µs        ? ?/sec
convert_rows 4096 i64(0)                                                                                          1.00     31.1±0.46µs        ? ?/sec    1.01     31.6±1.15µs        ? ?/sec
convert_rows 4096 i64(0.3)                                                                                        1.00     31.6±0.78µs        ? ?/sec    1.01     31.8±1.16µs        ? ?/sec
convert_rows 4096 large_list(0) of u64(0)                                                                         1.00    173.4±0.68µs        ? ?/sec    1.00    173.1±1.29µs        ? ?/sec
convert_rows 4096 large_list(0) sliced to 10 of u64(0)                                                            1.00   1217.2±3.49ns        ? ?/sec    1.01   1230.5±8.46ns        ? ?/sec
convert_rows 4096 list(0) of u64(0)                                                                               1.00    161.7±0.66µs        ? ?/sec    1.07    173.0±1.50µs        ? ?/sec
convert_rows 4096 list(0) sliced to 10 of u64(0)                                                                  1.00   1334.3±6.21ns        ? ?/sec    1.01   1345.5±7.97ns        ? ?/sec
convert_rows 4096 run_primitive(1024 physical)                                                                    1.00     83.6±0.60µs        ? ?/sec    1.01     84.0±1.31µs        ? ?/sec
convert_rows 4096 run_primitive(256 physical)                                                                     1.00     60.7±0.74µs        ? ?/sec    1.00     60.4±1.29µs        ? ?/sec
convert_rows 4096 run_primitive(512 physical)                                                                     1.00     68.6±0.69µs        ? ?/sec    1.00     68.7±1.30µs        ? ?/sec
convert_rows 4096 string view(1..100, 0)                                                                          1.00     70.9±0.63µs        ? ?/sec    1.01     71.5±1.19µs        ? ?/sec
convert_rows 4096 string view(1..100, 0.5)                                                                        1.00     46.7±0.51µs        ? ?/sec    1.02     47.8±1.25µs        ? ?/sec
convert_rows 4096 string view(10, 0)                                                                              1.00     48.4±0.39µs        ? ?/sec    1.00     48.5±1.29µs        ? ?/sec
convert_rows 4096 string view(100, 0)                                                                             1.00    102.0±0.54µs        ? ?/sec    1.03    104.6±1.45µs        ? ?/sec
convert_rows 4096 string view(100, 0.5)                                                                           1.08     67.3±0.54µs        ? ?/sec    1.00     62.0±1.35µs        ? ?/sec
convert_rows 4096 string view(30, 0)                                                                              1.01     64.3±0.52µs        ? ?/sec    1.00     63.6±1.23µs        ? ?/sec
convert_rows 4096 string(10, 0)                                                                                   1.00     46.6±0.70µs        ? ?/sec    1.02     47.6±1.06µs        ? ?/sec
convert_rows 4096 string(100, 0)                                                                                  1.00    103.7±0.43µs        ? ?/sec    1.01    104.7±1.31µs        ? ?/sec
convert_rows 4096 string(100, 0.5)                                                                                1.00     62.5±0.47µs        ? ?/sec    1.01     62.9±1.17µs        ? ?/sec
convert_rows 4096 string(20, 0.5), string(30, 0), string(100, 0), i64(0)                                          1.00    226.2±0.73µs        ? ?/sec    1.02    229.9±1.19µs        ? ?/sec
convert_rows 4096 string(30, 0)                                                                                   1.03     70.9±0.50µs        ? ?/sec    1.00     69.0±1.20µs        ? ?/sec
convert_rows 4096 string_dictionary(10, 0)                                                                        1.05     48.1±0.48µs        ? ?/sec    1.00     45.7±1.08µs        ? ?/sec
convert_rows 4096 string_dictionary(100, 0)                                                                       1.00    104.4±0.51µs        ? ?/sec    1.00    104.7±1.19µs        ? ?/sec
convert_rows 4096 string_dictionary(100, 0.5)                                                                     1.00     62.4±0.48µs        ? ?/sec    1.01     63.3±1.15µs        ? ?/sec
convert_rows 4096 string_dictionary(30, 0)                                                                        1.01     70.1±0.49µs        ? ?/sec    1.00     69.1±1.24µs        ? ?/sec
convert_rows 4096 string_dictionary_low_cardinality(10, 0)                                                        1.02     46.5±0.46µs        ? ?/sec    1.00     45.5±1.13µs        ? ?/sec
convert_rows 4096 string_dictionary_low_cardinality(100, 0)                                                       1.00    103.9±0.49µs        ? ?/sec    1.00    104.1±1.20µs        ? ?/sec
convert_rows 4096 string_dictionary_low_cardinality(30, 0)                                                        1.01     70.0±0.50µs        ? ?/sec    1.00     69.1±1.21µs        ? ?/sec
convert_rows 4096 u64(0)                                                                                          1.00     31.3±0.69µs        ? ?/sec    1.00     31.4±1.20µs        ? ?/sec
convert_rows 4096 u64(0.3)                                                                                        1.00     31.5±0.80µs        ? ?/sec    1.00     31.6±1.17µs        ? ?/sec
convert_rows 8192 53 columns                                                                                      1.00      4.6±0.58ms        ? ?/sec    1.02      4.7±0.60ms        ? ?/sec
convert_rows_parsed 4096 string view(1..100, 0)                                                                   1.22     90.5±0.35µs        ? ?/sec    1.00     74.0±0.13µs        ? ?/sec
convert_rows_parsed 4096 string view(1..100, 0.5)                                                                 1.61     74.0±0.48µs        ? ?/sec    1.00     46.1±0.07µs        ? ?/sec
convert_rows_parsed 4096 string view(10, 0)                                                                       1.00     50.2±0.14µs        ? ?/sec    1.00     50.1±0.31µs        ? ?/sec
convert_rows_parsed 4096 string view(100, 0)                                                                      1.03    112.7±0.30µs        ? ?/sec    1.00    109.8±0.12µs        ? ?/sec
convert_rows_parsed 4096 string view(100, 0.5)                                                                    1.08     69.4±0.22µs        ? ?/sec    1.00     64.2±0.64µs        ? ?/sec
convert_rows_parsed 4096 string view(30, 0)                                                                       1.00     61.9±0.08µs        ? ?/sec    1.00     62.0±0.12µs        ? ?/sec
convert_rows_parsed 4096 string(10, 0)                                                                            1.02     45.4±0.10µs        ? ?/sec    1.00     44.7±0.24µs        ? ?/sec
convert_rows_parsed 4096 string(100, 0)                                                                           1.00    116.8±0.26µs        ? ?/sec    1.00    117.2±0.16µs        ? ?/sec
convert_rows_parsed 4096 string(100, 0.5)                                                                         1.00     67.8±0.24µs        ? ?/sec    1.00     67.8±0.31µs        ? ?/sec
convert_rows_parsed 4096 string(30, 0)                                                                            1.05     74.4±0.08µs        ? ?/sec    1.00     71.0±0.64µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 455.1s
Peak memory 21.2 MiB
Avg memory 9.5 MiB
CPU user 452.3s
CPU sys 0.1s
Peak spill 0 B

branch

Metric Value
Wall time 450.1s
Peak memory 21.3 MiB
Avg memory 10.0 MiB
CPU user 447.5s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

@Jefffrey
Jefffrey merged commit 736090f into apache:main Aug 26, 2026
26 checks passed
@Jefffrey Jefffrey changed the title Replace BufferBuilder with Vec in decode_binary (arrow-row) Replace BufferBuilder with Vec in decode_binary Aug 26, 2026
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks @zhangxinyao88

Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Aug 28, 2026
# Which issue does this PR close?

- Part of apache#10245.

# Rationale for this change

This replaces one of the remaining `BufferBuilder` callsites listed in
apache#10245. Using `Vec` for the offsets avoids the builder overhead while
preserving the resulting `ScalarBuffer`.

# What changes are included in this PR?

`decode_binary` now constructs its offsets with `Vec<I>` instead of
`BufferBuilder<I>`.

# Are these changes tested?

Yes:

- `cargo fmt --all -- --check`
- `cargo test -p arrow-row` — 95 unit tests and 8 doc tests passed
- `cargo clippy -p arrow-row --all-targets --all-features -- -D
warnings`

Existing round-trip tests cover the changed path, so no new tests were
added.

Benchmark: `convert_rows 4096 string(100, 0)`

| | Time |
| --- | ---: |
| `main` | 47.769 µs |
| This PR | 45.488 µs |

Criterion reported a 4.4% improvement.

# Are there any user-facing changes?

No.

# AI assistance

Codex was used to identify the callsite, prepare the focused refactor,
run validation, and draft this description.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 2, 2026
# Which issue does this PR close?

- Part of apache#10245.

# Rationale for this change

This replaces one of the remaining `BufferBuilder` callsites listed in
apache#10245. Using `Vec` for the offsets avoids the builder overhead while
preserving the resulting `ScalarBuffer`.

# What changes are included in this PR?

`decode_binary` now constructs its offsets with `Vec<I>` instead of
`BufferBuilder<I>`.

# Are these changes tested?

Yes:

- `cargo fmt --all -- --check`
- `cargo test -p arrow-row` — 95 unit tests and 8 doc tests passed
- `cargo clippy -p arrow-row --all-targets --all-features -- -D
warnings`

Existing round-trip tests cover the changed path, so no new tests were
added.

Benchmark: `convert_rows 4096 string(100, 0)`

| | Time |
| --- | ---: |
| `main` | 47.769 µs |
| This PR | 45.488 µs |

Criterion reported a 4.4% improvement.

# Are there any user-facing changes?

No.

# AI assistance

Codex was used to identify the callsite, prepare the focused refactor,
run validation, and draft this description.
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.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
# Which issue does this PR close?

- Part of apache#10245.

# Rationale for this change

This replaces one of the remaining `BufferBuilder` callsites listed in
apache#10245. Using `Vec` for the offsets avoids the builder overhead while
preserving the resulting `ScalarBuffer`.

# What changes are included in this PR?

`decode_binary` now constructs its offsets with `Vec<I>` instead of
`BufferBuilder<I>`.

# Are these changes tested?

Yes:

- `cargo fmt --all -- --check`
- `cargo test -p arrow-row` — 95 unit tests and 8 doc tests passed
- `cargo clippy -p arrow-row --all-targets --all-features -- -D
warnings`

Existing round-trip tests cover the changed path, so no new tests were
added.

Benchmark: `convert_rows 4096 string(100, 0)`

| | Time |
| --- | ---: |
| `main` | 47.769 µs |
| This PR | 45.488 µs |

Criterion reported a 4.4% improvement.

# Are there any user-facing changes?

No.

# AI assistance

Codex was used to identify the callsite, prepare the focused refactor,
run validation, and draft this description.
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 21, 2026
# Which issue does this PR close?

- Part of apache#10245.

# Rationale for this change

This replaces one of the remaining `BufferBuilder` callsites listed in
apache#10245. Using `Vec` for the offsets avoids the builder overhead while
preserving the resulting `ScalarBuffer`.

# What changes are included in this PR?

`decode_binary` now constructs its offsets with `Vec<I>` instead of
`BufferBuilder<I>`.

# Are these changes tested?

Yes:

- `cargo fmt --all -- --check`
- `cargo test -p arrow-row` — 95 unit tests and 8 doc tests passed
- `cargo clippy -p arrow-row --all-targets --all-features -- -D
warnings`

Existing round-trip tests cover the changed path, so no new tests were
added.

Benchmark: `convert_rows 4096 string(100, 0)`

| | Time |
| --- | ---: |
| `main` | 47.769 µs |
| This PR | 45.488 µs |

Criterion reported a 4.4% improvement.

# Are there any user-facing changes?

No.

# AI assistance

Codex was used to identify the callsite, prepare the focused refactor,
run validation, and draft this description.
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 25, 2026
# Which issue does this PR close?

- Part of apache#10245.

# Rationale for this change

This replaces one of the remaining `BufferBuilder` callsites listed in
apache#10245. Using `Vec` for the offsets avoids the builder overhead while
preserving the resulting `ScalarBuffer`.

# What changes are included in this PR?

`decode_binary` now constructs its offsets with `Vec<I>` instead of
`BufferBuilder<I>`.

# Are these changes tested?

Yes:

- `cargo fmt --all -- --check`
- `cargo test -p arrow-row` — 95 unit tests and 8 doc tests passed
- `cargo clippy -p arrow-row --all-targets --all-features -- -D
warnings`

Existing round-trip tests cover the changed path, so no new tests were
added.

Benchmark: `convert_rows 4096 string(100, 0)`

| | Time |
| --- | ---: |
| `main` | 47.769 µs |
| This PR | 45.488 µs |

Criterion reported a 4.4% improvement.

# Are there any user-facing changes?

No.

# AI assistance

Codex was used to identify the callsite, prepare the focused refactor,
run validation, and draft this description.
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-row arrow-row crate performance Speed or memory optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants