Skip to content

Commit 49f5351

Browse files
committed
performance: Use push_unchecked when building indices for element take in fixedsizedlist take
Signed-off-by: Robert Kruszewski <[email protected]>
1 parent 848fd89 commit 49f5351

File tree

1 file changed

+16
-14
lines changed
  • vortex-array/src/arrays/fixed_size_list/compute

1 file changed

+16
-14
lines changed

vortex-array/src/arrays/fixed_size_list/compute/take.rs

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

44
use vortex_buffer::BitBufferMut;
5+
use vortex_buffer::BufferMut;
56
use vortex_dtype::IntegerPType;
6-
use vortex_dtype::Nullability;
77
use vortex_dtype::match_each_integer_ptype;
88
use vortex_error::VortexExpect;
99
use vortex_error::VortexResult;
@@ -16,8 +16,6 @@ use crate::ToCanonical;
1616
use crate::arrays::FixedSizeListArray;
1717
use crate::arrays::FixedSizeListVTable;
1818
use crate::arrays::PrimitiveArray;
19-
use crate::builders::ArrayBuilder;
20-
use crate::builders::PrimitiveBuilder;
2119
use crate::compute::TakeKernel;
2220
use crate::compute::TakeKernelAdapter;
2321
use crate::compute::{self};
@@ -92,8 +90,7 @@ fn take_non_nullable_fsl<I: IntegerPType>(
9290
let new_len = indices.len();
9391

9492
// Build the element indices directly without validity tracking.
95-
let mut elements_indices =
96-
PrimitiveBuilder::<I>::with_capacity(Nullability::NonNullable, new_len * list_size);
93+
let mut elements_indices = BufferMut::<I>::with_capacity(new_len * list_size);
9794

9895
// Build the element indices for each list.
9996
for data_idx in indices {
@@ -106,14 +103,17 @@ fn take_non_nullable_fsl<I: IntegerPType>(
106103

107104
// Expand the list into individual element indices.
108105
for i in list_start..list_end {
109-
elements_indices.append_value(I::from_usize(i).vortex_expect("i < list_end"))
106+
unsafe {
107+
elements_indices.push_unchecked(I::from_usize(i).vortex_expect("i < list_end"))
108+
};
110109
}
111110
}
112111

113-
let elements_indices = elements_indices.finish();
112+
let elements_indices = elements_indices.freeze();
114113
debug_assert_eq!(elements_indices.len(), new_len * list_size);
115114

116-
let new_elements = compute::take(array.elements(), elements_indices.as_ref())?;
115+
let elements_indices_array = PrimitiveArray::new(elements_indices, Validity::NonNullable);
116+
let new_elements = compute::take(array.elements(), elements_indices_array.as_ref())?;
117117
debug_assert_eq!(new_elements.len(), new_len * list_size);
118118

119119
// Both inputs are non-nullable, so the result is non-nullable.
@@ -142,8 +142,7 @@ fn take_nullable_fsl<I: IntegerPType>(
142142

143143
// We must use placeholder zeros for null lists to maintain the array length without
144144
// propagating nullability to the element array's take operation.
145-
let mut elements_indices =
146-
PrimitiveBuilder::<I>::with_capacity(Nullability::NonNullable, new_len * list_size);
145+
let mut elements_indices = BufferMut::<I>::with_capacity(new_len * list_size);
147146
let mut new_validity_builder = BitBufferMut::with_capacity(new_len);
148147

149148
// Build the element indices while tracking which lists are null.
@@ -158,7 +157,7 @@ fn take_nullable_fsl<I: IntegerPType>(
158157
if !is_index_valid || !array_validity.value(data_idx) {
159158
// Append placeholder zeros for null lists. These will be masked by the validity array.
160159
// We cannot use append_nulls here as explained above.
161-
elements_indices.append_zeros(list_size);
160+
unsafe { elements_indices.push_n_unchecked(I::zero(), list_size) };
162161
new_validity_builder.append(false);
163162
} else {
164163
// Append the actual element indices for this list.
@@ -167,17 +166,20 @@ fn take_nullable_fsl<I: IntegerPType>(
167166

168167
// Expand the list into individual element indices.
169168
for i in list_start..list_end {
170-
elements_indices.append_value(I::from_usize(i).vortex_expect("i < list_end"))
169+
unsafe {
170+
elements_indices.push_unchecked(I::from_usize(i).vortex_expect("i < list_end"))
171+
};
171172
}
172173

173174
new_validity_builder.append(true);
174175
}
175176
}
176177

177-
let elements_indices = elements_indices.finish();
178+
let elements_indices = elements_indices.freeze();
178179
debug_assert_eq!(elements_indices.len(), new_len * list_size);
179180

180-
let new_elements = compute::take(array.elements(), elements_indices.as_ref())?;
181+
let elements_indices_array = PrimitiveArray::new(elements_indices, Validity::NonNullable);
182+
let new_elements = compute::take(array.elements(), elements_indices_array.as_ref())?;
181183
debug_assert_eq!(new_elements.len(), new_len * list_size);
182184

183185
// At least one input was nullable, so the result is nullable.

0 commit comments

Comments
 (0)