Skip to content

Commit 9c01ab9

Browse files
committed
borrow block offsets in bit_widths
Signed-off-by: Matt Katz <[email protected]>
1 parent ad8ba8c commit 9c01ab9

16 files changed

Lines changed: 69 additions & 42 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use vortex_error::vortex_bail;
2222

2323
use crate::BitPacked;
2424
use crate::BitPackedArrayExt;
25-
use crate::BitWidths;
25+
use crate::BitWidthsView;
2626
use crate::FL_CHUNK_SIZE;
2727
use crate::unpack_iter::BitPacked as BitPackedUnpack;
2828
use crate::unpack_iter::BitUnpackedChunks;
@@ -167,7 +167,7 @@ pub(crate) fn apply_patches_to_uninit_range<S: NativePType, T: NativePType, F: F
167167
}
168168

169169
pub fn unpack_single(array: ArrayView<'_, BitPacked>, index: usize) -> VortexResult<Scalar> {
170-
let BitWidths::Global(bit_width) = array.bit_widths() else {
170+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
171171
vortex_bail!("BitPacked array has per-block bit widths");
172172
};
173173
let bit_width = bit_width as usize;

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,43 @@ where
134134
Ok(())
135135
}
136136

137-
/// How the blocks of a [`BitPackedArray`] are packed.
138-
#[derive(Clone, Debug)]
139-
pub enum BitWidths {
137+
/// How the blocks of a [`BitPackedArray`] are packed, borrowing block offsets if present.
138+
#[derive(Clone, Copy, Debug)]
139+
pub enum BitWidthsView<'a> {
140140
/// Every block is packed at this bit width.
141141
Global(u8),
142142
/// Byte boundaries of the packed blocks, from which each block's bit width is derived.
143-
Blocked(ArrayRef),
143+
Blocked(&'a ArrayRef),
144144
}
145145

146-
impl BitWidths {
146+
impl BitWidthsView<'_> {
147147
/// Returns `true` if every block is packed at one bit width.
148148
#[inline]
149149
pub fn is_global(&self) -> bool {
150150
matches!(self, Self::Global(_))
151151
}
152152
}
153153

154+
/// How the blocks of a [`BitPackedArray`] are packed, owning block offsets if present.
155+
///
156+
/// This is the owned form of [`BitWidthsView`], held by [`BitPackedDataParts`].
157+
#[derive(Clone, Debug)]
158+
pub enum BitWidths {
159+
/// Every block is packed at this bit width.
160+
Global(u8),
161+
/// Byte boundaries of the packed blocks, from which each block's bit width is derived.
162+
Blocked(ArrayRef),
163+
}
164+
165+
impl From<BitWidthsView<'_>> for BitWidths {
166+
fn from(view: BitWidthsView<'_>) -> Self {
167+
match view {
168+
BitWidthsView::Global(bit_width) => Self::Global(bit_width),
169+
BitWidthsView::Blocked(block_offsets) => Self::Blocked(block_offsets.clone()),
170+
}
171+
}
172+
}
173+
154174
pub struct BitPackedDataParts {
155175
pub offset: u16,
156176
pub bit_widths: BitWidths,
@@ -396,10 +416,10 @@ pub trait BitPackedArrayExt: BitPackedArraySlotsExt {
396416
/// How the blocks are packed: at one global bit width, or at the widths implied by the block
397417
/// offsets.
398418
#[inline]
399-
fn bit_widths(&self) -> BitWidths {
419+
fn bit_widths(&self) -> BitWidthsView<'_> {
400420
match (self.global_bit_width, self.block_offsets()) {
401-
(Some(bit_width), None) => BitWidths::Global(bit_width),
402-
(None, Some(block_offsets)) => BitWidths::Blocked(block_offsets.clone()),
421+
(Some(bit_width), None) => BitWidthsView::Global(bit_width),
422+
(None, Some(block_offsets)) => BitWidthsView::Blocked(block_offsets),
403423
_ => vortex_panic!(
404424
"BitPacked must have exactly one of a global bit width and block offsets"
405425
),

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::BitPackedArrayExt;
3636
use crate::BitPackedArraySlotsExt;
3737
use crate::BitPackedData;
3838
use crate::BitWidths;
39+
use crate::BitWidthsView;
3940
use crate::bitpacking::bitpack_compress::bitpack_to_best_bit_width;
4041

4142
static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
@@ -68,7 +69,7 @@ fn with_block_offsets(array: &BitPackedArray, offsets: ArrayRef) -> VortexResult
6869
#[test]
6970
fn global_bit_width_has_no_block_offsets() -> VortexResult<()> {
7071
let uniform = uniform()?;
71-
assert!(matches!(uniform.bit_widths(), BitWidths::Global(7)));
72+
assert!(matches!(uniform.bit_widths(), BitWidthsView::Global(7)));
7273
assert!(uniform.block_offsets().is_none());
7374
Ok(())
7475
}
@@ -156,7 +157,7 @@ fn block_offsets_have_no_constant_width(
156157
#[case] offsets: vortex_buffer::Buffer<u64>,
157158
) -> VortexResult<()> {
158159
let array = with_block_offsets(&uniform()?, offsets.into_array())?;
159-
assert!(matches!(array.bit_widths(), BitWidths::Blocked(_)));
160+
assert!(matches!(array.bit_widths(), BitWidthsView::Blocked(_)));
160161
// Decoding per-block widths is not supported yet.
161162
assert!(
162163
array
@@ -319,16 +320,19 @@ fn construct_blocks_without_a_uniform_width() -> VortexResult<()> {
319320
2048,
320321
0,
321322
)?;
322-
assert!(matches!(array.bit_widths(), BitWidths::Blocked(_)));
323+
assert!(matches!(array.bit_widths(), BitWidthsView::Blocked(_)));
323324
Ok(())
324325
}
325326

326327
#[test]
327328
fn empty_and_zero_width_arrays() -> VortexResult<()> {
328329
let mut ctx = SESSION.create_execution_ctx();
329-
assert!(matches!(encode(&[])?.bit_widths(), BitWidths::Global(0)));
330+
assert!(matches!(
331+
encode(&[])?.bit_widths(),
332+
BitWidthsView::Global(0)
333+
));
330334
let zeros = encode(&vec![0u32; 2049])?;
331-
assert!(matches!(zeros.bit_widths(), BitWidths::Global(0)));
335+
assert!(matches!(zeros.bit_widths(), BitWidthsView::Global(0)));
332336
assert_eq!(zeros.packed().len(), 0);
333337
assert_arrays_eq!(zeros, PrimitiveArray::from_iter(vec![0u32; 2049]), &mut ctx);
334338
Ok(())

‎encodings/fastlanes/src/bitpacking/compute/cast.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use vortex_array::validity::Validity;
1717
use vortex_error::VortexResult;
1818

1919
use crate::bitpacking::BitPacked;
20-
use crate::bitpacking::BitWidths;
20+
use crate::bitpacking::BitWidthsView;
2121
use crate::bitpacking::array::BitPackedArrayExt;
2222
use crate::bitpacking::array::bitpack_decompress::unpack_map_into_builder;
2323

@@ -55,7 +55,7 @@ fn build_with_validity(
5555

5656
impl CastReduce for BitPacked {
5757
fn cast(array: ArrayView<'_, Self>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
58-
let BitWidths::Global(bit_width) = array.bit_widths() else {
58+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
5959
return Ok(None);
6060
};
6161
if !array.dtype().eq_ignore_nullability(dtype) {
@@ -77,7 +77,7 @@ impl CastKernel for BitPacked {
7777
dtype: &DType,
7878
ctx: &mut ExecutionCtx,
7979
) -> VortexResult<Option<ArrayRef>> {
80-
let BitWidths::Global(bit_width) = array.bit_widths() else {
80+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
8181
return Ok(None);
8282
};
8383
// Nullability-only change: keep the values bit-packed, just adjust validity.

‎encodings/fastlanes/src/bitpacking/compute/compare.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use vortex_error::VortexResult;
2828

2929
use crate::BitPacked;
3030
use crate::BitPackedArrayExt;
31-
use crate::BitWidths;
31+
use crate::BitWidthsView;
3232
use crate::bitpacking::compute::compare_fused::stream_compare_fused;
3333
use crate::unpack_iter::BitPacked as BitPackedIter;
3434

@@ -39,7 +39,7 @@ impl CompareKernel for BitPacked {
3939
operator: CompareOperator,
4040
ctx: &mut ExecutionCtx,
4141
) -> VortexResult<Option<ArrayRef>> {
42-
let BitWidths::Global(bit_width) = lhs.bit_widths() else {
42+
let BitWidthsView::Global(bit_width) = lhs.bit_widths() else {
4343
return Ok(None);
4444
};
4545
// Only accelerate compare-against-constant.

‎encodings/fastlanes/src/bitpacking/compute/filter.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use super::take::UNPACK_CHUNK_THRESHOLD;
2626
use crate::BitPacked;
2727
use crate::BitPackedArrayExt;
2828
use crate::BitPackedData;
29-
use crate::BitWidths;
29+
use crate::BitWidthsView;
3030

3131
/// The threshold over which it is faster to fully unpack the entire [`BitPackedArray`](crate::BitPackedArray) and then
3232
/// filter the result than to unpack only specific bitpacked values into the output buffer.
@@ -50,7 +50,7 @@ impl FilterKernel for BitPacked {
5050
mask: &Mask,
5151
ctx: &mut ExecutionCtx,
5252
) -> VortexResult<Option<ArrayRef>> {
53-
let BitWidths::Global(bit_width) = array.bit_widths() else {
53+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
5454
return Ok(None);
5555
};
5656
let values = match mask {

‎encodings/fastlanes/src/bitpacking/compute/slice.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use vortex_array::patches::Patches;
1414
use vortex_error::VortexResult;
1515

1616
use crate::BitPacked;
17-
use crate::BitWidths;
17+
use crate::BitWidthsView;
1818
use crate::bitpacking::array::BitPackedArrayExt;
1919

2020
impl SliceReduce for BitPacked {
2121
fn slice(array: ArrayView<'_, Self>, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
22-
let BitWidths::Global(bit_width) = array.bit_widths() else {
22+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
2323
return Ok(None);
2424
};
2525
// We cannot access buffers (to slice the patches).
@@ -37,7 +37,7 @@ impl SliceKernel for BitPacked {
3737
range: Range<usize>,
3838
_ctx: &mut ExecutionCtx,
3939
) -> VortexResult<Option<ArrayRef>> {
40-
let BitWidths::Global(bit_width) = array.bit_widths() else {
40+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
4141
return Ok(None);
4242
};
4343
let patches = array

‎encodings/fastlanes/src/bitpacking/compute/take.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use vortex_error::VortexResult;
2525
use super::chunked_indices;
2626
use crate::BitPacked;
2727
use crate::BitPackedArrayExt;
28-
use crate::BitWidths;
28+
use crate::BitWidthsView;
2929
use crate::bitpack_decompress;
3030

3131
// TODO(connor): This is duplicated in `encodings/fastlanes/src/bitpacking/kernels/mod.rs`.
@@ -40,7 +40,7 @@ impl TakeExecute for BitPacked {
4040
indices: &ArrayRef,
4141
ctx: &mut ExecutionCtx,
4242
) -> VortexResult<Option<ArrayRef>> {
43-
let BitWidths::Global(bit_width) = array.bit_widths() else {
43+
let BitWidthsView::Global(bit_width) = array.bit_widths() else {
4444
return Ok(None);
4545
};
4646
// If the indices are large enough, it's faster to flatten and take the primitive array.

‎encodings/fastlanes/src/bitpacking/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use array::BitPackedData;
88
pub use array::BitPackedDataParts;
99
pub use array::BitPackedSlots;
1010
pub use array::BitWidths;
11+
pub use array::BitWidthsView;
1112
pub use array::bitpack_compress;
1213
pub use array::bitpack_decompress;
1314
pub use array::unpack_iter;

‎encodings/fastlanes/src/bitpacking/plugin.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::BitPacked;
3838
use crate::BitPackedArray;
3939
use crate::BitPackedArrayExt;
4040
use crate::BitPackedData;
41-
use crate::BitWidths;
41+
use crate::BitWidthsView;
4242
use crate::bitpacking::array::BitPackedSlots;
4343

4444
#[derive(Clone, prost::Message)]
@@ -71,7 +71,7 @@ impl ArrayPlugin for BitPackedPlugin {
7171
let view = array.as_opt::<BitPacked>().ok_or_else(|| {
7272
vortex_err!("BitPacked plugin cannot serialize {}", array.encoding_id())
7373
})?;
74-
let BitWidths::Global(bit_width) = view.bit_widths() else {
74+
let BitWidthsView::Global(bit_width) = view.bit_widths() else {
7575
vortex_bail!("BitPacked plugin cannot serialize per-block bit widths");
7676
};
7777
let metadata = BitPackedMetadata {
@@ -230,7 +230,7 @@ impl ArrayPlugin for BitPackedPatchedPlugin {
230230
let packed = bitpacked.packed().clone();
231231
let ptype = bitpacked.dtype().as_ptype();
232232
let validity = bitpacked.validity()?;
233-
let BitWidths::Global(bw) = bitpacked.bit_widths() else {
233+
let BitWidthsView::Global(bw) = bitpacked.bit_widths() else {
234234
vortex_panic!("BitPacked plugin always deserializes a global bit width");
235235
};
236236
let len = bitpacked.len();

0 commit comments

Comments
 (0)