Repository navigation
perf(arrow-arith): vectorize checked decimal add, sub and mul - #12062
Conversation
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C target-cpu=x86-64-v3
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-oclc6q2e-df121e2a-ln85 Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C target-cpu=x86-64-v3 -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-oclc6q2e-64c98ba8-w28v Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C target-cpu=x86-64-v3
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-11j2ashw-1b098063-44mm Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-11j2ashw-5b2a8cd0-98fd Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-11j2ashw-69d831de-tpgp Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (c5774ac) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C target-cpu=x86-64-v3 -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-11j2ashw-4803721b-kr8v Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (cffdb05) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (cffdb05) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (cffdb05) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (cffdb05) to 5cf1987 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 5cf1987a91
env:
BENCH_FILTER: ^decimal
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
run benchmark arithmetic_kernels |
|
🤖 Benchmark starting (GKE) | trigger Target: arithmetic_kernels Sharding factor: 1 (1 workers). Comparing vectorize-checked-decimal-arithmetic (f933cfa) to 362eb22 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 362eb2294d
env:
BENCH_FILTER: ^decimal(64_multiply|128_divide)
RUSTFLAGS: -C target-cpu=x86-64-v3 -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (f933cfa) to 362eb22 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 362eb2294d
env:
BENCH_FILTER: ^decimal(64_multiply|128_divide)
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-1jwtivje-76dcaf12-5hrm Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (f933cfa) to 362eb22 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 362eb2294d
env:
BENCH_FILTER: ^decimal(64_multiply|128_divide)
RUSTFLAGS: -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-1jwtivje-ad4e1533-frd7 Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (f933cfa) to 362eb22 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 362eb2294d
env:
BENCH_FILTER: ^decimal(64_multiply|128_divide)
RUSTFLAGS: -C target-cpu=x86-64-v3
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-1jwtivje-76dcaf12-2tws Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: arithmetic_kernels Comparing vectorize-checked-decimal-arithmetic (f933cfa) to 362eb22 diff Run configurationrun benchmark arithmetic_kernels
baseline:
ref: 362eb2294d
env:
BENCH_FILTER: ^decimal(64_multiply|128_divide)
RUSTFLAGS: -C target-cpu=x86-64-v3 -C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6
resources:
arch: amd64
shards: 1
DetailsPer-runner informationarithmetic_kernels — shard 1/1Node: gk3-benchmark-cluster-nap-1jwtivje-ad4e1533-dcrz Instance: c4-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arithmetic_kernelsCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
|
andygrove
left a comment
There was a problem hiding this comment.
Thanks @mbutrovich, this looks good to me. I checked the sign-bit overflow logic for add/sub, the widening multiplies, the masking of overflow in null slots (including offsets), and the fallback to the checked kernel, and they all look correct. I left a few non-blocking nits.
| c.bench_function(&format!("decimal64_add({null_density})"), |bench| { | ||
| bench.iter(|| hint::black_box(add(&a64, &b64).unwrap())) | ||
| }); | ||
| c.bench_function(&format!("decimal64_multiply({null_density})"), |bench| { |
There was a problem hiding this comment.
nit: The description says Decimal32 and Decimal64 add/sub now vectorize, but there's no Decimal32 benchmark and no decimal64_subtract. Could you add them? The Decimal32 path in particular (widening i32 to i64 for mul, sign-bit add/sub) has no coverage, so a slowdown there like the one you saw for i256 would go unnoticed.
| /// through a pointer. The checked `add` cases of the `arithmetic_kernels` | ||
| /// benchmark measure this. | ||
| #[inline(never)] | ||
| fn collect_overflowing<N>( |
There was a problem hiding this comment.
nit: Could you add numbers to the PR description comparing this to a safe version (e.g. Vec::with_capacity + push/extend with the same #[inline(never)] boundary)? Since the gain depends on LLVM's inlining choices for a given target, it would be good to have a record that the unsafe is what produces the gain.
| overflow |= overflowed; | ||
| written += 1; | ||
| } | ||
| debug_assert!(written <= values.capacity()); |
There was a problem hiding this comment.
nit: written <= values.capacity() can't fail, because the zip is bounded by spare_capacity_mut(). It doesn't check the set_len precondition either. Maybe drop it, or capture iter.len() up front and debug_assert_eq!(written, len), which at least catches an iterator that reports the wrong length.
sunchao
left a comment
There was a problem hiding this comment.
Thanks for the updates. The output reservation is fallible, Decimal256 uses the checked kernel again, and the tests now sweep overflow positions for the vectorized decimal widths. Fork CI passed the arrow-arith debug and release tests. I left two follow-ups on allocations introduced by the revised paths.
| ) -> Option<PrimitiveArray<T>> { | ||
| let valid_overflow = overflow | ||
| && nulls.as_ref().is_none_or(|n| { | ||
| let overflowed = BooleanBuffer::collect_bool(n.len(), overflowed); |
There was a problem hiding this comment.
Could the validity check avoid allocating two temporary bitmaps while holding the output? For nullable array–scalar arithmetic, the old kernel allocated the output and cloned the input validity buffer. If only a null slot overflows, the new check keeps the output but can panic if collect_bool cannot allocate its scratch bitmap. n.valid_indices().any(overflowed) would avoid both scratch buffers and short-circuit on the first valid overflow. If bit packing is faster, MutableBuffer::try_collect_bool and a word-by-word intersection would keep that loop and avoid the second allocation.
There was a problem hiding this comment.
Allocation failure is a failing case, and I'd rather not add code for it here. The scratch is 2 KiB for 8192 rows, and if it can't be allocated, the checked kernel this falls back to can't report that on nullable input either (try_binary, try_unary). valid_indices().any(..) is what this check was before, and it slowed down the common case it exists for: nullable UInt32 minus a scalar, where zero-filled null slots overflow, measured 0.70x on Xeon 8581C (run), against 1.17x with the bitmaps (run). test_checked_decimal_null_slot_overflow_sliced now covers a sliced validity buffer across several 64-row chunks.
| return None; | ||
| } | ||
| let (value, overflow) = array.value(0).mul_overflowing(mul); | ||
| (!overflow).then(|| PrimitiveArray::new(vec![value].into(), None)) |
There was a problem hiding this comment.
Could we pass the rescaled native value directly or make this temporary reservation fallible? With two valid scalars at different scales, such as 1.50 + 1.0000, the base binary path returns ArrowError::MemoryError if output allocation fails. The new vec![value] can abort first, before either the fast path's fallible reservation or the checked fallback. This also allocates when the other operand is an empty array and will never read the rescaled value.
There was a problem hiding this comment.
rescale_scalar now reserves fallibly and leaves the rescale to the rows if that fails, so 1.50 + 1.0000 reaches the fast path's fallible reservation. test_decimal_rescaled_scalar covers two scalars. Two literals rarely reach this kernel, because DataFusion's ConstEvaluator and Spark's ConstantFolding fold them at plan time, so I kept it to that.
Replace the BitAnd of the validity and overflow bitmaps with a shared, non-generic word-at-a-time intersection that stops at the first valid overflow, so the check allocates one bitmap instead of two.
|
I ran the
The new paths were reached. Of the decimal To rerun it, put each arrow-rs crate in arrow-pbt's ARROW_PBT_REASONS=1 cargo run -q -- run arith 1-18 10000 |
sunchao
left a comment
There was a problem hiding this comment.
Reviewed aee0225d3937ac7ee79b406c5b19eb76d9f94288 with five independent reviewers across correctness, resources, design/reuse, tests, and API/documentation. No new actionable findings.
Checked intermediate rescale overflow, scalar hoisting with empty/all-null inputs, result precision/scale, i128 multiplication boundaries, sliced validity, and checked fallback behavior. The temporary scalar reservation is now fallible, the validity intersection avoids a second bitmap, and Decimal256 retains the checked kernel. Existing bitmap/code-size and measured performance tradeoffs are already covered in the discussion.
The exact-head fork CI passed, including arrow-arith debug and optimized release tests; upstream Required Checks also passed. macOS/Windows were skipped, and this run does not independently establish the reported Miri validation. Published benchmark results identify earlier revisions. No local tests or benchmarks were run.
| criterion_group!(benches, add_benchmark); | ||
| /// Like `create_primitive_array_range`, but with its own `seed`, so that two | ||
| /// arrays hold different values and nulls. | ||
| fn seeded_array<T>( |
There was a problem hiding this comment.
seeded_array is create_primitive_array_range with a seed, and #12058 adds the same copy. Could it move to arrow/src/util/bench_util.rs as create_primitive_array_range_with_seed, next to create_primitive_array_with_seed? nit: a64 and b64 repeat the same expression, and a decimal64 = |seed| ... closure like decimal32 would remove the repetition.
There was a problem hiding this comment.
Could it move to
arrow/src/util/bench_util.rsascreate_primitive_array_range_with_seed, next tocreate_primitive_array_with_seed?
The copy here is identical to the one in #12058, so a trial merge of the two heads (git merge-tree fe005b260b aee0225d39) merges it cleanly into one. The file conflicts in the imports, in the new benchmark functions, and in criterion_group!, which the move doesn't change. The two PRs carry identical copies of what they share so that neither depends on the other (#12062 (comment) and #12058 (comment)). Only this file calls it, so I'd keep it here instead of adding a public function to arrow::util::bench_util.
nit:
a64andb64repeat the same expression, and adecimal64 = |seed| ...closure likedecimal32would remove the repetition.
Done.
| } | ||
|
|
||
| #[test] | ||
| fn test_checked_decimal_null_slot_overflow_sliced() { |
There was a problem hiding this comment.
After #12058 lands, this test runs the same overflowing_op and any_valid path as test_checked_null_slot_overflow_sliced there, because equal-scale Decimal64 add goes straight to overflowing_op. The only difference is i64 instead of i32. Could it be dropped when rebasing? The Kernel and Checked aliases would also collide, and test_checked_decimal_op and test_checked_integer_op are close enough that one helper taking a closure for the data type could serve both.
There was a problem hiding this comment.
Could it be dropped when rebasing?
I'd keep it. It's the only test that runs decimal_op on a sliced array whose null slots overflow. Until #12058 lands, it's also the only test in this PR of any_valid on a sliced validity buffer, which I added for #12062 (comment).
The
KernelandCheckedaliases would also collide
Yes. Whichever PR lands second drops its copy of the aliases along with its copy of the shared helpers, as in #12062 (comment) and #12058 (comment).
test_checked_decimal_opandtest_checked_integer_opare close enough that one helper taking a closure for the data type could serve both.
They differ in more than the data type. The decimal helper takes the two scales and checks each operation against the rescaled operands, and the integer helper also runs every pair without nulls. A shared helper would need both of those as parameters, so I'd keep them separate.
|
Thanks for the feedback, @comphead! |
|
let me know when this is good to merge. @andygrove are you waiting on anything in particular before merging? |
Nothing I'm waiting on. LGTM. |
…pache#12058) # Which issue does this PR close? N/A. # Rationale for this change Checked `add`, `sub` and `mul` on integers [call `try_binary` or `try_unary`](https://github.com/apache/arrow-rs/blob/b231cf15efa5dbbbe4ae8503849aa6ab3cfc4e0c/arrow-arith/src/numeric.rs#L337-L343), which return at the first overflow, so LLVM doesn't vectorize them. DataFusion calls them when `fail_on_overflow` is set ([`binary.rs`](https://github.com/apache/datafusion/blob/32bd81fb226ccf1ae5df67ff340d77baa3e73668/datafusion/physical-expr/src/expressions/binary.rs#L641-L651)), and Comet calls them for Spark's ANSI mode ([`checked_arithmetic.rs`](https://github.com/apache/datafusion-comet/blob/79b5ddd50ba2e3f605399d641e4c0b3be84c20ef/native/spark-expr/src/math_funcs/checked_arithmetic.rs#L146-L148)). # What changes are included in this PR? - Integer `add`, `sub` and `mul` compute the wrapping result and an overflow flag for every slot in one pass. - If a slot overflowed and the result has nulls, they recompute the overflow of every row into a bitmap and mask it with the validity bits. They rerun the existing checked kernel only if a valid row overflowed, so errors don't change. An overflow in a null slot, such as unsigned `0 - 1` in a slot that a builder filled with 0, doesn't rerun the batch. - Signed `add` and `sub` test sign bits, because the signed `overflowing_add` and `overflowing_sub` don't vectorize. - Signed `i64` `mul` uses `overflowing_mul`, while the narrower signed types multiply in the next wider type. Widened to `i128`, the `i64` multiply stayed a scalar `imul` while LLVM packed each row's overflow flag into vector registers, and it measured 0.65x with `x86-64-v3`. - The pass writes into the `Vec`'s spare capacity and calls `set_len` with the number of slots it wrote. With `collect`, LLVM sometimes left the loop inside `collect` out of line, where it loaded and stored the overflow flag on every row. apache#12062 measured that at 0.83x for rescaled `Decimal128` add on Neoverse-V2. apache#12062 has the same helper. - The output is reserved with `Vec::try_reserve_exact`, so an allocation failure returns `ArrowError::MemoryError`. - Null slots of the result hold the wrapping result instead of zero, as they already do for `add_wrapping`. - 48ca288 adds the benchmark cases, so the benchmark bot can compare against it. # Are these changes tested? New tests in `arrow-arith/src/numeric.rs` compare the kernels with the scalar checked operations at the edge values of every integer type. They run as arrays with and without nulls, as one-row slices, and with a scalar on either side. Another test moves one overflowing row through a 300-row batch, so that each lane of the vector loop and the scalar remainder hold it once. <img width="1600" height="3700" alt="checked-arith-bot-6035cda" src="https://github.com/user-attachments/assets/e381c473-b6d2-4056-8dd0-92d976e4ac15" /> The overflow cases are expected to be slower, because they pay for the new pass, the null-slot check, and then the checked kernel. With `fail_on_overflow` or ANSI mode, an overflow fails the query, so that cost is paid at most once per partition. A batch that overflows only in null slots pays for the pass and the null-slot check, and still beats `main` in the default-layout runs: `int32_add_checked_null_slot_overflow` measured 2.67x on Neoverse-V2, 1.36x on Xeon 8581C, and 2.76x with `x86-64-v3`. The lowest is `uint64_add_checked_null_slot_overflow` on Xeon 8581C, at 1.03x default and 0.86x aligned. The extra pass also costs code size: 132 KiB for `integer_op` and the new helpers across the eight integer types, against 18.5 KiB before, in the thin-LTO bench binary on aarch64. The checked kernels that the fallback calls are unchanged. Bot runs: Neoverse-V2 [default](apache#12058 (comment)) and [aligned](apache#12058 (comment)), Xeon 8581C [default](apache#12058 (comment)) and [aligned](apache#12058 (comment)), and Xeon 8581C with `-C target-cpu=x86-64-v3` [default](apache#12058 (comment)) and [aligned](apache#12058 (comment)). Those runs measured 8aa2750. 6035cda changes only the `i64` multiply, and its `Int64` runs are: Neoverse-V2 [default](apache#12058 (comment)) and [aligned](apache#12058 (comment)), Xeon 8581C [default](apache#12058 (comment)) and [aligned](apache#12058 (comment)), and `x86-64-v3` [default](apache#12058 (comment)) and [aligned](apache#12058 (comment)). # Are there any significant user-facing changes? No. ## AI Disclosure I wrote this PR with help from Claude but understand and support the core approach, implementation, and test coverage.
Which issue does this PR close?
N/A.
Rationale for this change
decimal_opruns decimaladd,subandmul, includingadd_wrapping,sub_wrappingandmul_wrapping, throughtry_op!with the checked operations, which return at the first overflow. DataFusion callsadd_wrappingfor+(binary.rs), so its default decimal arithmetic runs one row at a time. #12058 does the same change for integers.What changes are included in this PR?
add,subandmulcompute the wrapping result and an overflow flag for every slot in one pass, with equal scales and when one side is rescaled.Decimal32andDecimal64addandsubvectorize.Decimal128stays scalar and loses the per-row branch and the operand spills of the error path.i128mulmultiplies ini64when both operands fit.Decimal64mulusesoverflowing_mul, as in perf(arrow-arith): vectorize checked integeradd,subandmul#12058.Decimal256keeps the checked kernel. With-C target-cpu=x86-64-v3, LLVM vectorizes the one-passi256add loop two rows at a time, packing the carries into vector registers with about 20 stack spills per iteration, and it measured 0.75x (perf(arrow-arith): vectorize checked decimaladd,subandmul#12062 (comment)).OverflowingOp,overflowing_opandcollect_overflowingare the same as in perf(arrow-arith): vectorize checked integeradd,subandmul#12058, so whichever PR lands second drops its copy.collect_overflowingwrites into theVec's spare capacity and callsset_lenwith the number of slots it wrote. Withcollect, LLVM sometimes left the loop insidecollectout of line, where it loaded and stored the overflow flag on every row. Which instances it left out of line changed with the closure and the target: on generic aarch64 andneoverse-n1it was rescaledDecimal128add, which the bot measured at 0.83x on Neoverse-V2. Writing the values in our own loop withoutunsafecost 30% to 40% with a zero-filledVec, lost vectorization withpush, and was slower in a two-pass,unzipor chunked form.Vec::try_reserve_exact, so an allocation failure returnsArrowError::MemoryError.add_wrapping.Are these changes tested?
New tests in
arrow-arith/src/numeric.rscompare the kernels with the scalar checked operations at the edge values of every decimal type, with equal scales and with either side rescaled, as arrays, as one-row slices, and with a scalar on either side. They also coverDecimal128mulon both sides of thei64bounds. Another test moves one overflowing row through a 100-row batch of every decimal type, including rows where only the rescale overflows. A third checks a rescaled scalar, including one whose rescale overflows, against valid, all-null, and empty arrays. Miri passes these tests, except the position test, which checks vectorized code that Miri doesn't run.The overflow cases are expected to be slower, because they pay for the new pass, the null-slot check, and then the checked kernel. With
fail_on_overflowor ANSI mode, an overflow fails the query, so that cost is paid at most once per partition. A batch that overflows only in null slots pays for the pass and the null-slot check:decimal128_add_null_slot_overflowmeasured 0.88x on Neoverse-V2 and 0.86x on Xeon 8581C, becauseDecimal128doesn't vectorize, and 1.45x withx86-64-v3.The extra pass also costs code size: 161 KiB for
decimal_opand the new helpers across the four decimal types, against 30 KiB before, in the thin-LTO bench binary on aarch64. The checked kernels that the fallback calls are unchanged.decimal128_multiply_wide(0.1), where half the values don't fit ini64, measured 0.89x on Neoverse-V2. On that input thei64branch ini128mulgoes either way at random, and the one pass also multiplies the null slots that the checked kernel skips.Bot runs: Neoverse-V2 default and aligned, Xeon 8581C default and aligned, and Xeon 8581C with
-C target-cpu=x86-64-v3default and aligned. Those runs measured 41647fd. f933cfa changes only thei64multiply, and itsDecimal64mulruns are: Neoverse-V2 default and aligned, Xeon 8581C default and aligned, andx86-64-v3default and aligned.Are there any significant user-facing changes?
No.
AI Disclosure
I wrote this PR with help from Claude but understand and support the core approach, implementation, and test coverage.