Skip to content

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion arrow-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ name = "occupancy"
harness = false

[[bench]]
name = "gc_view_types"
name = "view_types"
harness = false

[[bench]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ fn criterion_benchmark(c: &mut Criterion) {
hint::black_box(sliced.gc());
});
});

c.bench_function("view types slice", |b| {
b.iter(|| {
black_box(array.slice(0, 100_000 / 2));
});
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
49 changes: 33 additions & 16 deletions arrow-array/src/array/byte_view_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]>,
phantom: PhantomData<T>,
nulls: Option<NullBuffer>,
}
Expand All @@ -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()
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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") {
Expand All @@ -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,
}
}
Expand All @@ -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(),
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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(),
}
Expand Down Expand Up @@ -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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Vec::with_capacity rather than reusing the previous buffers as it was before)

Copy link
Contributor Author

@ctsk ctsk Jun 28, 2025

Choose a reason for hiding this comment

The 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 clone() when using Arc<ViewBuffers>...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, it appears that I can't consistently reproduce the slowdown.

Copy link
Contributor

Choose a reason for hiding this comment

The 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());
}
buffers
};

let builder = ArrayDataBuilder::new(T::DATA_TYPE)
.len(len)
.buffers(array.buffers)
.buffers(new_buffers)
.nulls(array.nulls);

unsafe { builder.build_unchecked() }
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions arrow-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ pub mod temporal_conversions;
pub mod timezone;
mod trusted_len;
pub mod types;
mod view_buffers;
pub use view_buffers::ViewBuffers;

#[cfg(test)]
mod tests {
Expand Down
44 changes: 44 additions & 0 deletions arrow-array/src/view_buffers.rs
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]>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea I had was instead of Arc<[Buffer>] what if we left it as Arc<Vec<Buffer>> so converting back/forth to Vec wasn't as costly 🤔


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())
}
}
Loading