This is an automated email from the ASF dual-hosted git repository.
Jefffrey pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new e6a059d714 perf : optimize take(fixed_sized_list) (#10441)
e6a059d714 is described below
commit e6a059d7143a7fb8a86ecf7e694391f1fdc37c31
Author: RIchard Baah <[email protected]>
AuthorDate: Thu Aug 27 22:22:57 2026 -0400
perf : optimize take(fixed_sized_list) (#10441)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- works towards #8879? nothing specific.
- this is currently blocked by #10442. After that is merged into main we
can compare
- context
[https://github.com/apache/arrow-rs/pull/10441#issuecomment-5396969816]
# Rationale for this change
`take_fixed_size_list` with a primitive child and no child nulls was
allocating a full index array and dispatching per-element. This replaces
that with a direct `memcopy` of row-sized byte blocks.
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
# What changes are included in this PR?
Adds a fast path in `take_fixed_size_list` that fires when the child
array is a primitive type with no nulls. Instead of constructing a
UInt32Array of element indices and calling `take_impl`, it copies whole
fixed-size rows as raw bytes via `copy_from_slice`.
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
# Are these changes tested?
Yes, existing test_take_fixed_size_list cases cover the fast path.
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->
# Are there any user-facing changes?
no
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
---
arrow-select/src/take.rs | 123 +++++++++++++++++++++++++++++++++++++++--------
1 file changed, 102 insertions(+), 21 deletions(-)
diff --git a/arrow-select/src/take.rs b/arrow-select/src/take.rs
index 9ddbf38083..9991e4bec2 100644
--- a/arrow-select/src/take.rs
+++ b/arrow-select/src/take.rs
@@ -30,7 +30,7 @@ use arrow_buffer::{
ScalarBuffer, bit_util,
};
use arrow_cmp::make_comparator;
-use arrow_data::transform::MutableArrayData;
+use arrow_data::{ArrayData, transform::MutableArrayData};
use arrow_schema::{ArrowError, DataType, FieldRef, SortOptions, UnionMode};
use num_traits::Zero;
@@ -766,31 +766,82 @@ fn take_fixed_size_list<IndexType: ArrowPrimitiveType,
const CHECKED: bool>(
indices: &PrimitiveArray<IndexType>,
length: <UInt32Type as ArrowPrimitiveType>::Native,
) -> Result<FixedSizeListArray, ArrowError> {
- let list_indices = take_value_indices_from_fixed_size_list(values,
indices, length)?;
- let taken = take_impl::<UInt32Type, CHECKED>(values.values().as_ref(),
&list_indices)?;
+ let field = values.value_field();
+ let child = values.values();
+ let nulls = take_nulls::<_, CHECKED>(values.nulls(), indices);
+
+ // Fast path: primitive child with no nulls copy row-sized byte blocks
directly,
+ let taken_child = if child.null_count() == 0
+ && let Some(element_size) = child.data_type().primitive_width()
+ {
+ take_fixed_size_list_primitive(
+ child,
+ indices,
+ length as usize,
+ element_size,
+ nulls.as_ref(),
+ )
+ } else {
+ let list_indices = take_value_indices_from_fixed_size_list(values,
indices, length)?;
+ take_impl::<UInt32Type, CHECKED>(child.as_ref(), &list_indices)?
+ };
- // determine null count and null buffer, which are a function of `values`
and `indices`
- let num_bytes = bit_util::ceil(indices.len(), 8);
- let mut null_buf = MutableBuffer::new(num_bytes).with_bitset(num_bytes,
true);
- let null_slice = null_buf.as_slice_mut();
+ FixedSizeListArray::try_new(field.clone(), length as i32, taken_child,
nulls)
+}
- for i in 0..indices.len() {
- let index = indices
- .value(i)
- .to_usize()
- .ok_or_else(|| ArrowError::ComputeError("Cast to usize
failed".to_string()))?;
- if !indices.is_valid(i) || values.is_null(index) {
- bit_util::unset_bit(null_slice, i);
+#[inline(never)]
+fn take_fixed_size_list_primitive<IndexType: ArrowPrimitiveType>(
+ child: &ArrayRef,
+ indices: &PrimitiveArray<IndexType>,
+ list_size: usize,
+ element_size: usize,
+ taken_row_nulls: Option<&NullBuffer>,
+) -> ArrayRef {
+ let row_bytes = list_size * element_size;
+ let child_data = child.to_data();
+ let src = child_data.buffers()[0].as_slice();
+ let child_byte_offset = child_data.offset() * element_size;
+
+ debug_assert!(
+ indices.len().checked_mul(row_bytes).is_some(),
+ "take_fixed_size_list_primitive: output buffer size overflows usize"
+ );
+ let out_len = indices.len() * list_size;
+
+ let mut out = MutableBuffer::from_len_zeroed(indices.len() * row_bytes);
+ let out_slice = out.as_slice_mut();
+
+ if indices.null_count() == 0 {
+ for (out_row, index) in indices.values().iter().enumerate() {
+ let src_start = child_byte_offset + index.as_usize() * row_bytes;
+ out_slice[out_row * row_bytes..(out_row + 1) * row_bytes]
+ .copy_from_slice(&src[src_start..src_start + row_bytes]);
+ }
+ } else {
+ for (out_row, index) in indices.values().iter().enumerate() {
+ if indices.is_valid(out_row) {
+ let src_start = child_byte_offset + index.as_usize() *
row_bytes;
+ out_slice[out_row * row_bytes..(out_row + 1) * row_bytes]
+ .copy_from_slice(&src[src_start..src_start + row_bytes]);
+ }
}
}
-
- let field = match values.data_type() {
- DataType::FixedSizeList(field, _) => field.clone(),
- d => unreachable!("take_fixed_size_list called with
non-fixed-size-list data type {d}"),
+ // Expand the per-row null bitmap to per-element: each null row produces
`list_size` null elements.
+ let child_null_buf = taken_row_nulls.map(|n|
n.expand(list_size).buffer().clone());
+
+ // SAFETY:
+ // - The data buffer has `indices.len() * row_bytes` bytes = `out_len`
elements of a
+ // primitive type, matching `.len(out_len)`.
+ // - The null buffer (when present) is `ceil(out_len, 8)` bytes via
`expand(list_size).buffer()`.
+ // - Primitive types have exactly one value buffer.
+ let array_data = unsafe {
+ ArrayData::builder(child.data_type().clone())
+ .len(out_len)
+ .add_buffer(out.into())
+ .null_bit_buffer(child_null_buf)
+ .build_unchecked()
};
- let nulls = NullBuffer::from_unsliced_buffer(null_buf, indices.len());
-
- FixedSizeListArray::try_new(field, length as i32, taken, nulls)
+ make_array(array_data)
}
/// The take kernel implementation for `FixedSizeBinaryArray`.
@@ -3033,4 +3084,34 @@ mod tests {
]
);
}
+
+ // parent null rows on FixedSizeList must propagate to child elements.
+ #[test]
+ fn test_take_fixed_size_list_parent_nulls() {
+ let list = FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
+ vec![
+ Some(vec![Some(1), Some(2)]),
+ None,
+ Some(vec![Some(5), Some(6)]),
+ ],
+ 2,
+ );
+
+ let indices = UInt32Array::from(vec![2, 1, 0]);
+ let result = take(&list, &indices, None).unwrap();
+ let result = result.as_fixed_size_list();
+
+ assert_eq!(result.len(), 3);
+ assert!(result.is_valid(0));
+ assert!(result.is_null(1));
+ assert!(result.is_valid(2));
+
+ let child = result.values().as_primitive::<Int32Type>();
+ assert_eq!(child.value(0), 5);
+ assert_eq!(child.value(1), 6);
+ assert!(child.is_null(2));
+ assert!(child.is_null(3));
+ assert_eq!(child.value(4), 1);
+ assert_eq!(child.value(5), 2);
+ }
}