Skip to content

Commit d6b0bd7

Browse files
authored
Expose &Arc[Buffer] in GenericByteViewArray::data_buffers for cheaper cloning (#10708)
# Which issue does this PR close? - Related to #10692. # Rationale for this change This change follows directly from the existing ByteView design history: - #6408 traced slow ByteView slicing to allocating and cloning the backing-buffer list. The discussion specifically identified `take` as another operation that would benefit from sharing this list. - #6427 distinguished two related but separate concerns: very large buffer lists may indicate missing GC or deduplication, but avoiding allocation when an operation retains the complete list is independently worthwhile. - #6808 demonstrated why eager deduplication in a general-purpose path is a different tradeoff: it added substantial overhead to common operations, and review favored treating ByteView compaction more holistically. - #9016 subsequently changed `GenericByteViewArray` to store the collection as `Arc<[Buffer]>`, making it possible to share the complete list without allocation. The downstream impact is concrete. [apache/datafusion#16206](apache/datafusion#16206) describes hash joins where concatenating build-side batches produces ByteView payload columns with many backing buffers, and constructing join output repeatedly calls `take`. In that pattern, cloning and later dropping every buffer handle can become a significant part of execution time. Arrow issue #10692 provides a compact multi-stage `take` and `BatchCoalescer` reproduction of the same ownership-metadata amplification. `take_byte_view` and `filter_byte_view` do not yet use this shared representation. They rebuild the collection with `data_buffers().to_vec()`, allocating a new collection and cloning every `Buffer`. This makes the ownership bookkeeping for selection O(number of backing buffers), despite retaining exactly the same complete buffer list. Selection already copies the chosen views while leaving their payloads zero-copy. The cost of retaining the unchanged backing-buffer collection should therefore not grow with the number of entries in that collection. This PR completes that narrow part of the earlier design while leaving buffer canonicalization as a separate problem. # What changes are included in this PR? - Change `GenericByteViewArray::data_buffers()` to return `&Arc<[Buffer]>`, allowing callers to inspect the buffers as before or clone the collection's `Arc` in O(1). - Change `GenericByteViewArray::new_unchecked` to accept `Arc<[Buffer]>` directly, making shared versus newly constructed buffer-list ownership explicit at each call site. - Use `Arc::clone` in the ByteView `take` and `filter` kernels instead of rebuilding the collection. - Replace three additional `data_buffers().to_vec()` call sites found by the stricter constructor signature with shared `Arc` clones. - Verify for both StringView and BinaryView that selection results share the input buffer collection. This PR only removes repeated ownership-metadata cloning. It retains the same complete collection of backing buffers as before. It does not prune or deduplicate buffer entries, remap views, run GC, or copy string/binary payloads. The broader buffer-fragmentation problem described in #10692 remains separate. # Are these changes tested? ```shell cargo fmt --all -- --check cargo check --workspace --all-targets cargo test -p arrow-array -p arrow-select -p arrow-row -p arrow-ipc cargo test -p parquet --test arrow_reader invalid_utf8 cargo clippy -p arrow-array -p arrow-select -p arrow-row -p arrow-ipc -p parquet --all-targets --all-features -- -D warnings cargo doc -p arrow-array --no-deps ``` A temporary local Criterion benchmark was used to validate the asymptotic behavior. It takes 8,192 views distributed across a varying number of buffer entries. The entries share one immutable payload allocation, isolating collection-ownership cost from payload size. `main` and this PR were built in separate Cargo target directories on an Intel Xeon Platinum 8474C: | Buffer entries | `main` | This PR | Speedup | |---:|---:|---:|---:| | 1 | 5.4900 us | 5.4619 us | 1.01x | | 16 | 5.7342 us | 5.4577 us | 1.05x | | 256 | 9.2806 us | 5.4518 us | 1.70x | | 4,096 | 65.774 us | 5.4584 us | 12.05x | Existing normal-size benchmarks did not regress: | Benchmark | `main` | This PR | |---|---:|---:| | `take stringview 512` | 504.73 ns | 439.40 ns | | `take stringview 1024` | 872.36 ns | 773.48 ns | | `filter context mixed string view (kept 1/2)` | 73.406 us | 72.083 us | # Are there any user-facing changes? There are two public signature changes. `data_buffers()` now returns `&Arc<[Buffer]>` instead of `&[Buffer]`; slice methods remain available through deref, while direct `for` iteration can use `.iter()`. `new_unchecked` now requires `Arc<[Buffer]>`; callers constructing a `Vec<Buffer>` can convert it with `.into()`. ByteView selection results now share the immutable backing-buffer collection instead of allocating an equivalent collection. Logical values, null handling, buffer indexes, payload lifetimes, and GC behavior are unchanged. # AI assistance I used OpenAI Codex to help inspect the relevant implementation history, draft the patch and tests, and prepare the benchmark harness and PR text. I reviewed the implementation and benchmark methodology and ran the checks above locally.
1 parent bab2817 commit d6b0bd7

11 files changed

Lines changed: 36 additions & 28 deletions

File tree

‎arrow-array/src/array/byte_view_array.rs‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,11 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
238238
/// # Safety
239239
///
240240
/// Safe if [`Self::try_new`] would not error
241-
pub unsafe fn new_unchecked<U>(
241+
pub unsafe fn new_unchecked(
242242
views: ScalarBuffer<u128>,
243-
buffers: U,
243+
buffers: Arc<[Buffer]>,
244244
nulls: Option<NullBuffer>,
245-
) -> Self
246-
where
247-
U: Into<Arc<[Buffer]>>,
248-
{
245+
) -> Self {
249246
if cfg!(feature = "force_validate") {
250247
return Self::new(views, buffers, nulls);
251248
}
@@ -254,7 +251,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
254251
data_type: T::DATA_TYPE,
255252
phantom: Default::default(),
256253
views,
257-
buffers: buffers.into(),
254+
buffers,
258255
nulls,
259256
}
260257
}
@@ -300,9 +297,13 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
300297
&self.views
301298
}
302299

