Skip to content

Commit

Permalink
rework from_unary to skip vector initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
etseidl committed Aug 20, 2024
1 parent c388d30 commit cf625bf
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,16 +1030,14 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
F: FnMut(U::Item) -> T::Native,
{
let nulls = left.logical_nulls();
let mut values: Vec<T::Native> = vec![T::Native::default(); left.len()];
let buffer = unsafe {
// SAFETY: i in range 0..left.len()
let iter = (0..left.len()).map(|i| op(left.value_unchecked(i)));
// SAFETY: upper bound is trusted because `iter` is over a range
Buffer::from_trusted_len_iter(iter)
};

for (i, val) in values.iter_mut().enumerate().take(left.len()) {
// SAFETY: i in range 0..len
unsafe {
*val = op(left.value_unchecked(i));
}
}
let values = ScalarBuffer::from(values);
Self::new(values, nulls)
PrimitiveArray::new(buffer.into(), nulls)
}

/// Returns a `PrimitiveBuilder` for this array, suitable for mutating values
Expand Down

0 comments on commit cf625bf

Please sign in to comment.