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
6 changes: 6 additions & 0 deletions arrow-array/benches/view_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ fn criterion_benchmark(c: &mut Criterion) {
black_box(array.slice(0, 100_000 / 2));
});
});

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

criterion_group!(benches, criterion_benchmark);
Expand Down
45 changes: 30 additions & 15 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, MAX_INLINE_VIEW_LEN};
use arrow_schema::{ArrowError, DataType};
Expand Down Expand Up @@ -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>,
}
Expand All @@ -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()
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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") {
Expand All @@ -242,7 +250,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
data_type: T::DATA_TYPE,
phantom: Default::default(),
views,
buffers,
buffers: buffers.into(),
nulls,
}
}
Expand All @@ -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(),
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Arc::make_mut 🤔

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.

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 shrink_to_fit a no-op.

That's also why make_mut wont lead to more shrinking

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 @@ -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(),
}
Expand Down Expand Up @@ -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 = {
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());
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() }
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
52 changes: 52 additions & 0 deletions arrow-array/src/view_buffers.rs
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]>);
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())
}
}

impl Deref for ViewBuffers {
type Target = [Buffer];

fn deref(&self) -> &Self::Target {
self.0.as_ref()
}
}
48 changes: 28 additions & 20 deletions arrow/benches/concatenate_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ fn bench_concat_arrays(arrays: &[&dyn Array]) {
fn add_benchmark(c: &mut Criterion) {
let v1 = create_primitive_array::<Int32Type>(1024, 0.0);
let v2 = create_primitive_array::<Int32Type>(1024, 0.0);
c.bench_function("concat i32 1024", |b| b.iter(|| bench_concat(&v1, &v2)));
c.bench_function("concat i32 1024", |b| {
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let v1 = create_primitive_array::<Int32Type>(1024, 0.5);
let v2 = create_primitive_array::<Int32Type>(1024, 0.5);
c.bench_function("concat i32 nulls 1024", |b| {
b.iter(|| bench_concat(&v1, &v2))
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let small_array = create_primitive_array::<Int32Type>(4, 0.0);
let arrays: Vec<_> = (0..1024).map(|_| &small_array as &dyn Array).collect();
c.bench_function("concat 1024 arrays i32 4", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});

{
Expand All @@ -59,7 +61,7 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Vec<_>>();
let arrays: Vec<_> = input.iter().map(|arr| arr as &dyn Array).collect();
c.bench_function("concat i32 8192 over 100 arrays", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}

Expand All @@ -69,24 +71,26 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Vec<_>>();
let arrays: Vec<_> = input.iter().map(|arr| arr as &dyn Array).collect();
c.bench_function("concat i32 nulls 8192 over 100 arrays", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}

let v1 = create_boolean_array(1024, 0.0, 0.5);
let v2 = create_boolean_array(1024, 0.0, 0.5);
c.bench_function("concat boolean 1024", |b| b.iter(|| bench_concat(&v1, &v2)));
c.bench_function("concat boolean 1024", |b| {
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let v1 = create_boolean_array(1024, 0.5, 0.5);
let v2 = create_boolean_array(1024, 0.5, 0.5);
c.bench_function("concat boolean nulls 1024", |b| {
b.iter(|| bench_concat(&v1, &v2))
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let small_array = create_boolean_array(4, 0.0, 0.5);
let arrays: Vec<_> = (0..1024).map(|_| &small_array as &dyn Array).collect();
c.bench_function("concat 1024 arrays boolean 4", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});

{
Expand All @@ -95,7 +99,7 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Vec<_>>();
let arrays: Vec<_> = input.iter().map(|arr| arr as &dyn Array).collect();
c.bench_function("concat boolean 8192 over 100 arrays", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}

Expand All @@ -105,24 +109,26 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Vec<_>>();
let arrays: Vec<_> = input.iter().map(|arr| arr as &dyn Array).collect();
c.bench_function("concat boolean nulls 8192 over 100 arrays", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}

let v1 = create_string_array::<i32>(1024, 0.0);
let v2 = create_string_array::<i32>(1024, 0.0);
c.bench_function("concat str 1024", |b| b.iter(|| bench_concat(&v1, &v2)));
c.bench_function("concat str 1024", |b| {
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let v1 = create_string_array::<i32>(1024, 0.5);
let v2 = create_string_array::<i32>(1024, 0.5);
c.bench_function("concat str nulls 1024", |b| {
b.iter(|| bench_concat(&v1, &v2))
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let small_array = create_string_array::<i32>(4, 0.0);
let arrays: Vec<_> = (0..1024).map(|_| &small_array as &dyn Array).collect();
c.bench_function("concat 1024 arrays str 4", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});

{
Expand All @@ -131,7 +137,7 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Vec<_>>();
let arrays: Vec<_> = input.iter().map(|arr| arr as &dyn Array).collect();
c.bench_function("concat str 8192 over 100 arrays", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}

Expand All @@ -141,7 +147,7 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Vec<_>>();
let arrays: Vec<_> = input.iter().map(|arr| arr as &dyn Array).collect();
c.bench_function("concat str nulls 8192 over 100 arrays", |b| {
b.iter(|| bench_concat_arrays(&arrays))
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}

Expand All @@ -155,7 +161,9 @@ fn add_benchmark(c: &mut Criterion) {
let id = format!(
"concat utf8_view {name} max_str_len={str_len} null_density={null_density}"
);
c.bench_function(&id, |b| b.iter(|| bench_concat_arrays(&arrays)));
c.bench_function(&id, |b| {
b.iter_with_large_drop(|| bench_concat_arrays(&arrays))
});
}
}

Expand All @@ -164,15 +172,15 @@ fn add_benchmark(c: &mut Criterion) {
let v2 = create_string_array_with_len::<i32>(10, 0.0, 20);
let v2 = create_dict_from_values::<Int32Type>(1024, 0.0, &v2);
c.bench_function("concat str_dict 1024", |b| {
b.iter(|| bench_concat(&v1, &v2))
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let v1 = create_string_array_with_len::<i32>(1024, 0.0, 20);
let v1 = create_sparse_dict_from_values::<Int32Type>(1024, 0.0, &v1, 10..20);
let v2 = create_string_array_with_len::<i32>(1024, 0.0, 20);
let v2 = create_sparse_dict_from_values::<Int32Type>(1024, 0.0, &v2, 30..40);
c.bench_function("concat str_dict_sparse 1024", |b| {
b.iter(|| bench_concat(&v1, &v2))
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

let v1 = FixedSizeListArray::try_new(
Expand All @@ -190,7 +198,7 @@ fn add_benchmark(c: &mut Criterion) {
)
.unwrap();
c.bench_function("concat fixed size lists", |b| {
b.iter(|| bench_concat(&v1, &v2))
b.iter_with_large_drop(|| bench_concat(&v1, &v2))
});

{
Expand Down Expand Up @@ -233,7 +241,7 @@ fn add_benchmark(c: &mut Criterion) {

c.bench_function(
&format!("concat struct with int32 and dicts size={batch_size} count={batch_count}"),
|b| b.iter(|| bench_concat_arrays(&array_refs)),
|b| b.iter_with_large_drop(|| bench_concat_arrays(&array_refs)),
);
}
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy