-
Notifications
You must be signed in to change notification settings - Fork 981
Continued: Use Arc<[Buffer]> instead of raw Vec<Buffer> in GenericByteViewArray for faster slice #7773
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
base: main
Are you sure you want to change the base?
Continued: Use Arc<[Buffer]> instead of raw Vec<Buffer> in GenericByteViewArray for faster slice #7773
Changes from 6 commits
8421b96
2d9e7af
9078199
0748cc0
aaa2341
25e215a
9eda113
cd4bf18
cf19477
1bb37b2
5733877
e71a332
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,9 @@ use crate::builder::{ArrayBuilder, GenericByteViewBuilder}; | |
use crate::iterator::ArrayIter; | ||
use crate::types::bytes::ByteArrayNativeType; | ||
use crate::types::{BinaryViewType, ByteViewType, StringViewType}; | ||
use crate::{Array, ArrayAccessor, ArrayRef, GenericByteArray, OffsetSizeTrait, Scalar}; | ||
use crate::{ | ||
Array, ArrayAccessor, ArrayRef, GenericByteArray, OffsetSizeTrait, Scalar, ViewBuffers, | ||
}; | ||
use arrow_buffer::{ArrowNativeType, Buffer, NullBuffer, ScalarBuffer}; | ||
use arrow_data::{ArrayData, ArrayDataBuilder, ByteView}; | ||
use arrow_schema::{ArrowError, DataType}; | ||
|
@@ -162,7 +164,7 @@ use super::ByteArrayType; | |
pub struct GenericByteViewArray<T: ByteViewType + ?Sized> { | ||
data_type: DataType, | ||
views: ScalarBuffer<u128>, | ||
buffers: Vec<Buffer>, | ||
buffers: Arc<[Buffer]>, | ||
ctsk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
phantom: PhantomData<T>, | ||
nulls: Option<NullBuffer>, | ||
} | ||
|
@@ -185,7 +187,11 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
/// # Panics | ||
/// | ||
/// Panics if [`GenericByteViewArray::try_new`] returns an error | ||
pub fn new(views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>) -> Self { | ||
pub fn new( | ||
views: ScalarBuffer<u128>, | ||
buffers: impl Into<ViewBuffers>, | ||
nulls: Option<NullBuffer>, | ||
) -> Self { | ||
Self::try_new(views, buffers, nulls).unwrap() | ||
} | ||
|
||
|
@@ -197,9 +203,11 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
/// * [ByteViewType::validate] fails | ||
pub fn try_new( | ||
views: ScalarBuffer<u128>, | ||
buffers: Vec<Buffer>, | ||
buffers: impl Into<ViewBuffers>, | ||
nulls: Option<NullBuffer>, | ||
) -> Result<Self, ArrowError> { | ||
let buffers: Arc<[Buffer]> = buffers.into().0; | ||
|
||
T::validate(&views, &buffers)?; | ||
|
||
if let Some(n) = nulls.as_ref() { | ||
|
@@ -229,7 +237,7 @@ 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<ViewBuffers>, | ||
nulls: Option<NullBuffer>, | ||
) -> Self { | ||
if cfg!(feature = "force_validate") { | ||
|
@@ -240,7 +248,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
data_type: T::DATA_TYPE, | ||
phantom: Default::default(), | ||
views, | ||
buffers, | ||
buffers: buffers.into().0, | ||
nulls, | ||
} | ||
} | ||
|
@@ -250,7 +258,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(), | ||
} | ||
|
@@ -276,7 +284,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>) { | ||
(self.views, self.buffers, self.nulls) | ||
} | ||
|
||
|
@@ -607,8 +615,9 @@ impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> { | |
|
||
fn shrink_to_fit(&mut self) { | ||
self.views.shrink_to_fit(); | ||
self.buffers.iter_mut().for_each(|b| b.shrink_to_fit()); | ||
self.buffers.shrink_to_fit(); | ||
if let Some(buffers) = Arc::get_mut(&mut self.buffers) { | ||
buffers.iter_mut().for_each(|b| b.shrink_to_fit()); | ||
} | ||
if let Some(nulls) = &mut self.nulls { | ||
nulls.shrink_to_fit(); | ||
} | ||
|
@@ -670,7 +679,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(), | ||
} | ||
|
@@ -734,12 +743,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 = { | ||
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 wonder if this code is doing an extra allocation (namely it is now making a new 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 can reproduce the slowdown for the concat kernel on my laptop. For this allocation, I believe we in turn save doing this allocation during 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. Nevermind, it appears that I can't consistently reproduce the slowdown. 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. yeah it is really strange |
||
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()); | ||
} | ||
ctsk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
buffers | ||
}; | ||
|
||
let builder = ArrayDataBuilder::new(T::DATA_TYPE) | ||
.len(len) | ||
.buffers(array.buffers) | ||
.buffers(new_buffers) | ||
.nulls(array.nulls); | ||
|
||
unsafe { builder.build_unchecked() } | ||
|
@@ -795,7 +812,7 @@ impl BinaryViewArray { | |
/// # Safety | ||
/// Caller is responsible for ensuring that items in array are utf8 data. | ||
pub unsafe fn to_string_view_unchecked(self) -> StringViewArray { | ||
StringViewArray::new_unchecked(self.views, self.buffers, self.nulls) | ||
StringViewArray::new_unchecked(self.views, ViewBuffers(self.buffers), self.nulls) | ||
} | ||
} | ||
|
||
|
@@ -827,7 +844,7 @@ pub type StringViewArray = GenericByteViewArray<StringViewType>; | |
impl StringViewArray { | ||
/// Convert the [`StringViewArray`] to [`BinaryViewArray`] | ||
pub fn to_binary_view(self) -> BinaryViewArray { | ||
unsafe { BinaryViewArray::new_unchecked(self.views, self.buffers, self.nulls) } | ||
unsafe { BinaryViewArray::new_unchecked(self.views, ViewBuffers(self.buffers), self.nulls) } | ||
} | ||
|
||
/// Returns true if all data within this array is ASCII | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use arrow_buffer::Buffer; | ||
|
||
/// A cheaply cloneable, owned slice of [`Buffer`] | ||
/// | ||
/// Similar to `Arc<Vec<Buffer>>` or `Arc<[Buffer]>` | ||
#[derive(Clone, Debug)] | ||
pub struct ViewBuffers(pub(crate) Arc<[Buffer]>); | ||
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. One idea I had was instead of |
||
|
||
impl FromIterator<Buffer> for ViewBuffers { | ||
fn from_iter<T: IntoIterator<Item = Buffer>>(iter: T) -> Self { | ||
Self(iter.into_iter().collect()) | ||
} | ||
} | ||
|
||
impl From<Vec<Buffer>> for ViewBuffers { | ||
fn from(value: Vec<Buffer>) -> Self { | ||
Self(value.into()) | ||
} | ||
} | ||
|
||
impl From<&[Buffer]> for ViewBuffers { | ||
fn from(value: &[Buffer]) -> Self { | ||
Self(value.into()) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.