This is an automated email from the ASF dual-hosted git repository.
alamb 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 1db1053901 Remove some `unsafe` and allocations when creating
PrimitiveArrays from Vec and `from_trusted_len_iter` (#9299)
1db1053901 is described below
commit 1db105390144427604b5713a07e7ca8a745839b5
Author: Andrew Lamb <[email protected]>
AuthorDate: Fri Jan 30 08:18:15 2026 -0500
Remove some `unsafe` and allocations when creating PrimitiveArrays from Vec
and `from_trusted_len_iter` (#9299)
# Which issue does this PR close?
- part of https://github.com/apache/arrow-rs/issues/9298
# Rationale for this change
While reviewing https://github.com/apache/arrow-rs/pull/9294 from
@Dandandan I noticed some other places where we can avoid making
ArrayData and thus save some allocations (and `unsafe`)
I don't expect this to make a huge performance difference, but every
little allocation helps, and I think the change is justified simply from
the perspective of avoiding some more `unsafe`
# What changes are included in this PR?
Construct primitive arrays directly
# Are these changes tested?
By existing CI
# Are there any user-facing changes?
<!--
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-array/src/array/primitive_array.rs | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/arrow-array/src/array/primitive_array.rs
b/arrow-array/src/array/primitive_array.rs
index 186bfca2d4..29189b450a 100644
--- a/arrow-array/src/array/primitive_array.rs
+++ b/arrow-array/src/array/primitive_array.rs
@@ -25,7 +25,9 @@ use crate::timezone::Tz;
use crate::trusted_len::trusted_len_unzip;
use crate::types::*;
use crate::{Array, ArrayAccessor, ArrayRef, Scalar};
-use arrow_buffer::{ArrowNativeType, Buffer, NullBuffer, NullBufferBuilder,
ScalarBuffer, i256};
+use arrow_buffer::{
+ ArrowNativeType, BooleanBuffer, Buffer, NullBuffer, NullBufferBuilder,
ScalarBuffer, i256,
+};
use arrow_data::bit_iterator::try_for_each_valid_idx;
use arrow_data::{ArrayData, ArrayDataBuilder};
use arrow_schema::{ArrowError, DataType};
@@ -1488,10 +1490,9 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
let (null, buffer) = unsafe { trusted_len_unzip(iterator) };
- let data = unsafe {
- ArrayData::new_unchecked(T::DATA_TYPE, len, None, Some(null), 0,
vec![buffer], vec![])
- };
- PrimitiveArray::from(data)
+ let nulls =
+ Some(NullBuffer::new(BooleanBuffer::new(null, 0, len))).filter(|n|
n.null_count() > 0);
+ PrimitiveArray::new(ScalarBuffer::from(buffer), nulls)
}
}
@@ -1502,11 +1503,9 @@ macro_rules! def_numeric_from_vec {
( $ty:ident ) => {
impl From<Vec<<$ty as ArrowPrimitiveType>::Native>> for
PrimitiveArray<$ty> {
fn from(data: Vec<<$ty as ArrowPrimitiveType>::Native>) -> Self {
- let array_data = ArrayData::builder($ty::DATA_TYPE)
- .len(data.len())
- .add_buffer(Buffer::from_vec(data));
- let array_data = unsafe { array_data.build_unchecked() };
- PrimitiveArray::from(array_data)
+ let buffer = ScalarBuffer::from(Buffer::from_vec(data));
+ let nulls = None;
+ PrimitiveArray::new(buffer, nulls)
}
}