303-
/// Returns the buffers storing string data
300+
/// Returns the shared collection of buffers storing non-inline string or binary data.
301+
///
302+
/// The returned `Arc` can be cloned to share the buffers with another array without
303+
/// allocating a new collection or cloning the individual buffers. To consume this
304+
/// array and take ownership of its buffers, use [`Self::into_parts`].
304305
#[inline]
305-
pub fn data_buffers(&self) -> &[Buffer] {
306+
pub fn data_buffers(&self) -> &Arc<[Buffer]> {
306307
&self.buffers
307308
}
308309

@@ -543,7 +544,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
543544
return unsafe {
544545
GenericByteViewArray::new_unchecked(
545546
self.views().clone(),
546-
vec![], // empty data blocks
547+
Arc::from([]), // empty data blocks
547548
nulls,
548549
)
549550
};
@@ -558,7 +559,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
558559
return unsafe {
559560
GenericByteViewArray::new_unchecked(
560561
self.views().clone(),
561-
vec![], // empty data blocks
562+
Arc::from([]), // empty data blocks
562563
nulls,
563564
)
564565
};
@@ -657,7 +658,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
657658
let views_scalar = ScalarBuffer::from(views_buf);
658659

659660
// SAFETY: views_scalar, data_blocks, and nulls are correctly aligned and sized
660-
unsafe { GenericByteViewArray::new_unchecked(views_scalar, data_blocks, nulls) }
661+
unsafe { GenericByteViewArray::new_unchecked(views_scalar, data_blocks.into(), nulls) }
661662
}
662663

663664
/// Copy the i‑th view into `data_buf` if it refers to an out‑of‑line buffer.
@@ -1635,7 +1636,7 @@ mod tests {
16351636
gced.data_buffers().len()
16361637
);
16371638
// No output buffer may exceed the cap.
1638-
for buf in gced.data_buffers() {
1639+
for buf in gced.data_buffers().iter() {
16391640
assert!(buf.len() <= max_buffer_size, "buffer exceeded max size");
16401641
}
16411642
// Every value (inline, large, and null) is unchanged and in order.

‎arrow-array/src/builder/generic_bytes_view_builder.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> {
500500
}
501501
let views = std::mem::take(&mut self.views_buffer);
502502
// SAFETY: valid by construction
503-
unsafe { GenericByteViewArray::new_unchecked(views.into(), completed, nulls) }
503+
unsafe { GenericByteViewArray::new_unchecked(views.into(), completed.into(), nulls) }
504504
}
505505

506506
/// Builds the [`GenericByteViewArray`] without resetting the builder
@@ -514,7 +514,7 @@ impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> {
514514
let views = ScalarBuffer::new(views, 0, len);
515515
let nulls = self.null_buffer_builder.finish_cloned();
516516
// SAFETY: valid by construction
517-
unsafe { GenericByteViewArray::new_unchecked(views, completed, nulls) }
517+
unsafe { GenericByteViewArray::new_unchecked(views, completed.into(), nulls) }
518518
}
519519

520520
/// Returns the current null buffer as a slice

