-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-11628: [Rust] Made Bytes typed
#9496
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,20 +17,20 @@ | |
|
|
||
| //! This module contains an implementation of a contiguous immutable memory region that knows | ||
| //! how to de-allocate itself, [`Bytes`]. | ||
| //! Note that this is a low-level functionality of this crate. | ||
|
|
||
| use core::slice; | ||
| use std::ptr::NonNull; | ||
| use std::sync::Arc; | ||
| use std::{fmt::Debug, fmt::Formatter}; | ||
| use std::{ptr::NonNull, sync::Arc}; | ||
|
|
||
| use crate::{alloc, ffi}; | ||
| use crate::ffi; | ||
|
|
||
| use super::{alloc, alloc::NativeType}; | ||
|
|
||
| /// Mode of deallocating memory regions | ||
| pub enum Deallocation { | ||
| /// Native deallocation, using Rust deallocator with Arrow-specific memory aligment | ||
| Native(usize), | ||
| /// Foreign interface, via a callback | ||
| // Foreign interface, via a callback | ||
|
||
| Foreign(Arc<ffi::FFI_ArrowArray>), | ||
| } | ||
|
|
||
|
|
@@ -55,9 +55,9 @@ impl Debug for Deallocation { | |
| /// and deallocated accordingly [`free_aligned`](memory::free_aligned). | ||
| /// When the region is allocated by an foreign allocator, [Deallocation::Foreign], this calls the | ||
| /// foreign deallocator to deallocate the region when it is no longer needed. | ||
| pub struct Bytes { | ||
| pub struct Bytes<T: NativeType> { | ||
| /// The raw pointer to be begining of the region | ||
| ptr: NonNull<u8>, | ||
| ptr: NonNull<T>, | ||
|
|
||
| /// The number of bytes visible to this region. This is always smaller than its capacity (when avaliable). | ||
| len: usize, | ||
|
|
@@ -66,7 +66,7 @@ pub struct Bytes { | |
| deallocation: Deallocation, | ||
| } | ||
|
|
||
| impl Bytes { | ||
| impl<T: NativeType> Bytes<T> { | ||
| /// Takes ownership of an allocated memory region, | ||
| /// | ||
| /// # Arguments | ||
|
|
@@ -81,18 +81,19 @@ impl Bytes { | |
| /// bytes. If the `ptr` and `capacity` come from a `Buffer`, then this is guaranteed. | ||
| #[inline] | ||
| pub unsafe fn new( | ||
| ptr: std::ptr::NonNull<u8>, | ||
| ptr: std::ptr::NonNull<T>, | ||
| len: usize, | ||
| deallocation: Deallocation, | ||
| ) -> Bytes { | ||
| Bytes { | ||
| ) -> Self { | ||
| Self { | ||
| ptr, | ||
| len, | ||
| deallocation, | ||
| } | ||
| } | ||
|
|
||
| fn as_slice(&self) -> &[u8] { | ||
| #[inline] | ||
| fn as_slice(&self) -> &[T] { | ||
| self | ||
| } | ||
|
|
||
|
|
@@ -107,7 +108,7 @@ impl Bytes { | |
| } | ||
|
|
||
| #[inline] | ||
| pub fn ptr(&self) -> NonNull<u8> { | ||
| pub fn ptr(&self) -> NonNull<T> { | ||
| self.ptr | ||
| } | ||
|
|
||
|
|
@@ -121,34 +122,34 @@ impl Bytes { | |
| } | ||
| } | ||
|
|
||
| impl Drop for Bytes { | ||
| impl<T: NativeType> Drop for Bytes<T> { | ||
| #[inline] | ||
| fn drop(&mut self) { | ||
| match &self.deallocation { | ||
| Deallocation::Native(capacity) => { | ||
| unsafe { alloc::free_aligned::<u8>(self.ptr, *capacity) }; | ||
| unsafe { alloc::free_aligned(self.ptr, *capacity) }; | ||
| } | ||
| // foreign interface knows how to deallocate itself. | ||
| Deallocation::Foreign(_) => (), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::ops::Deref for Bytes { | ||
| type Target = [u8]; | ||
| impl<T: NativeType> std::ops::Deref for Bytes<T> { | ||
| type Target = [T]; | ||
|
|
||
| fn deref(&self) -> &[u8] { | ||
| fn deref(&self) -> &[T] { | ||
| unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) } | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq for Bytes { | ||
| fn eq(&self, other: &Bytes) -> bool { | ||
| impl<T: NativeType> PartialEq for Bytes<T> { | ||
| fn eq(&self, other: &Bytes<T>) -> bool { | ||
| self.as_slice() == other.as_slice() | ||
| } | ||
| } | ||
|
|
||
| impl Debug for Bytes { | ||
| impl<T: NativeType> Debug for Bytes<T> { | ||
| fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { | ||
| write!(f, "Bytes {{ ptr: {:?}, len: {}, data: ", self.ptr, self.len,)?; | ||
|
|
||
|
|
@@ -157,3 +158,7 @@ impl Debug for Bytes { | |
| write!(f, " }}") | ||
| } | ||
| } | ||
|
|
||
| // This is sound because `Bytes` is an immutable container | ||
| unsafe impl<T: NativeType> Send for Bytes<T> {} | ||
| unsafe impl<T: NativeType> Sync for Bytes<T> {} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorgecarleitao if Buffer always has
Bytes<u8>, I think this alone doesn't address one of the motivations of doing this; being that we'd like to know what boundary to slice buffers in when offsetting arrays.This would be when using
Buffer::offsetandBuffer::lengthon nested arrays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
However, we can't make
Buffer<T>becauseArrayDataforce us to use an homogeneous type across all buffers. We can't dropArrayDatabecause FFI, IPC, CSV, JSON, equality andarray/transformdepend on it.This PR was a stop-gap.
Th target design for me is in the proposal. Specifically, for primitive arrays, it would look like this
But because of what I wrote (a lot of dependencies on
ArrayData), it requires a large (like +10k lines large) refactor of this crate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth still pursuing adding more information to
Bufferwhile working separately on the proposal? I'm blocked on some parquet work that I'm doing, because it depends on being able to correctly slice nested arrays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that any work that unblocks you is worth pursuing :)
A path could be:
Buffersupport both bit slices and byte slices:offset_bitstoBuffer.Buffer::slice_bitmapthat incrementsoffset_bits(and panics if called withoffset != 0).Buffer::slicepanic if called withoffset_bits != 0.These will allow us to slice
Bufferwhen it is either holder of bits and a holder of bytes (we just need to use either API independently). This is required because the Boolean array's values are in bits (and thus part of ArrayData::buffers).pub fn slice(&self, offset: usize, length: usize) -> Selfon every array, that calls
sliceorslice_bitsdepending on what the Buffer represents (and also fix the incorrect slicing of arrays with offsets).make
Array::slicecall the concrete implementationsMake all calls of
get_bitand the like to useBuffer::offset_bitsto compute slot nullability and/or values.Note that I have not tested this and thus this may not work. I also do not know the consequences to IPC and FFI. :/
I am more confident that the proposal works because it passes IPC tests (IPC read and write, little and big endian ;)).