-
Notifications
You must be signed in to change notification settings - Fork 829
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
Use Arc<[Buffer]>
instead of raw Vec<Buffer>
in GenericByteViewArray
for faster slice
#6427
Draft
ShiKaiWi
wants to merge
2
commits into
apache:main
Choose a base branch
from
ShiKaiWi:faster-string-view-array-slice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+25
−11
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -114,7 +114,7 @@ use super::ByteArrayType; | |
pub struct GenericByteViewArray<T: ByteViewType + ?Sized> { | ||
data_type: DataType, | ||
views: ScalarBuffer<u128>, | ||
buffers: Vec<Buffer>, | ||
buffers: Arc<[Buffer]>, | ||
phantom: PhantomData<T>, | ||
nulls: Option<NullBuffer>, | ||
} | ||
|
@@ -178,7 +178,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
Ok(Self { | ||
data_type: T::DATA_TYPE, | ||
views, | ||
buffers, | ||
buffers: buffers.into(), | ||
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. I think we should take |
||
nulls, | ||
phantom: Default::default(), | ||
}) | ||
|
@@ -191,14 +191,14 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
/// Safe if [`Self::try_new`] would not error | ||
pub unsafe fn new_unchecked( | ||
views: ScalarBuffer<u128>, | ||
buffers: Vec<Buffer>, | ||
buffers: impl Into<Arc<[Buffer]>>, | ||
nulls: Option<NullBuffer>, | ||
) -> Self { | ||
Self { | ||
data_type: T::DATA_TYPE, | ||
phantom: Default::default(), | ||
views, | ||
buffers, | ||
buffers: buffers.into(), | ||
nulls, | ||
} | ||
} | ||
|
@@ -208,7 +208,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
Self { | ||
data_type: T::DATA_TYPE, | ||
views: vec![0; len].into(), | ||
buffers: vec![], | ||
buffers: vec![].into(), | ||
nulls: Some(NullBuffer::new_null(len)), | ||
phantom: Default::default(), | ||
} | ||
|
@@ -234,7 +234,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
} | ||
|
||
/// Deconstruct this array into its constituent parts | ||
pub fn into_parts(self) -> (ScalarBuffer<u128>, Vec<Buffer>, Option<NullBuffer>) { | ||
pub fn into_parts(self) -> (ScalarBuffer<u128>, Arc<[Buffer]>, Option<NullBuffer>) { | ||
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. This is also a breaking change |
||
(self.views, self.buffers, self.nulls) | ||
} | ||
|
||
|
@@ -516,7 +516,7 @@ impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T> { | |
Self { | ||
data_type: T::DATA_TYPE, | ||
views, | ||
buffers, | ||
buffers: buffers.into(), | ||
nulls: value.nulls().cloned(), | ||
phantom: Default::default(), | ||
} | ||
|
@@ -569,12 +569,20 @@ where | |
} | ||
|
||
impl<T: ByteViewType + ?Sized> From<GenericByteViewArray<T>> for ArrayData { | ||
fn from(mut array: GenericByteViewArray<T>) -> Self { | ||
fn from(array: GenericByteViewArray<T>) -> Self { | ||
let len = array.len(); | ||
array.buffers.insert(0, array.views.into_inner()); | ||
let new_buffers = { | ||
let mut buffers = Vec::with_capacity(array.buffers.len() + 1); | ||
buffers.push(array.views.into_inner()); | ||
for buffer in array.buffers.iter() { | ||
buffers.push(buffer.clone()); | ||
} | ||
buffers | ||
}; | ||
|
||
let builder = ArrayDataBuilder::new(T::DATA_TYPE) | ||
.len(len) | ||
.buffers(array.buffers) | ||
.buffers(new_buffers) | ||
.nulls(array.nulls); | ||
|
||
unsafe { builder.build_unchecked() } | ||
|
Oops, something went wrong.
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.
What's the rationale for Arc<[Buffer]> vs Vec<Arc>?
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.
Cloning an
Arc
is relatively cheap (no allocation), cloning aVec
isn't.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 get it. However, if i understand correctly,
Arc<[Buffer]>
means the buffers can be passed around and shared only when they are within single slice, which can be limiting. For example, Can i merge two arrays, combining theirArc<Buffer>
s without moving or cloning the buffers?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.
No -- you would have to create a new
Vec<Buffer
> (or some other way to getArc<[Buffer]>
)So while there are some cases where new allocations are required, slicing / cloning is faster
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.
but that would prevent buffer sharing between two arrays, right?
cloning yes
slicing -- i didn't see it
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 the observation is that during
StringViewArray::slice
, theslice
actually happens on theview
s -- the buffers (that the views can point at) must be copiedHere is the clone of
buffers
: https://docs.rs/arrow-array/53.1.0/src/arrow_array/array/byte_view_array.rs.html#385