mbutrovich commented on PR #12058:
URL: https://github.com/apache/arrow-rs/pull/12058#issuecomment-6864283786

   Thanks @andygrove and @sunchao!
   
   > Nobody should be reading null slots, so this is fine, but please mention 
the behaviour change in the PR description.
   
   The description says it under "What changes are included in this PR?": "Null 
slots of the result hold the wrapping result instead of zero, as they already 
do for `add_wrapping`."
   
   > Could the overflow flag be masked with the validity bitmap before deciding 
to fall back?
   
   Yes. After a pass that flagged an overflow, `overflowing_op` masks the 
overflow of every row with the validity bits and falls back only if a valid row 
overflowed. That is the same change as in 
https://github.com/apache/arrow-rs/pull/12058#discussion_r4565570175. Masking 
inside the loop would add work to every batch, so the mask is computed only 
after a flagged pass. `int32_add_checked_null_slot_overflow` measured 2.67x on 
[Neoverse-V2](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6863769659),
 1.36x on [Xeon 
8581C](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6863835999), 
and 2.76x with 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6864013103).
 `int64_add_checked_null_slot_overflow` measured 2.29x, 1.33x, and 2.07x.
   
   > Could you share benchmark numbers for them, or leave them on `try_op!`?
   
   Unsigned `mul` gains in every configuration. Without and with 10% nulls, 
`uint32_multiply_checked` measured 1.52x and 1.80x on 
[Neoverse-V2](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6863769659),
 1.86x and 2.33x on [Xeon 
8581C](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6863835999), 
and 1.65x and 1.90x with 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12058#issuecomment-6864013103).
 `uint64_multiply_checked` measured 1.27x and 1.97x, 1.84x and 2.30x, and 1.45x 
and 2.29x. The multiply stays scalar for both, so the gain comes from dropping 
the early return and the walk over valid indices.
   
   `Int64` `mul` is the exception. It gains on Neoverse-V2 (1.21x and 1.93x 
without and with nulls) and is flat on Xeon 8581C (0.95x and 1.12x). With 
`x86-64-v3` it is slower: this PR takes 9.5 to 10.6 us across runs, while 
`main` takes 6.0 to 9.6 us, so between 0.65x and 0.90x without nulls. The same 
runs measure unsigned `u64` `mul` at 4.2 us, so the cost is in how the signed 
`i64` multiply compiles there. I'll fix it or move signed 64-bit `mul` back to 
`try_op!` in this PR.
   
   On code size, `integer_op` and the new helpers now take 135 KiB across the 
eight integer types, against 18.5 KiB on `main`, in the thin-LTO bench binary 
on aarch64. The error for a failed allocation is built in one shared `#[cold]` 
function, as #12080 does for `mul_checked` in decimal `div` and `rem`. That 
saved 16 KiB over formatting it in each instance.
   
   > Can it reuse the existing dispatch, or at least match it?
   
   It matches it. `PrimitiveArray` doesn't override 
[`Array::logical_nulls`](https://github.com/apache/arrow-rs/blob/9c9486188744e80ec86c3b2bcdd649b89945ffe5/arrow-array/src/array/mod.rs#L233-L235),
 so `nulls()` and `logical_nulls()` return the same buffer. For empty input, 
both paths return an empty `T::DATA_TYPE` array without nulls. It can't go 
through `binary` or `unary`, because their closures would have to carry the 
overflow flag through a capture. That keeps the flag in memory and the loop 
scalar, which is why `collect_overflowing` exists.
   
   > Checked arithmetic on durations and timestamps, and direct `try_binary(.., 
add_checked)` callers, don't benefit.
   
   I filed #12241 for durations, which go through 
[`duration_op`](https://github.com/apache/arrow-rs/blob/9c9486188744e80ec86c3b2bcdd649b89945ffe5/arrow-arith/src/numeric.rs#L983-L1001)
 with `try_op_ref!`. A public helper in `arity.rs` would become permanent API, 
so I'd add one when a caller outside `numeric.rs` needs it.
   
   > A short comment explaining that, and pointing to the benchmark that would 
catch a regression, would help future readers.
   
   The comment on `collect_overflowing` now names the checked `add` cases of 
`arithmetic_kernels`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to