You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/10/16 06:16:47 UTC

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2883: Copying inappropriately aligned buffer in ipc reader

tustvold commented on code in PR #2883:
URL: https://github.com/apache/arrow-rs/pull/2883#discussion_r996395378


##########
arrow/src/ipc/reader.rs:
##########
@@ -499,6 +500,34 @@ fn create_primitive_array(
     make_array(array_data)
 }
 
+/// Checks if given `Buffer` is properly aligned with `i128`.
+/// If not, copying the data and padded it for alignment.
+fn get_aligned_buffer(buffer: &Buffer, length: usize) -> Buffer {
+    let ptr = buffer.as_ptr();
+    let align_req = std::mem::align_of::<i128>();
+    let align_offset = ptr.align_offset(align_req);
+    // The buffer is not aligned properly. The writer might use a smaller alignment
+    // e.g. 8 bytes, but on some platform (e.g. ARM) i128 requires 16 bytes alignment.
+    // We need to copy the buffer as fallback.
+    if align_offset != 0 {
+        let len_in_bytes = length * std::mem::size_of::<i128>();
+        let pad_len = padding(len_in_bytes, align_req);
+        let mut aligned_buffer = MutableBuffer::with_capacity(len_in_bytes + pad_len);
+        aligned_buffer.extend_from_slice(&buffer.as_slice()[0..len_in_bytes]);
+        aligned_buffer.extend_from_slice(&vec![0u8; pad_len]);
+        aligned_buffer.into()
+    } else {
+        buffer.clone()
+    }
+}
+
+/// Calculate byte boundary and return the number of bytes needed to pad to `align_req` bytes
+#[inline]
+fn padding(len: usize, align_req: usize) -> usize {

Review Comment:
   This is already handled for you by MutableBuffer, you shouldn't need to add explicit padding



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org