Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions vortex-compute/src/take/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! Take function implementations on slices.

use vortex_buffer::Buffer;
use vortex_buffer::BufferMut;
use vortex_dtype::UnsignedPType;

use crate::take::Take;
Expand Down Expand Up @@ -43,7 +44,21 @@ impl<T: Copy, I: UnsignedPType> Take<[I]> for &[T] {
unused,
reason = "Compiler may see this as unused based on enabled features"
)]
#[inline]
fn take_scalar<T: Copy, I: UnsignedPType>(buffer: &[T], indices: &[I]) -> Buffer<T> {
indices.iter().map(|idx| buffer[idx.as_()]).collect()
// NB: The simpler `indices.iter().map(|idx| buff1er[idx.as_()]).collect()` generates suboptimal
// assembly where the buffer length is repeatedly loaded from the stack on each iteration.

let mut result = BufferMut::with_capacity(indices.len());
let ptr = result.spare_capacity_mut().as_mut_ptr().cast::<T>();

// This explicit loop with pointer writes keeps the length in a register and avoids per-element
// capacity checks from `push()`.
for (i, idx) in indices.iter().enumerate() {
// SAFETY: We reserved `indices.len()` capacity, so `ptr.add(i)` is valid.
unsafe { ptr.add(i).write(buffer[idx.as_()]) };
}

// SAFETY: We just wrote exactly `indices.len()` elements.
unsafe { result.set_len(indices.len()) };
result.freeze()
}
Loading