viirya commented on code in PR #2740:
URL: https://github.com/apache/arrow-rs/pull/2740#discussion_r974453747
##########
arrow/src/compute/kernels/arity.rs:
##########
@@ -298,36 +298,52 @@ where
let len = a.len();
if a.null_count() == 0 && b.null_count() == 0 {
- let values = a.values().iter().zip(b.values()).map(|(l, r)| op(*l,
*r));
- let buffer = unsafe { Buffer::try_from_trusted_len_iter(values) }?;
- // JUSTIFICATION
- // Benefit
- // ~75% speedup
- // Soundness
- // `values` is an iterator with a known size from a PrimitiveArray
- return Ok(unsafe { build_primitive_array(len, buffer, 0, None) });
+ try_binary_no_nulls(len, a, b, op)
+ } else {
+ let null_buffer = combine_option_bitmap(&[a.data(), b.data()],
len).unwrap();
+
+ let null_count = null_buffer
+ .as_ref()
+ .map(|x| len - x.count_set_bits())
+ .unwrap_or_default();
+
+ let mut buffer = BufferBuilder::<O::Native>::new(len);
+ buffer.append_n_zeroed(len);
+ let slice = buffer.as_slice_mut();
+
+ try_for_each_valid_idx(len, 0, null_count, null_buffer.as_deref(),
|idx| {
+ unsafe {
+ *slice.get_unchecked_mut(idx) =
+ op(a.value_unchecked(idx), b.value_unchecked(idx))?
+ };
+ Ok::<_, ArrowError>(())
+ })?;
+
+ Ok(unsafe {
+ build_primitive_array(len, buffer.finish(), null_count,
null_buffer)
+ })
}
+}
- let null_buffer = combine_option_bitmap(&[a.data(), b.data()],
len).unwrap();
-
- let null_count = null_buffer
- .as_ref()
- .map(|x| len - x.count_set_bits())
- .unwrap_or_default();
-
- let mut buffer = BufferBuilder::<O::Native>::new(len);
- buffer.append_n_zeroed(len);
- let slice = buffer.as_slice_mut();
-
- try_for_each_valid_idx(len, 0, null_count, null_buffer.as_deref(), |idx| {
+/// This intentional inline(never) attribute helps LLVM optimize the loop.
+#[inline(never)]
+fn try_binary_no_nulls<A: ArrayAccessor, B: ArrayAccessor, F, O>(
+ len: usize,
+ a: A,
+ b: B,
+ op: F,
+) -> Result<PrimitiveArray<O>>
+where
+ O: ArrowPrimitiveType,
+ F: Fn(A::Item, B::Item) -> Result<O::Native>,
+{
+ let mut buffer = MutableBuffer::new(len);
Review Comment:
Missed it. Thanks for catching it.
--
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]