‎arrow-array/src/ffi.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ mod tests_from_ffi {
18151815
#[cfg(not(feature = "force_validate"))]
18161816
fn test_utf8_view_ffi_from_dangling_pointer() {
18171817
let empty = GenericByteViewBuilder::<StringViewType>::new().finish();
1818-
let buffers = empty.data_buffers().to_vec();
1818+
let buffers = Arc::clone(empty.data_buffers());
18191819
let nulls = empty.nulls().cloned();
18201820

18211821
// Create a dangling pointer to a view buffer with zero length.

‎arrow-ipc/src/reader.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3604,7 +3604,7 @@ mod tests {
36043604
let array = unsafe {
36053605
StringViewArray::new_unchecked(
36063606
binary_view_array.views().clone(),
3607-
binary_view_array.data_buffers().to_vec(),
3607+
Arc::clone(binary_view_array.data_buffers()),
36083608
binary_view_array.nulls().cloned(),
36093609
)
36103610
};

‎arrow-row/src/variable.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn decode_binary_view_inner<const VALIDATE_UTF8: bool>(
375375

376376
// SAFETY:
377377
// Valid by construction above
378-
unsafe { BinaryViewArray::new_unchecked(views.into(), [values.into()], nulls) }
378+
unsafe { BinaryViewArray::new_unchecked(views.into(), [values.into()].into(), nulls) }
379379
}
380380

381381
/// Decodes a binary view array from `rows` with the provided `options`

‎arrow-select/src/coalesce/byte_view.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,9 @@ impl<B: ByteViewType> InProgressArray for InProgressByteViewArray<B> {
501501

502502
// Safety: we created valid views and buffers above and the
503503
// input arrays had value data and nulls
504-
let new_array =
505-
unsafe { GenericByteViewArray::<B>::new_unchecked(views.into(), buffers, nulls) };
504+
let new_array = unsafe {
505+
GenericByteViewArray::<B>::new_unchecked(views.into(), buffers.into(), nulls)
506+
};
506507
Ok(Arc::new(new_array))
507508
}
508509

‎arrow-select/src/filter.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ fn filter_byte_view<T: ByteViewType>(
934934
) -> GenericByteViewArray<T> {
935935
let new_view_buffer = filter_native(array.views(), predicate);
936936
let views = ScalarBuffer::new(new_view_buffer, 0, predicate.count);
937-
let buffers = array.data_buffers().to_vec();
937+
let buffers = Arc::clone(array.data_buffers());
938938
let nulls = predicate.filter_nulls(array.nulls());
939939

940940
// SAFETY: each view is copied unchanged from `array.views()` and `buffers`
@@ -1297,6 +1297,9 @@ mod tests {
12971297
let actual = filter(&array, &predicate).unwrap();
12981298

12991299
assert_eq!(actual.len(), 3);
1300+
let actual_buffers = actual.as_byte_view::<T>().data_buffers();
1301+
let input_buffers = array.data_buffers();
1302+
assert!(Arc::ptr_eq(actual_buffers, input_buffers));
13001303

13011304
let expected = {
13021305
// ["hello", null, "large payload over 12 bytes"]

‎arrow-select/src/interleave.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ fn interleave_views<T: ByteViewType>(
340340
.collect();
341341

342342
let array = unsafe {
343-
GenericByteViewArray::<T>::new_unchecked(views.into(), buffers, interleaved.nulls)
343+
GenericByteViewArray::<T>::new_unchecked(views.into(), buffers.into(), interleaved.nulls)
344344
};
345345
Ok(Arc::new(array))
346346
}

‎arrow-select/src/take.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,9 @@ fn take_byte_view<T: ByteViewType, IndexType: ArrowPrimitiveType>(
633633
) -> Result<GenericByteViewArray<T>, ArrowError> {
634634
let new_views = take_native(array.views(), indices);
635635
let new_nulls = take_nulls(array.nulls(), indices);
636+
let buffers = Arc::clone(array.data_buffers());
636637
// Safety: array.views was valid, and take_native copies only valid values, and verifies bounds
637-
Ok(unsafe {
638-
GenericByteViewArray::new_unchecked(new_views, array.data_buffers().to_vec(), new_nulls)
639-
})
638+
Ok(unsafe { GenericByteViewArray::new_unchecked(new_views, buffers, new_nulls) })
640639
}
641640

642641
/// `take` implementation for list arrays
@@ -1798,6 +1797,9 @@ mod tests {
17981797
let actual = take(&array, &index, None).unwrap();
17991798

18001799
assert_eq!(actual.len(), index.len());
1800+
let actual_buffers = actual.as_byte_view::<T>().data_buffers();
1801+
let input_buffers = array.data_buffers();
1802+
assert!(Arc::ptr_eq(actual_buffers, input_buffers));
18011803

18021804
let expected = {
18031805
// ["large payload over 12 bytes", null, "world", "large payload over 12 bytes", "lulu", null]

‎parquet/src/arrow/buffer/view_buffer.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ impl ViewBuffer {
5757
let len = self.views.len();
5858
let views = ScalarBuffer::from(self.views);
5959
let nulls = null_buffer.and_then(|b| NullBuffer::from_unsliced_buffer(b, len));
60+
let buffers = self.buffers.into();
6061
match data_type {
6162
ArrowType::Utf8View => {
6263
// Safety: views were created correctly, and checked that the data is utf8 when building the buffer
63-
unsafe { Arc::new(StringViewArray::new_unchecked(views, self.buffers, nulls)) }
64+
unsafe { Arc::new(StringViewArray::new_unchecked(views, buffers, nulls)) }
6465
}
6566
ArrowType::BinaryView => {
6667
// Safety: views were created correctly
67-
unsafe { Arc::new(BinaryViewArray::new_unchecked(views, self.buffers, nulls)) }
68+
unsafe { Arc::new(BinaryViewArray::new_unchecked(views, buffers, nulls)) }
6869
}
6970
_ => panic!("Unsupported data type: {data_type}"),
7071
}

0 commit comments

Comments
 (0)