Skip to content

Commit d0d31cc

Browse files
committed
refactor: make bitpacked CPU kernels consume stored chunk layouts
Signed-off-by: "Matt Katz" <mhkatz97@gmail.com> Signed-off-by: Matt Katz <mhkatz97@gmail.com>
1 parent ecd1bd3 commit d0d31cc

36 files changed

Lines changed: 408 additions & 277 deletions

‎encodings/fastlanes/src/bitpacking/array/bitpack_decompress.rs‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ use vortex_array::match_each_integer_ptype;
1717
use vortex_array::match_each_unsigned_integer_ptype;
1818
use vortex_array::patches::Patches;
1919
use vortex_array::scalar::Scalar;
20-
use vortex_error::VortexExpect;
2120
use vortex_error::VortexResult;
2221

2322
use crate::BitPacked;
2423
use crate::BitPackedArrayExt;
2524
use crate::FL_CHUNK_SIZE;
26-
use crate::unpack_iter::BitPacked as BitPackedUnpack;
27-
use crate::unpack_iter::BitUnpackedChunks;
25+
use crate::bitpacking::unpack_iter::BitPacked as BitPackedUnpack;
26+
use crate::bitpacking::unpack_iter::BitUnpackedChunks;
2827

2928
/// Unpacks a bit-packed array into a primitive array.
3029
pub fn unpack_array(
@@ -124,8 +123,9 @@ where
124123
// SAFETY: `decode` writes a value to every slot in this range.
125124
let uninit_slice = unsafe { uninit_range.slice_uninit_mut(0, len) };
126125

126+
let widths = array.chunk_widths(ctx)?;
127127
let mut scratch = [const { MaybeUninit::<F>::uninit() }; FL_CHUNK_SIZE];
128-
let mut chunks = array.unpacked_chunks::<F>(&mut scratch)?;
128+
let mut chunks = array.unpacked_chunks::<F>(&widths, &mut scratch)?;
129129
decode(&mut chunks, uninit_slice, &map);
130130

131131
if let Some(patches) = array.patches() {
@@ -164,19 +164,28 @@ pub(crate) fn apply_patches_to_uninit_range<S: NativePType, T: NativePType, F: F
164164
Ok(())
165165
}
166166

167-
pub fn unpack_single(array: ArrayView<'_, BitPacked>, index: usize) -> Scalar {
168-
let bit_width = array.bit_width() as usize;
167+
pub fn unpack_single(
168+
array: ArrayView<'_, BitPacked>,
169+
index: usize,
170+
ctx: &mut ExecutionCtx,
171+
) -> VortexResult<Scalar> {
169172
let ptype = array.dtype().as_ptype();
170-
// let packed = array.packed().into_primitive()?;
171173
let index_in_encoded = index + array.offset() as usize;
174+
let chunk = index_in_encoded / FL_CHUNK_SIZE;
175+
let index_in_chunk = index_in_encoded % FL_CHUNK_SIZE;
176+
let (range, bit_width) = array.chunk_range(chunk, ctx)?;
172177
let scalar: Scalar = match_each_unsigned_integer_ptype!(ptype.to_unsigned(), |P| {
178+
let packed_chunk =
179+
&array.packed_slice::<P>()[range.start / size_of::<P>()..range.end / size_of::<P>()];
180+
// SAFETY: `packed_chunk` is exactly one packed block at `bit_width`, and the index is
181+
// within the chunk.
173182
unsafe {
174-
unpack_single_primitive::<P>(array.packed_slice::<P>(), bit_width, index_in_encoded)
175-
.into()
183+
BitPacking::unchecked_unpack_single(bit_width as usize, packed_chunk, index_in_chunk)
176184
}
185+
.into()
177186
});
178187
// Cast to fix signedness and nullability
179-
scalar.cast(array.dtype()).vortex_expect("cast failure")
188+
scalar.cast(array.dtype())
180189
}
181190

182191
/// # Safety
@@ -227,12 +236,13 @@ mod tests {
227236
use vortex_buffer::Buffer;
228237
use vortex_buffer::BufferMut;
229238
use vortex_buffer::buffer;
239+
use vortex_error::VortexExpect;
230240
use vortex_session::VortexSession;
231241

232242
use super::*;
233243
use crate::BitPackedArray;
234244
use crate::BitPackedData;
235-
use crate::bitpack_compress::bitpack_encode;
245+
use crate::bitpacking::bitpack_compress::bitpack_encode;
236246

237247
fn encode(array: &PrimitiveArray, bit_width: u8) -> BitPackedArray {
238248
bitpack_encode(array, bit_width, None, &mut SESSION.create_execution_ctx()).unwrap()
@@ -259,7 +269,7 @@ mod tests {
259269
.iter()
260270
.enumerate()
261271
.for_each(|(i, v)| {
262-
let scalar: u16 = (&unpack_single(compressed.as_view(), i))
272+
let scalar: u16 = (&unpack_single(compressed.as_view(), i, &mut ctx).unwrap())
263273
.try_into()
264274
.unwrap();
265275
assert_eq!(scalar, *v);

0 commit comments

Comments
 (0)