Skip to content

Commit d270200

Browse files
authored
perf(sequence): take through the fallible lane kernels (#10142)
Sequence take checked bounds with a panic inside the map closure and branched on the validity bit per index. It now evaluates every index through `try_map_into` or `try_map_masked_into`.
1 parent dbaf5f8 commit d270200

3 files changed

Lines changed: 50 additions & 32 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎encodings/sequence/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ prost = { workspace = true }
1818
smallvec = { workspace = true }
1919
vortex-array = { workspace = true }
2020
vortex-buffer = { workspace = true }
21+
vortex-compute = { workspace = true }
2122
vortex-error = { workspace = true }
2223
vortex-mask = { workspace = true }
2324
vortex-session = { workspace = true }

‎encodings/sequence/src/compute/take.rs‎

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use vortex_array::dtype::Nullability;
1414
use vortex_array::match_each_integer_ptype;
1515
use vortex_array::scalar::Scalar;
1616
use vortex_array::validity::Validity;
17-
use vortex_buffer::Buffer;
17+
use vortex_buffer::BufferAllocatorRef;
18+
use vortex_buffer::BufferMut;
19+
use vortex_compute::lane_kernels::IndexedSourceExt;
1820
use vortex_error::VortexResult;
1921
use vortex_error::vortex_panic;
2022
use vortex_mask::AllOr;
@@ -24,53 +26,57 @@ use crate::Sequence;
2426
use crate::eval;
2527
use crate::eval::SequenceValue;
2628

29+
/// Evaluates the sequence at every index. Out-of-bounds indices are reported through the lane
30+
/// kernel so the hot loop stays branch-free, and null indices are exempt from the bounds check.
2731
fn take_inner<T: IntegerPType, O: SequenceValue>(
2832
base: O,
2933
multiplier: O,
3034
indices: &[T],
3135
indices_mask: Mask,
3236
result_nullability: Nullability,
3337
len: usize,
38+
allocator: BufferAllocatorRef,
3439
) -> ArrayRef {
35-
match indices_mask.bit_buffer() {
36-
AllOr::All => PrimitiveArray::new(
37-
Buffer::from_trusted_len_iter(indices.iter().map(|i| {
38-
if i.as_() >= len {
39-
vortex_panic!(OutOfBounds: i.as_(), 0, len);
40-
}
41-
eval::wrapping_value(base, multiplier, i.as_())
42-
})),
43-
Validity::from(result_nullability),
44-
)
45-
.into_array(),
46-
AllOr::None => ConstantArray::new(
47-
Scalar::null(DType::Primitive(O::PTYPE, Nullability::Nullable)),
48-
indices.len(),
49-
)
50-
.into_array(),
51-
AllOr::Some(b) => {
52-
let buffer =
53-
Buffer::from_trusted_len_iter(indices.iter().enumerate().map(|(mask_index, i)| {
54-
if b.value(mask_index) {
55-
if i.as_() >= len {
56-
vortex_panic!(OutOfBounds: i.as_(), 0, len);
57-
}
40+
let value_at = |index: T| {
41+
let index: usize = index.as_();
42+
(index < len).then(|| eval::wrapping_value(base, multiplier, index))
43+
};
5844

59-
eval::wrapping_value(base, multiplier, i.as_())
60-
} else {
61-
O::zero()
62-
}
63-
}));
64-
PrimitiveArray::new(buffer, Validity::from(b.clone())).into_array()
45+
let mut buffer = BufferMut::<O>::with_capacity_in(indices.len(), allocator);
46+
let out = &mut buffer.spare_capacity_mut()[..indices.len()];
47+
let validity = match indices_mask.bit_buffer() {
48+
AllOr::All => {
49+
if let Err(position) = indices.try_map_into(out, value_at) {
50+
vortex_panic!(OutOfBounds: indices[position].as_(), 0, len);
51+
}
52+
Validity::from(result_nullability)
6553
}
66-
}
54+
AllOr::None => {
55+
return ConstantArray::new(
56+
Scalar::null(DType::Primitive(O::PTYPE, Nullability::Nullable)),
57+
indices.len(),
58+
)
59+
.into_array();
60+
}
61+
AllOr::Some(bits) => {
62+
if let Err(position) = indices.try_map_masked_into(bits, out, value_at) {
63+
vortex_panic!(OutOfBounds: indices[position].as_(), 0, len);
64+
}
65+
Validity::from(bits.clone())
66+
}
67+
};
68+
// SAFETY: the lane kernel wrote every lane before returning `Ok`.
69+
unsafe { buffer.set_len(indices.len()) };
70+
71+
PrimitiveArray::new(buffer.freeze(), validity).into_array()
6772
}
6873

6974
fn take_with_typed_indices<T: IntegerPType>(
7075
array: ArrayView<'_, Sequence>,
7176
indices: &[T],
7277
indices_mask: Mask,
7378
result_nullability: Nullability,
79+
allocator: BufferAllocatorRef,
7480
) -> VortexResult<ArrayRef> {
7581
match_each_integer_ptype!(array.dtype().as_ptype(), |O| {
7682
let (base, multiplier) = array.wrapping_parts::<O>()?;
@@ -81,6 +87,7 @@ fn take_with_typed_indices<T: IntegerPType>(
8187
indices_mask,
8288
result_nullability,
8389
array.len(),
90+
allocator,
8491
))
8592
})
8693
}
@@ -90,13 +97,15 @@ fn take_sequence(
9097
indices: &PrimitiveArray,
9198
indices_mask: Mask,
9299
result_nullability: Nullability,
100+
allocator: BufferAllocatorRef,
93101
) -> VortexResult<ArrayRef> {
94102
match_each_integer_ptype!(indices.ptype(), |T| {
95103
take_with_typed_indices::<T>(
96104
array,
97105
indices.as_slice::<T>(),
98106
indices_mask,
99107
result_nullability,
108+
allocator,
100109
)
101110
})
102111
}
@@ -111,7 +120,14 @@ impl TakeExecute for Sequence {
111120
let indices = indices.clone().execute::<PrimitiveArray>(ctx)?;
112121
let result_nullability = array.dtype().nullability() | indices.dtype().nullability();
113122

114-
take_sequence(array, &indices, mask, result_nullability).map(Some)
123+
take_sequence(
124+
array,
125+
&indices,
126+
mask,
127+
result_nullability,
128+
ctx.allocator().clone(),
129+
)
130+
.map(Some)
115131
}
116132
}
117133

0 commit comments

Comments
 (0)