-
Notifications
You must be signed in to change notification settings - Fork 976
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 all 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, MAX_INLINE_VIEW_LEN}; | ||
use arrow_schema::{ArrowError, DataType}; | ||
|
@@ -164,7 +166,7 @@ use super::ByteArrayType; | |
pub struct GenericByteViewArray<T: ByteViewType + ?Sized> { | ||
data_type: DataType, | ||
views: ScalarBuffer<u128>, | ||
buffers: Vec<Buffer>, | ||
buffers: ViewBuffers, | ||
phantom: PhantomData<T>, | ||
nulls: Option<NullBuffer>, | ||
} | ||
|
@@ -187,7 +189,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() | ||
} | ||
|
||
|
@@ -199,9 +205,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 = buffers.into(); | ||
|
||
T::validate(&views, &buffers)?; | ||
|
||
if let Some(n) = nulls.as_ref() { | ||
|
@@ -231,7 +239,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") { | ||
|
@@ -242,7 +250,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> { | |
data_type: T::DATA_TYPE, | ||
phantom: Default::default(), | ||
views, | ||
buffers, | ||
buffers: buffers.into(), | ||
nulls, | ||
} | ||
} | ||
|
@@ -252,7 +260,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(), | ||
} | ||
|
@@ -278,7 +286,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>, ViewBuffers, Option<NullBuffer>) { | ||
(self.views, self.buffers, self.nulls) | ||
} | ||
|
||
|
@@ -609,8 +617,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.0) { | ||
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 this changes the semantics slightly -- and it only shrinks the buffers if they aren't shared Maybe it should be using 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. The underlying Buffers are reference counted too, and only shrink themselves if the reference count is 1. So while this does change the semantics slightly, I don't think it changes as much in practice: When the Arc is shared, the (current) alternative would be to store references to the same buffers in another Vec - thus incrementing the reference counts on the underlying buffers and making their That's also why |
||
buffers.iter_mut().for_each(|b| b.shrink_to_fit()); | ||
} | ||
if let Some(nulls) = &mut self.nulls { | ||
nulls.shrink_to_fit(); | ||
} | ||
|
@@ -668,11 +677,11 @@ impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T> { | |
fn from(value: ArrayData) -> Self { | ||
let views = value.buffers()[0].clone(); | ||
let views = ScalarBuffer::new(views, value.offset(), value.len()); | ||
let buffers = value.buffers()[1..].to_vec(); | ||
let buffers = &value.buffers()[1..]; | ||
Self { | ||
data_type: T::DATA_TYPE, | ||
views, | ||
buffers, | ||
buffers: buffers.into(), | ||
nulls: value.nulls().cloned(), | ||
phantom: Default::default(), | ||
} | ||
|
@@ -736,12 +745,18 @@ 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()); | ||
buffers.extend_from_slice(&array.buffers); | ||
buffers | ||
}; | ||
|
||
let builder = ArrayDataBuilder::new(T::DATA_TYPE) | ||
.len(len) | ||
.buffers(array.buffers) | ||
.buffers(new_buffers) | ||
.nulls(array.nulls); | ||
|
||
unsafe { builder.build_unchecked() } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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::{ops::Deref, 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()) | ||
} | ||
} | ||
|
||
impl Deref for ViewBuffers { | ||
type Target = [Buffer]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.0.as_ref() | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.