Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Reduce how much code is instantiated for comparisons #2365

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 34 additions & 18 deletions arrow/src/array/array_boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,46 @@ impl<'a> BooleanArray {
}
}

fn new_with_size_hint(
(_, data_len): (usize, Option<usize>),
) -> (usize, MutableBuffer, MutableBuffer) {
let data_len = data_len.expect("Iterator must be sized"); // panic if no upper bound.

let num_bytes = bit_util::ceil(data_len, 8);
(
data_len,
MutableBuffer::from_len_zeroed(num_bytes),
MutableBuffer::from_len_zeroed(num_bytes),
)
}

unsafe fn boolean_array_from_builders(
data_len: usize,
null_builder: MutableBuffer,
val_builder: MutableBuffer,
) -> BooleanArray {
let data = ArrayData::new_unchecked(
DataType::Boolean,
data_len,
None,
Some(null_builder.into()),
0,
vec![val_builder.into()],
vec![],
);
BooleanArray::from(data)
}

impl<Ptr: Borrow<Option<bool>>> FromIterator<Ptr> for BooleanArray {
fn from_iter<I: IntoIterator<Item = Ptr>>(iter: I) -> Self {
let iter = iter.into_iter();
let (_, data_len) = iter.size_hint();
let data_len = data_len.expect("Iterator must be sized"); // panic if no upper bound.

let num_bytes = bit_util::ceil(data_len, 8);
let mut null_builder = MutableBuffer::from_len_zeroed(num_bytes);
let mut val_builder = MutableBuffer::from_len_zeroed(num_bytes);
let (data_len, mut null_builder, mut val_builder) =
new_with_size_hint(iter.size_hint());

let data = val_builder.as_slice_mut();

let null_slice = null_builder.as_slice_mut();

iter.enumerate().for_each(|(i, item)| {
if let Some(a) = item.borrow() {
bit_util::set_bit(null_slice, i);
Expand All @@ -255,18 +282,7 @@ impl<Ptr: Borrow<Option<bool>>> FromIterator<Ptr> for BooleanArray {
}
});

let data = unsafe {
ArrayData::new_unchecked(
DataType::Boolean,
data_len,
None,
Some(null_builder.into()),
0,
vec![val_builder.into()],
vec![],
)
};
BooleanArray::from(data)
unsafe { boolean_array_from_builders(data_len, null_builder, val_builder) }
}
}

Expand Down
53 changes: 52 additions & 1 deletion arrow/src/array/array_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
/// Creates a PrimitiveArray based on a constant value with `count` elements
pub fn from_value(value: T::Native, count: usize) -> Self {
// # Safety: iterator (0..count) correctly reports its length
let val_buf = unsafe { Buffer::from_trusted_len_iter((0..count).map(|_| value)) };
let val_buf = unsafe {
Buffer::from_trusted_len_iter(std::iter::repeat(value).take(count))
};
let data = unsafe {
ArrayData::new_unchecked(
T::DATA_TYPE,
Expand Down Expand Up @@ -167,6 +169,16 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
) -> impl Iterator<Item = Option<T::Native>> + 'a {
indexes.map(|opt_index| opt_index.map(|index| self.value_unchecked(index)))
}

pub(crate) fn as_array_view(&self) -> NativeArrayView<'_, T::Native> {
// Safety: The constructed `RawPtrBox` will not outlive `&self.data`
unsafe {
NativeArrayView {
data: &self.data,
raw_values: RawPtrBox::new(self.raw_values.as_ptr() as *const u8),
}
}
}
}

impl<T: ArrowPrimitiveType> From<PrimitiveArray<T>> for ArrayData {
Expand Down Expand Up @@ -201,6 +213,45 @@ impl<'a, T: ArrowPrimitiveType> ArrayAccessor for &'a PrimitiveArray<T> {
}
}

pub(crate) struct NativeArrayView<'a, T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we perhaps find some way to eliminate this part, in a trade-off between more unsafe and getting LLVM to do more work, I would always take the latter...

/// Underlying ArrayData
/// # Safety
/// must have exactly one buffer, aligned to type T
data: &'a ArrayData,
/// Pointer to the value array. The lifetime of this must be <= to the value buffer
/// stored in `data`, so it's safe to store.
/// # Safety
/// raw_values must have a value equivalent to `data.buffers()[0].raw_data()`
/// raw_values must have alignment for type T::NativeType
raw_values: RawPtrBox<T>,
}

impl<'a, T> NativeArrayView<'a, T>
where
T: Copy,
{
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> T {
let offset = i + self.data.offset();
*self.raw_values.as_ptr().add(offset)
}

pub unsafe fn take_iter_unchecked<'b>(
&'b self,
indexes: impl Iterator<Item = Option<usize>> + 'b,
) -> impl Iterator<Item = Option<T>> + 'b {
indexes.map(|opt_index| opt_index.map(|index| self.value_unchecked(index)))
}

pub unsafe fn cast<U>(self) -> NativeArrayView<'a, U> {
assert_eq!(mem::size_of::<U>(), mem::size_of::<T>());
NativeArrayView {
data: self.data,
raw_values: RawPtrBox::new(self.raw_values.as_ptr() as *const u8),
}
}
}

fn as_datetime<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveDateTime> {
match T::DATA_TYPE {
DataType::Date32 => Some(temporal_conversions::date32_to_datetime(v as i32)),
Expand Down