alamb commented on code in PR #8793:
URL: https://github.com/apache/arrow-rs/pull/8793#discussion_r2503952030


##########
arrow-buffer/src/buffer/ops.rs:
##########
@@ -60,39 +61,70 @@ where
 
 /// Apply a bitwise operation `op` to two inputs and return the result as a 
Buffer.
 /// The inputs are treated as bitmaps, meaning that offsets and length are 
specified in number of bits.
+///
+/// The output is guaranteed to have
+/// 1. all bits outside the specified range set to zero
+/// 2. start at offset zero
 pub fn bitwise_bin_op_helper<F>(
     left: &Buffer,
     left_offset_in_bits: usize,
     right: &Buffer,
     right_offset_in_bits: usize,
     len_in_bits: usize,
-    mut op: F,
+    op: F,
 ) -> Buffer
 where
     F: FnMut(u64, u64) -> u64,
 {
-    let left_chunks = left.bit_chunks(left_offset_in_bits, len_in_bits);
-    let right_chunks = right.bit_chunks(right_offset_in_bits, len_in_bits);
+    if len_in_bits == 0 {
+        return Buffer::default();
+    }
 
-    let chunks = left_chunks
-        .iter()
-        .zip(right_chunks.iter())
-        .map(|(left, right)| op(left, right));
-    // Soundness: `BitChunks` is a `BitChunks` iterator which
-    // correctly reports its upper bound
-    let mut buffer = unsafe { MutableBuffer::from_trusted_len_iter(chunks) };
+    // figure out the starting byte for left buffer
+    let start_byte = left_offset_in_bits / 8;
+    let starting_bit_in_byte = left_offset_in_bits % 8;
 
-    let remainder_bytes = ceil(left_chunks.remainder_len(), 8);
-    let rem = op(left_chunks.remainder_bits(), right_chunks.remainder_bits());
-    // we are counting its starting from the least significant bit, to 
to_le_bytes should be correct
-    let rem = &rem.to_le_bytes()[0..remainder_bytes];
-    buffer.extend_from_slice(rem);
+    let len_bytes = ceil(starting_bit_in_byte + len_in_bits, 8);
+    let mut result = left[start_byte..len_bytes].to_vec();

Review Comment:
   optimization works well: https://github.com/apache/arrow-rs/pull/8807



-- 
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]

Reply via email to