nevi-me commented on a change in pull request #8262:
URL: https://github.com/apache/arrow/pull/8262#discussion_r501681537
##########
File path: rust/arrow/src/buffer.rs
##########
@@ -254,6 +256,21 @@ impl Buffer {
)
}
+ /// Returns a slice of this buffer starting at a certain bit offset.
+ /// If the offset is byte-aligned the returned buffer is a shallow clone,
+ /// otherwise a new buffer is allocated and filled with a copy of the bits
in the range.
+ pub fn bit_slice(&self, offset: usize, len: usize) -> Self {
+ if offset % 8 == 0 && len % 8 == 0 {
+ return self.slice(offset / 8);
+ }
+
+ bitwise_unary_op_helper(&self, offset, len, |a| a)
+ }
+
+ pub fn bit_chunks(&self, offset: usize, len: usize) -> BitChunks {
Review comment:
May you please add a docstring here
##########
File path: rust/arrow/src/buffer.rs
##########
@@ -369,120 +394,171 @@ where
result.freeze()
}
+/// 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.
fn bitwise_bin_op_helper<F>(
left: &Buffer,
- left_offset: usize,
+ left_offset_in_bits: usize,
right: &Buffer,
- right_offset: usize,
- len: usize,
+ right_offset_in_bits: usize,
+ len_in_bits: usize,
op: F,
) -> Buffer
where
- F: Fn(u8, u8) -> u8,
+ F: Fn(u64, u64) -> u64,
{
- let mut result = MutableBuffer::new(len).with_bitset(len, false);
+ // reserve capacity and set length so we can get a typed view of u64 chunks
+ let mut result =
+ MutableBuffer::new(ceil(len_in_bits, 8)).with_bitset(len_in_bits / 64
* 8, false);
- result
- .data_mut()
- .iter_mut()
- .zip(
- left.data()[left_offset..]
- .iter()
- .zip(right.data()[right_offset..].iter()),
- )
+ 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);
+ let result_chunks = result.typed_data_mut::<u64>().iter_mut();
+
+ result_chunks
+ .zip(left_chunks.iter().zip(right_chunks.iter()))
Review comment:
Amirite that here we're?:
- indexing into left and right at size u64 chunks
- creating a `result_chunks` of same output len in bytes (so len / 8)
- iterating through the l&r chunks, and writing the result of `op(*)` to the
`result_chunks`, which becomes aligned
- doing the same with `remainder_bytes`.
##########
File path: rust/arrow/src/util/bit_chunk_iterator.rs
##########
@@ -0,0 +1,223 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+use crate::buffer::Buffer;
+use crate::util::bit_util::ceil;
+use std::fmt::Debug;
+
+#[derive(Debug)]
+pub struct BitChunks<'a> {
+ buffer: &'a Buffer,
+ raw_data: *const u8,
+ offset: usize,
+ chunk_len: usize,
+ remainder_len: usize,
+}
+
+impl<'a> BitChunks<'a> {
+ pub fn new(buffer: &'a Buffer, offset: usize, len: usize) -> Self {
+ assert!(ceil(offset + len, 8) <= buffer.len() * 8);
Review comment:
Might be helpful to add a failure message to this assertion, also helps
with testing sometimes
##########
File path: rust/arrow/src/buffer.rs
##########
@@ -369,120 +394,171 @@ where
result.freeze()
}
+/// 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.
fn bitwise_bin_op_helper<F>(
left: &Buffer,
- left_offset: usize,
+ left_offset_in_bits: usize,
right: &Buffer,
- right_offset: usize,
- len: usize,
+ right_offset_in_bits: usize,
+ len_in_bits: usize,
op: F,
) -> Buffer
where
- F: Fn(u8, u8) -> u8,
+ F: Fn(u64, u64) -> u64,
{
- let mut result = MutableBuffer::new(len).with_bitset(len, false);
+ // reserve capacity and set length so we can get a typed view of u64 chunks
+ let mut result =
+ MutableBuffer::new(ceil(len_in_bits, 8)).with_bitset(len_in_bits / 64
* 8, false);
- result
- .data_mut()
- .iter_mut()
- .zip(
- left.data()[left_offset..]
- .iter()
- .zip(right.data()[right_offset..].iter()),
- )
+ 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);
+ let result_chunks = result.typed_data_mut::<u64>().iter_mut();
+
+ result_chunks
+ .zip(left_chunks.iter().zip(right_chunks.iter()))
.for_each(|(res, (left, right))| {
- *res = op(*left, *right);
+ *res = op(left, right);
});
+ let remainder_bytes = ceil(left_chunks.remainder_len(), 8);
+ let rem = op(left_chunks.remainder_bits(), right_chunks.remainder_bits());
+ let rem = &rem.to_le_bytes()[0..remainder_bytes];
+ result
+ .write_all(rem)
+ .expect("not enough capacity in buffer");
+
result.freeze()
}
+/// Apply a bitwise operation `op` to one input and return the result as a
Buffer.
+/// The input is treated as a bitmap, meaning that offset and length are
specified in number of bits.
fn bitwise_unary_op_helper<F>(
left: &Buffer,
- left_offset: usize,
- len: usize,
+ offset_in_bits: usize,
+ len_in_bits: usize,
op: F,
) -> Buffer
where
- F: Fn(u8) -> u8,
+ F: Fn(u64) -> u64,
{
- let mut result = MutableBuffer::new(len).with_bitset(len, false);
+ // reserve capacity and set length so we can get a typed view of u64 chunks
+ let mut result =
+ MutableBuffer::new(ceil(len_in_bits, 8)).with_bitset(len_in_bits / 64
* 8, false);
- result
- .data_mut()
- .iter_mut()
- .zip(left.data()[left_offset..].iter())
+ let left_chunks = left.bit_chunks(offset_in_bits, len_in_bits);
+ let result_chunks = result.typed_data_mut::<u64>().iter_mut();
+
+ result_chunks
+ .zip(left_chunks.iter())
.for_each(|(res, left)| {
- *res = op(*left);
+ *res = op(left);
});
+ let remainder_bytes = ceil(left_chunks.remainder_len(), 8);
+ let rem = op(left_chunks.remainder_bits());
+ let rem = &rem.to_le_bytes()[0..remainder_bytes];
+ result
+ .write_all(rem)
+ .expect("not enough capacity in buffer");
+
result.freeze()
}
pub(super) fn buffer_bin_and(
left: &Buffer,
- left_offset: usize,
+ left_offset_in_bits: usize,
right: &Buffer,
- right_offset: usize,
- len: usize,
+ right_offset_in_bits: usize,
+ len_in_bits: usize,
) -> Buffer {
- // SIMD implementation if available
+ // SIMD implementation if available and byte-aligned
Review comment:
Seems like a reasonable tradeoff, that if an array's slice isn't byte
aligned, it might end up being processed on the slower path
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]