Skip to content

Commit 7801846

Browse files
committed
feat: switch the impl to inplace
Signed-off-by: Alexander Droste <[email protected]>
1 parent 4b871b6 commit 7801846

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

encodings/alp/src/alp/compress.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
use itertools::Itertools;
5+
use std::mem::transmute;
56
use vortex_array::arrays::PrimitiveArray;
67
use vortex_array::patches::Patches;
78
use vortex_array::validity::Validity;
@@ -169,18 +170,18 @@ pub fn decompress_chunked(
169170
let patches_values = patches_values.as_slice::<T>();
170171
let patches_chunk_offsets = patches_chunk_offsets.as_slice::<C>();
171172

172-
let alp_buffer = alp_encoded.into_buffer();
173+
let mut alp_buffer = alp_encoded.into_buffer_mut();
173174
let array_len = array.len();
174-
let mut decoded_values = BufferMut::<T>::with_capacity(array_len);
175175
let patches_offset = patches.offset();
176176

177177
for (chunk_idx, chunk_start) in (0..array_len).step_by(1024).enumerate() {
178178
let chunk_end = (chunk_start + 1024).min(array_len);
179-
let chunk_slice = &alp_buffer.as_slice()[chunk_start..chunk_end];
180-
<T>::decode_into_buffer(chunk_slice, array.exponents(), &mut decoded_values);
179+
let chunk_slice = &mut alp_buffer.as_mut_slice()[chunk_start..chunk_end];
180+
<T>::decode_slice_inplace(chunk_slice, array.exponents());
181181

182+
let decoded_chunk: &mut [T] = unsafe { transmute(chunk_slice) };
182183
PrimitiveArray::patch_chunk(
183-
&mut decoded_values,
184+
decoded_chunk,
184185
patches_indices,
185186
patches_values,
186187
patches_offset,
@@ -189,7 +190,8 @@ pub fn decompress_chunked(
189190
);
190191
}
191192

192-
PrimitiveArray::new::<T>(decoded_values, validity)
193+
let decoded_buffer: BufferMut<T> = unsafe { transmute(alp_buffer) };
194+
PrimitiveArray::new::<T>(decoded_buffer.freeze(), validity)
193195
})
194196
})
195197
})

encodings/alp/src/alp/mod.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
use std::fmt::{Display, Formatter};
5-
use std::mem::size_of;
5+
use std::mem::{size_of, transmute, transmute_copy};
66

77
use itertools::Itertools;
88
use num_traits::{CheckedSub, Float, PrimInt, ToPrimitive};
@@ -192,37 +192,20 @@ pub trait ALPFloat: private::Sealed + Float + Display + NativePType {
192192
values
193193
}
194194

195-
/// Decodes a slice of encoded ALP values into the output buffer.
196-
///
197-
/// ## Preconditions
198-
///
199-
/// The `output` buffer must have sufficient spare capacity for `encoded.len()` elements
200-
fn decode_into_buffer(
201-
encoded: &[Self::ALPInt],
202-
exponents: Exponents,
203-
output: &mut BufferMut<Self>,
204-
) {
205-
let input_len = encoded.len();
206-
let current_len = output.len();
207-
let buffer_uninit = output.spare_capacity_mut();
208-
209-
// SAFETY: `MaybeUninit<Self>` and `Self` have the same layout.
210-
let buffer_values: &mut [Self] =
211-
unsafe { std::mem::transmute(&mut buffer_uninit[..input_len]) };
212-
213-
for (idx, &encoded_val) in encoded.iter().enumerate() {
214-
buffer_values[idx] = Self::decode_single(encoded_val, exponents);
215-
}
216-
217-
unsafe {
218-
output.set_len(current_len + input_len);
219-
}
220-
}
221-
222195
fn decode_buffer(encoded: BufferMut<Self::ALPInt>, exponents: Exponents) -> BufferMut<Self> {
223196
encoded.map_each_in_place(move |encoded| Self::decode_single(encoded, exponents))
224197
}
225198

199+
fn decode_slice_inplace(encoded: &mut [Self::ALPInt], exponents: Exponents) {
200+
let decoded: &mut [Self] = unsafe { transmute(encoded) };
201+
decoded.iter_mut().for_each(|v| {
202+
*v = Self::decode_single(
203+
unsafe { transmute_copy::<Self, Self::ALPInt>(v) },
204+
exponents,
205+
)
206+
})
207+
}
208+
226209
#[inline(always)]
227210
fn decode_single(encoded: Self::ALPInt, exponents: Exponents) -> Self {
228211
Self::from_int(encoded) * Self::F10[exponents.f as usize] * Self::IF10[exponents.e as usize]

vortex-array/src/arrays/primitive/array/patch.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use vortex_buffer::BufferMut;
54
use vortex_dtype::{
65
IntegerPType, NativePType, UnsignedPType, match_each_integer_ptype, match_each_native_ptype,
76
};
@@ -61,15 +60,15 @@ impl PrimitiveArray {
6160
///
6261
/// # Arguments
6362
///
64-
/// * `decoded_values` - Mutable buffer of decoded values to be patched
63+
/// * `decoded_values` - Mutable slice of decoded values to be patched
6564
/// * `patches_indices` - Indices indicating which positions to patch
6665
/// * `patches_values` - Values to apply at the patched indices
6766
/// * `patches_offset` - Offset to subtract from patch indices
6867
/// * `chunk_offsets_slice` - Slice containing offsets for each chunk
6968
/// * `chunk_idx` - Index of the chunk to patch
7069
#[inline]
7170
pub fn patch_chunk<T, I, C>(
72-
decoded_values: &mut BufferMut<T>,
71+
decoded_values: &mut [T],
7372
patches_indices: &[I],
7473
patches_values: &[T],
7574
patches_offset: usize,
@@ -87,10 +86,12 @@ impl PrimitiveArray {
8786
patches_indices.len()
8887
};
8988

89+
let chunk_start = chunk_idx * 1024;
9090
for patches_idx in patches_start_idx..patches_end_idx {
9191
let patched_value = patches_values[patches_idx];
92-
let patches_idx_without_offset: usize = patches_indices[patches_idx].as_();
93-
decoded_values[patches_idx_without_offset - patches_offset] = patched_value;
92+
let absolute_index: usize = patches_indices[patches_idx].as_() - patches_offset;
93+
let chunk_relative_index = absolute_index - chunk_start;
94+
decoded_values[chunk_relative_index] = patched_value;
9495
}
9596
}
9697
}

0 commit comments

Comments
 (0)