neilconway commented on code in PR #11242:
URL: https://github.com/apache/arrow-rs/pull/11242#discussion_r4118000339
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -2678,20 +2663,24 @@ where
fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions)
-> PrimitiveArray<T>
where
T: ArrowPrimitiveType,
- T::Native: num_traits::NumCast,
{
- let iter = (0..from.len()).map(|i| {
- if from.is_null(i) {
- None
+ let to_numeric = |bits: u64, i: usize| {
+ if bits & (1 << i) != 0 {
+ T::Native::ONE
} else {
- single_bool_to_numeric::<T::Native>(from.value(i))
- }
- });
- // Benefit:
- // 20% performance improvement
- // Soundness:
- // The iterator is trustedLen because it comes from a Range
- unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
+ T::Native::ZERO
+ }
+ };
+ // Unpack a 64-bit word at a time: the fixed-size inner loop helps the
+ // compiler vectorize the conversion.
+ let chunks = from.values().bit_chunks();
+ let mut values = Vec::with_capacity(from.len());
+ for bits in &chunks {
+ values.extend((0..64).map(|i| to_numeric(bits, i)));
+ }
+ let bits = chunks.remainder_bits();
+ values.extend((0..chunks.remainder_len()).map(|i| to_numeric(bits, i)));
Review Comment:
Happy to admit that I did not think of this myself :) But it's a neat trick
that Fable found; it outperforms a simpler loop over `from.values` (and also a
version using `PrimitiveArray::from_unary`) by about 5x.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]