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 d13e46aec8 Speed up `collect_bool` and remove `unsafe`, optimize
`take_bits`, `take_native` for null values (#8849)
d13e46aec8 is described below
commit d13e46aec85951f9e808b5c1d8a4c0c6cd320e6e
Author: Daniël Heres <[email protected]>
AuthorDate: Mon Nov 17 22:55:55 2025 +0100
Speed up `collect_bool` and remove `unsafe`, optimize `take_bits`,
`take_native` for null values (#8849)
# 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.
- Closes #8848
# Rationale for this change
* Some more performance
* Remove `unsafe`
```
take bool 512 time: [450.90 ns 455.52 ns 463.35 ns]
change: [−13.852% −12.345% −10.584%] (p = 0.00 <
0.05)
Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
2 (2.00%) low mild
1 (1.00%) high mild
4 (4.00%) high severe
take bool 1024 time: [810.17 ns 825.99 ns 857.14 ns]
change: [−7.4751% −5.4268% −2.8132%] (p = 0.00 <
0.05)
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
5 (5.00%) high mild
7 (7.00%) high severe
take bool null indices 1024
time: [963.37 ns 967.58 ns 973.34 ns]
change: [−5.1713% −4.3630% −3.5176%] (p = 0.00 <
0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high severe
take bool null values 1024
time: [1.5872 µs 1.5936 µs 1.6034 µs]
change: [−8.4184% −7.6316% −6.9030%] (p = 0.00 <
0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
2 (2.00%) low mild
1 (1.00%) high mild
2 (2.00%) high severe
take bool null values null indices 1024
time: [1.9172 µs 1.9262 µs 1.9403 µs]
change: [−4.4966% −3.9076% −3.3188%] (p = 0.00 <
0.05)
Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) high mild
3 (3.00%) high severe
take i32 null indices 1024
time: [539.55 ns 541.72 ns 545.28 ns]
change: [−6.8747% −6.5190% −6.0826%] (p = 0.00 <
0.05)
Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) low mild
1 (1.00%) high mild
2 (2.00%) high severe
take i32 null values 1024
time: [1.2699 µs 1.2750 µs 1.2823 µs]
change: [−4.8767% −4.4049% −3.8817%] (p = 0.00 <
0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
2 (2.00%) high mild
3 (3.00%) high severe
take i32 null values null indices 1024
time: [1.4711 µs 1.4781 µs 1.4874 µs]
change: [−6.3342% −4.7819% −3.6249%] (p = 0.00 <
0.05)
Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
2 (2.00%) high mild
5 (5.00%) high severe
```
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?
Changes to use `Vec` API (extend, push) rather than `MutableBuffer` and
unsafe code.
# Are these changes tested?
Existing tests
# Are there any user-facing changes?
No
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
arrow-buffer/src/buffer/mutable.rs | 13 ++++++-------
arrow-ord/src/cmp.rs | 16 +++++++---------
arrow-select/src/take.rs | 13 ++++++++-----
3 files changed, 21 insertions(+), 21 deletions(-)
diff --git a/arrow-buffer/src/buffer/mutable.rs
b/arrow-buffer/src/buffer/mutable.rs
index ff3bf9e432..83c4d87834 100644
--- a/arrow-buffer/src/buffer/mutable.rs
+++ b/arrow-buffer/src/buffer/mutable.rs
@@ -557,20 +557,19 @@ impl MutableBuffer {
/// as it eliminates the conditional `Iterator::next`
#[inline]
pub fn collect_bool<F: FnMut(usize) -> bool>(len: usize, mut f: F) -> Self
{
- let mut buffer = Self::new(bit_util::ceil(len, 64) * 8);
+ let mut buffer: Vec<u64> = Vec::with_capacity(bit_util::ceil(len, 64));
let chunks = len / 64;
let remainder = len % 64;
- for chunk in 0..chunks {
+ buffer.extend((0..chunks).map(|chunk| {
let mut packed = 0;
for bit_idx in 0..64 {
let i = bit_idx + chunk * 64;
packed |= (f(i) as u64) << bit_idx;
}
- // SAFETY: Already allocated sufficient capacity
- unsafe { buffer.push_unchecked(packed) }
- }
+ packed
+ }));
if remainder != 0 {
let mut packed = 0;
@@ -579,10 +578,10 @@ impl MutableBuffer {
packed |= (f(i) as u64) << bit_idx;
}
- // SAFETY: Already allocated sufficient capacity
- unsafe { buffer.push_unchecked(packed) }
+ buffer.push(packed)
}
+ let mut buffer: MutableBuffer = buffer.into();
buffer.truncate(bit_util::ceil(len, 8));
buffer
}
diff --git a/arrow-ord/src/cmp.rs b/arrow-ord/src/cmp.rs
index e90e782de1..30943ede4a 100644
--- a/arrow-ord/src/cmp.rs
+++ b/arrow-ord/src/cmp.rs
@@ -30,7 +30,7 @@ use arrow_array::{
GenericByteArray, GenericByteViewArray, downcast_primitive_array,
};
use arrow_buffer::bit_util::ceil;
-use arrow_buffer::{BooleanBuffer, MutableBuffer, NullBuffer};
+use arrow_buffer::{BooleanBuffer, NullBuffer};
use arrow_schema::ArrowError;
use arrow_select::take::take;
use std::cmp::Ordering;
@@ -390,14 +390,14 @@ fn take_bits(v: &dyn AnyDictionaryArray, buffer:
BooleanBuffer) -> BooleanBuffer
/// Invokes `f` with values `0..len` collecting the boolean results into a new
`BooleanBuffer`
///
-/// This is similar to [`MutableBuffer::collect_bool`] but with
+/// This is similar to [`arrow_buffer::MutableBuffer::collect_bool`] but with
/// the option to efficiently negate the result
fn collect_bool(len: usize, neg: bool, f: impl Fn(usize) -> bool) ->
BooleanBuffer {
- let mut buffer = MutableBuffer::new(ceil(len, 64) * 8);
+ let mut buffer = Vec::with_capacity(ceil(len, 64));
let chunks = len / 64;
let remainder = len % 64;
- for chunk in 0..chunks {
+ buffer.extend((0..chunks).map(|chunk| {
let mut packed = 0;
for bit_idx in 0..64 {
let i = bit_idx + chunk * 64;
@@ -407,9 +407,8 @@ fn collect_bool(len: usize, neg: bool, f: impl Fn(usize) ->
bool) -> BooleanBuff
packed = !packed
}
- // SAFETY: Already allocated sufficient capacity
- unsafe { buffer.push_unchecked(packed) }
- }
+ packed
+ }));
if remainder != 0 {
let mut packed = 0;
@@ -421,8 +420,7 @@ fn collect_bool(len: usize, neg: bool, f: impl Fn(usize) ->
bool) -> BooleanBuff
packed = !packed
}
- // SAFETY: Already allocated sufficient capacity
- unsafe { buffer.push_unchecked(packed) }
+ buffer.push(packed);
}
BooleanBuffer::new(buffer.into(), 0, len)
}
diff --git a/arrow-select/src/take.rs b/arrow-select/src/take.rs
index eec4ffa14e..a42e28410c 100644
--- a/arrow-select/src/take.rs
+++ b/arrow-select/src/take.rs
@@ -422,9 +422,10 @@ fn take_native<T: ArrowNativeType, I: ArrowPrimitiveType>(
.enumerate()
.map(|(idx, index)| match values.get(index.as_usize()) {
Some(v) => *v,
- None => match n.is_null(idx) {
- true => T::default(),
- false => panic!("Out-of-bounds index {index:?}"),
+ // SAFETY: idx<indices.len()
+ None => match unsafe { n.inner().value_unchecked(idx) } {
+ false => T::default(),
+ true => panic!("Out-of-bounds index {index:?}"),
},
})
.collect(),
@@ -448,8 +449,10 @@ fn take_bits<I: ArrowPrimitiveType>(
let mut output_buffer = MutableBuffer::new_null(len);
let output_slice = output_buffer.as_slice_mut();
nulls.valid_indices().for_each(|idx| {
- if values.value(indices.value(idx).as_usize()) {
- bit_util::set_bit(output_slice, idx);
+ // SAFETY: idx is a valid index in indices.nulls() -->
idx<indices.len()
+ if values.value(unsafe {
indices.value_unchecked(idx).as_usize() }) {
+ // SAFETY: MutableBuffer was created with space for
indices.len() bit, and idx < indices.len()
+ unsafe { bit_util::set_bit_raw(output_slice.as_mut_ptr(),
idx) };
}
});
BooleanBuffer::new(output_buffer.into(), 0, len)