Repository navigation
perf: optimize loops for primitive types binary kernels. - #10518
Anakin100100 wants to merge 2 commits into
Conversation
|
run benchmarks arithmetic_kernels bitwise_kernel decimal_arithmetic |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing perf/arithmetic-buffer-optimization (9f4e3b3) to ed92960 (merge-base) diff Run configurationrun benchmark bitwise_kernelCPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing perf/arithmetic-buffer-optimization (9f4e3b3) to ed92960 (merge-base) diff Run configurationrun benchmark decimal_arithmeticCPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing perf/arithmetic-buffer-optimization (9f4e3b3) to ed92960 (merge-base) diff Run configurationrun benchmark arithmetic_kernelsCPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
|
||
| let buffer: Vec<_> = values.collect(); | ||
| let len = a.len(); | ||
| let byte_width = O::Native::get_byte_width(); |
There was a problem hiding this comment.
binary: replaced iterator collect with a direct loop which LLVM can better optimize and avoids option checks. The loop overhead here is a large part of the runtime when the underlying op is simple.
this seems surprising to me since as we were iterating+zipping over values() there wouldnt be any option checks going on
| F: Fn(A::Item, B::Item) -> Result<O::Native, ArrowError>, | ||
| { | ||
| let mut buffer = MutableBuffer::new(len * O::Native::get_byte_width()); | ||
| let byte_width = O::Native::get_byte_width(); |
There was a problem hiding this comment.
does this get better performance than simply using a vec here?
| let values = Buffer::from(buffer).into(); | ||
| Ok(PrimitiveArray::new(values, Some(nulls))) |
There was a problem hiding this comment.
| let values = Buffer::from(buffer).into(); | |
| Ok(PrimitiveArray::new(values, Some(nulls))) | |
| Ok(PrimitiveArray::new(buffer.into(), Some(nulls))) |
|
Marking as draft as I think this PR is no longer waiting on feedback and I am trying to make it easier to find PRs in need of review. Please mark it as ready for review when it is ready for another look |
|
@Anakin100100 do you have time to complete this PR / respond to feedback from @Jefffrey ? |
) # 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.
…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.
…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.
|
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days. |
|
This PR appears to be abandoned. Please reopen and respond to @Jefffrey 's reviews if you want to proceed Thank you |
Which issue does this PR close?
Part of #10245
Rationale for this change
Improves performance of the arithmetic kernels operating on primite types.
What changes are included in this PR?
I read the issue and focused on try_binary and discovered a few other optimizations there. All mentions of benchmarks here refer to decimal_arithmetic benchmark which covers addition and subtraction on
binary: replaced iterator collect with a direct loop which LLVM can better optimize and avoids option checks. The loop overhead here is a large part of the runtime when the underlying op is simple.
try_binary: in the null path uses Vec directly to create the buffer instead of going through the BufferBuilder. I tested it with a benchmark with 10-90% nulls and there is 5-9% improvement to the overall runtime. I don't think there's reason to include this benchmark in the library because it's very specific to this path and not general enough to be useful in the future.
try_binary_no_nulls: the typed array slice write can be vectorized more easily, Independent of that I verified that removing the no-inline annotation increases performance by additional 5% which makes sense with a more direct loop.
On average these changes increase the throughput of the kernels by 26-27% on same scale and 14-15% of different scale benchamrks for 32 and 64 bit types because the loop is a smaller fraction in the second case.
decimal32_equal_scale/sub time: [917.00 ns 923.95 ns 932.62 ns] thrpt: [1.0980 Gelem/s 1.1083 Gelem/s 1.1167 Gelem/s] change: time: [−23.032% −21.954% −20.901%] (p = 0.00 < 0.05) thrpt: [+26.424% +28.130% +29.924%] decimal64_different_scale/add time: [1.9084 µs 1.9212 µs 1.9366 µs] thrpt: [528.77 Melem/s 533.00 Melem/s 536.59 Melem/s] change: time: [−15.134% −14.025% −12.902%] (p = 0.00 < 0.05) thrpt: [+14.813% +16.313% +17.833%]On 128 and 256 bit types there is around 5% speedup because of more complex math behind the underlying op.
Are these changes tested?
Yes, already covered by tests.
Are there any user-facing changes?
No