|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
| 4 | +use std::mem::MaybeUninit; |
4 | 5 | use std::sync::Arc; |
5 | 6 |
|
| 7 | +use fsst::Decompressor; |
6 | 8 | use vortex_array::ArrayRef; |
7 | 9 | use vortex_array::ArrayView; |
8 | 10 | use vortex_array::ExecutionCtx; |
@@ -42,36 +44,82 @@ pub(super) fn canonicalize_fsst( |
42 | 44 | }) |
43 | 45 | } |
44 | 46 |
|
| 47 | +/// Extra headroom that keeps [`Decompressor::decompress_into`] on its fast path. |
| 48 | +/// |
| 49 | +/// It never writes past the slice it is given — every store is bounded by the end of the output — |
| 50 | +/// but it emits whole 8-byte symbols, so it can only use the wide-store loop while at least 8 |
| 51 | +/// bytes remain. Handing it 7 spare bytes lets that loop run through the final value instead of |
| 52 | +/// finishing byte at a time. This is a performance knob, not a safety requirement. |
| 53 | +/// |
| 54 | +/// [`Decompressor::decompress_into`]: fsst::Decompressor::decompress_into |
| 55 | +pub(crate) const FSST_DECODE_SLACK: usize = 7; |
| 56 | + |
| 57 | +/// Everything needed to decode an FSST array's values in one bulk `decompress_into` call. |
| 58 | +pub(crate) struct FsstDecodePlan { |
| 59 | + codes: ByteBuffer, |
| 60 | + /// Per-row uncompressed lengths, zero for null rows. |
| 61 | + pub(crate) lengths: PrimitiveArray, |
| 62 | + /// Total decoded size, i.e. the sum of `lengths`. |
| 63 | + pub(crate) total_size: usize, |
| 64 | +} |
| 65 | + |
| 66 | +impl FsstDecodePlan { |
| 67 | + pub(crate) fn new( |
| 68 | + fsst_array: ArrayView<'_, FSST>, |
| 69 | + ctx: &mut ExecutionCtx, |
| 70 | + ) -> VortexResult<Self> { |
| 71 | + let codes = fsst_array.codes().sliced_bytes(); |
| 72 | + let lengths = fsst_array |
| 73 | + .uncompressed_lengths() |
| 74 | + .clone() |
| 75 | + .execute::<PrimitiveArray>(ctx)?; |
| 76 | + |
| 77 | + #[expect(clippy::cast_possible_truncation)] |
| 78 | + let total_size: usize = match_each_integer_ptype!(lengths.ptype(), |P| { |
| 79 | + lengths.as_slice::<P>().iter().map(|x| *x as usize).sum() |
| 80 | + }); |
| 81 | + |
| 82 | + Ok(Self { |
| 83 | + codes, |
| 84 | + lengths, |
| 85 | + total_size, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + /// Bulk-decompresses the whole code stream into `out`, which must hold at least |
| 90 | + /// `total_size + FSST_DECODE_SLACK` bytes. |
| 91 | + /// |
| 92 | + /// Kept inlinable so the decoder is not called from behind an extra frame in whichever |
| 93 | + /// codegen unit the caller lands in; see OnPair's equivalent for why that matters. |
| 94 | + #[inline] |
| 95 | + pub(crate) fn decode_into( |
| 96 | + &self, |
| 97 | + decompressor: &Decompressor<'_>, |
| 98 | + out: &mut [MaybeUninit<u8>], |
| 99 | + ) -> VortexResult<usize> { |
| 100 | + let len = decompressor.decompress_into(self.codes.as_slice(), out); |
| 101 | + vortex_ensure!( |
| 102 | + len == self.total_size, |
| 103 | + "FSST decoded {len} bytes, expected {}", |
| 104 | + self.total_size |
| 105 | + ); |
| 106 | + Ok(len) |
| 107 | + } |
| 108 | +} |
| 109 | + |
45 | 110 | pub(crate) fn fsst_decode_bytes( |
46 | 111 | fsst_array: ArrayView<'_, FSST>, |
47 | 112 | ctx: &mut ExecutionCtx, |
48 | 113 | ) -> VortexResult<(ByteBufferMut, PrimitiveArray)> { |
49 | | - let bytes = fsst_array.codes().sliced_bytes(); |
50 | | - let uncompressed_lens_array = fsst_array |
51 | | - .uncompressed_lengths() |
52 | | - .clone() |
53 | | - .execute::<PrimitiveArray>(ctx)?; |
54 | | - |
55 | | - #[expect(clippy::cast_possible_truncation)] |
56 | | - let total_size: usize = match_each_integer_ptype!(uncompressed_lens_array.ptype(), |P| { |
57 | | - uncompressed_lens_array |
58 | | - .as_slice::<P>() |
59 | | - .iter() |
60 | | - .map(|x| *x as usize) |
61 | | - .sum() |
62 | | - }); |
63 | | - |
64 | | - let decompressor = fsst_array.decompressor(); |
65 | | - let mut uncompressed_bytes = ByteBufferMut::with_capacity(total_size + 7); |
66 | | - let len = |
67 | | - decompressor.decompress_into(bytes.as_slice(), uncompressed_bytes.spare_capacity_mut()); |
68 | | - vortex_ensure!( |
69 | | - len == total_size, |
70 | | - "FSST decoded {len} bytes, expected {total_size}" |
71 | | - ); |
72 | | - // SAFETY: `decompress_into` initialized the first `len` bytes. |
| 114 | + let plan = FsstDecodePlan::new(fsst_array, ctx)?; |
| 115 | + let mut uncompressed_bytes = ByteBufferMut::with_capacity(plan.total_size + FSST_DECODE_SLACK); |
| 116 | + let len = plan.decode_into( |
| 117 | + &fsst_array.decompressor(), |
| 118 | + uncompressed_bytes.spare_capacity_mut(), |
| 119 | + )?; |
| 120 | + // SAFETY: `decode_into` initialized the first `len` bytes. |
73 | 121 | unsafe { uncompressed_bytes.set_len(len) }; |
74 | | - Ok((uncompressed_bytes, uncompressed_lens_array)) |
| 122 | + Ok((uncompressed_bytes, plan.lengths)) |
75 | 123 | } |
76 | 124 |
|
77 | 125 | pub(crate) fn fsst_decode_views( |
|
0 commit comments