etseidl commented on code in PR #6252:
URL: https://github.com/apache/arrow-rs/pull/6252#discussion_r1723674743
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -1016,6 +1016,32 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
PrimitiveArray::new(values, Some(nulls))
}
+ /// Applies a unary infallible function to each value in an array,
producing a
+ /// new primitive array.
+ ///
+ /// # Null Handling
+ ///
+ /// Applies the function for all values, including those on null slots.
This
+ /// will often allow the compiler to generate faster vectorized code, but
+ /// requires that the operation must be infallible (not error/panic) for
any
+ /// value of the corresponding type or this function may panic.
+ pub fn from_unary<U: ArrayAccessor, F>(left: U, mut op: F) -> Self
+ where
+ F: FnMut(U::Item) -> T::Native,
+ {
+ let nulls = left.logical_nulls();
+ let mut values: Vec<T::Native> = vec![T::Native::default();
left.len()];
+
+ for (i, val) in values.iter_mut().enumerate().take(left.len()) {
Review Comment:
On my workstation that caused a 10-20% regression for fixed_len_byte_arrays.
:(
How about the following (which avoids the vector initialization):
```rust
let mut values: Vec<T::Native> = Vec::with_capacity(left.len());
unsafe {
// SAFETY: setting len to capacity, and then iterating over
range 0..len
values.set_len(left.len());
for (i, val) in values.iter_mut().enumerate().take(left.len()) {
*val = op(left.value_unchecked(i));
}
}
```
--
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]