-
Notifications
You must be signed in to change notification settings - Fork 847
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
Decouple buffer deallocation from ffi and allow creating buffers from rust vec #1494
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db72917
Decouple buffer deallocation from ffi and allow zero-copy buffer crea…
jhorstmann 2cc581a
Move allocation owner to alloc module
jhorstmann 55ddb82
Rename and comment Deallocation variants
jhorstmann a919566
Fix doc link
jhorstmann 246c3e5
Explicitly assert that Buffer is UnwindSafe
jhorstmann 9db69cd
Merge remote-tracking branch 'apache/master' into decouple-ffi-deallo…
alamb e6c6c07
fix: doc comment
alamb 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 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 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 |
---|---|---|
|
@@ -1459,10 +1459,11 @@ impl ArrayDataBuilder { | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::ptr::NonNull; | ||
|
||
use crate::array::{ | ||
Array, BooleanBuilder, Int32Array, Int32Builder, Int64Array, StringArray, | ||
StructBuilder, UInt64Array, | ||
make_array, Array, BooleanBuilder, Int32Array, Int32Builder, Int64Array, | ||
StringArray, StructBuilder, UInt64Array, | ||
}; | ||
use crate::buffer::Buffer; | ||
use crate::datatypes::Field; | ||
|
@@ -2594,4 +2595,52 @@ mod tests { | |
|
||
assert_eq!(&struct_array_slice, &cloned); | ||
} | ||
|
||
#[test] | ||
fn test_string_data_from_foreign() { | ||
let mut strings = "foobarfoobar".to_owned(); | ||
let mut offsets = vec![0_i32, 0, 3, 6, 12]; | ||
let mut bitmap = vec![0b1110_u8]; | ||
|
||
let strings_buffer = unsafe { | ||
Buffer::from_custom_allocation( | ||
NonNull::new_unchecked(strings.as_mut_ptr()), | ||
strings.len(), | ||
Arc::new(strings), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems pretty neat to me that the ownership of the |
||
) | ||
}; | ||
let offsets_buffer = unsafe { | ||
Buffer::from_custom_allocation( | ||
NonNull::new_unchecked(offsets.as_mut_ptr() as *mut u8), | ||
offsets.len() * std::mem::size_of::<i32>(), | ||
Arc::new(offsets), | ||
) | ||
}; | ||
let null_buffer = unsafe { | ||
Buffer::from_custom_allocation( | ||
NonNull::new_unchecked(bitmap.as_mut_ptr()), | ||
bitmap.len(), | ||
Arc::new(bitmap), | ||
) | ||
}; | ||
|
||
let data = ArrayData::try_new( | ||
DataType::Utf8, | ||
4, | ||
None, | ||
Some(null_buffer), | ||
0, | ||
vec![offsets_buffer, strings_buffer], | ||
vec![], | ||
) | ||
.unwrap(); | ||
|
||
let array = make_array(data); | ||
let array = array.as_any().downcast_ref::<StringArray>().unwrap(); | ||
|
||
let expected = | ||
StringArray::from(vec![None, Some("foo"), Some("bar"), Some("foobar")]); | ||
|
||
assert_eq!(array, &expected); | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -21,12 +21,10 @@ use std::ptr::NonNull; | |
use std::sync::Arc; | ||
use std::{convert::AsRef, usize}; | ||
|
||
use crate::alloc::{Allocation, Deallocation}; | ||
use crate::ffi::FFI_ArrowArray; | ||
use crate::util::bit_chunk_iterator::{BitChunks, UnalignedBitChunk}; | ||
use crate::{ | ||
bytes::{Bytes, Deallocation}, | ||
datatypes::ArrowNativeType, | ||
ffi, | ||
}; | ||
use crate::{bytes::Bytes, datatypes::ArrowNativeType}; | ||
|
||
use super::ops::bitwise_unary_op_helper; | ||
use super::MutableBuffer; | ||
|
@@ -76,7 +74,7 @@ impl Buffer { | |
/// bytes. If the `ptr` and `capacity` come from a `Buffer`, then this is guaranteed. | ||
pub unsafe fn from_raw_parts(ptr: NonNull<u8>, len: usize, capacity: usize) -> Self { | ||
assert!(len <= capacity); | ||
Buffer::build_with_arguments(ptr, len, Deallocation::Native(capacity)) | ||
Buffer::build_with_arguments(ptr, len, Deallocation::Arrow(capacity)) | ||
} | ||
|
||
/// Creates a buffer from an existing memory region (must already be byte-aligned), this | ||
|
@@ -86,18 +84,41 @@ impl Buffer { | |
/// | ||
/// * `ptr` - Pointer to raw parts | ||
/// * `len` - Length of raw parts in **bytes** | ||
/// * `data` - An [ffi::FFI_ArrowArray] with the data | ||
/// * `data` - An [crate::ffi::FFI_ArrowArray] with the data | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function is unsafe as there is no guarantee that the given pointer is valid for `len` | ||
/// bytes and that the foreign deallocator frees the region. | ||
#[deprecated( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
note = "use from_custom_allocation instead which makes it clearer that the allocation is in fact owned" | ||
)] | ||
pub unsafe fn from_unowned( | ||
ptr: NonNull<u8>, | ||
len: usize, | ||
data: Arc<ffi::FFI_ArrowArray>, | ||
data: Arc<FFI_ArrowArray>, | ||
) -> Self { | ||
Self::from_custom_allocation(ptr, len, data) | ||
} | ||
|
||
/// Creates a buffer from an existing memory region. Ownership of the memory is tracked via reference counting | ||
/// and the memory will be freed using the `drop` method of [crate::alloc::Allocation] when the reference count reaches zero. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `ptr` - Pointer to raw parts | ||
/// * `len` - Length of raw parts in **bytes** | ||
/// * `owner` - A [crate::alloc::Allocation] which is responsible for freeing that data | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function is unsafe as there is no guarantee that the given pointer is valid for `len` bytes | ||
pub unsafe fn from_custom_allocation( | ||
ptr: NonNull<u8>, | ||
len: usize, | ||
owner: Arc<dyn Allocation>, | ||
) -> Self { | ||
Buffer::build_with_arguments(ptr, len, Deallocation::Foreign(data)) | ||
Buffer::build_with_arguments(ptr, len, Deallocation::Custom(owner)) | ||
} | ||
|
||
/// Auxiliary method to create a new Buffer | ||
|
@@ -321,6 +342,7 @@ impl<T: ArrowNativeType> FromIterator<T> for Buffer { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::panic::{RefUnwindSafe, UnwindSafe}; | ||
use std::thread; | ||
|
||
use super::*; | ||
|
@@ -533,4 +555,30 @@ mod tests { | |
Buffer::from(&[0b01101101, 0b10101010]).count_set_bits_offset(7, 9) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_unwind_safe() { | ||
fn assert_unwind_safe<T: RefUnwindSafe + UnwindSafe>() {} | ||
assert_unwind_safe::<Buffer>() | ||
} | ||
|
||
#[test] | ||
fn test_from_foreign_vec() { | ||
let mut vector = vec![1_i32, 2, 3, 4, 5]; | ||
let buffer = unsafe { | ||
Buffer::from_custom_allocation( | ||
NonNull::new_unchecked(vector.as_mut_ptr() as *mut u8), | ||
vector.len() * std::mem::size_of::<i32>(), | ||
Arc::new(vector), | ||
) | ||
}; | ||
|
||
let slice = unsafe { buffer.typed_data::<i32>() }; | ||
assert_eq!(slice, &[1, 2, 3, 4, 5]); | ||
|
||
let buffer = buffer.slice(std::mem::size_of::<i32>()); | ||
|
||
let slice = unsafe { buffer.typed_data::<i32>() }; | ||
assert_eq!(slice, &[2, 3, 4, 5]); | ||
} | ||
} |
This file contains 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 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 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
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.
The trait bound on
RefUnwindSafe
was needed to make one test compile. I'll need to check that in more detail and document why it is required.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 added an explicit test that
Buffer
isUnwindSafe
to make this requirement clearer.