Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Allocate proper buffer #19357

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
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
41 changes: 21 additions & 20 deletions crates/polars-arrow/src/compute/take/fixed_size_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ fn get_buffer_and_size(array: &dyn Array) -> (&[u8], usize) {
}

unsafe fn from_buffer(mut buf: Vec<u8>, dtype: &ArrowDataType) -> ArrayRef {
assert_eq!(buf.as_ptr().align_offset(256), 0);

match dtype.to_physical_type() {
PhysicalType::Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {

Expand All @@ -127,28 +125,31 @@ unsafe fn from_buffer(mut buf: Vec<u8>, dtype: &ArrowDataType) -> ArrayRef {
}
}

// Use an alignedvec so the alignment always fits the actual type
// That way we can operate on bytes and reduce monomorphization.
#[repr(C, align(256))]
struct Align256([u8; 256]);
unsafe fn aligned_vec(dt: &ArrowDataType, n_bytes: usize) -> Vec<u8> {
match dt.to_physical_type() {
PhysicalType::Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {

let n_units = (n_bytes / size_of::<$T>()) + 1;

unsafe fn aligned_vec(n_bytes: usize) -> Vec<u8> {
// Lazy math to ensure we always have enough.
let n_units = (n_bytes / size_of::<Align256>()) + 1;
let mut aligned: Vec<$T> = Vec::with_capacity(n_units);

let mut aligned: Vec<Align256> = Vec::with_capacity(n_units);
let ptr = aligned.as_mut_ptr();
let len_units = aligned.len();
let cap_units = aligned.capacity();

let ptr = aligned.as_mut_ptr();
let len_units = aligned.len();
let cap_units = aligned.capacity();
std::mem::forget(aligned);

std::mem::forget(aligned);
Vec::from_raw_parts(
ptr as *mut u8,
len_units * size_of::<$T>(),
cap_units * size_of::<$T>(),
)

Vec::from_raw_parts(
ptr as *mut u8,
len_units * size_of::<Align256>(),
cap_units * size_of::<Align256>(),
)
}),
_ => {
unimplemented!()
},
}
}

fn no_inner_validities(values: &ArrayRef) -> bool {
Expand All @@ -174,7 +175,7 @@ pub(super) unsafe fn take_unchecked<O: Index>(
let n_idx = indices.len();
let total_bytes = bytes_per_element * n_idx;

let mut buf = aligned_vec(total_bytes);
let mut buf = aligned_vec(leaves.dtype(), total_bytes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically unsound if a panic occurs between the moment this is allocated until it is forgotten. Should probably wrap it in a ManuallyDrop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

let dst = buf.spare_capacity_mut();

let mut count = 0;
Expand Down
Loading