You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/06/10 16:43:11 UTC

(datafusion) 01/01: perf: fast-path inline strings in ByteViewGroupValueBuilder::vectorized_append (#21794)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-21794-fdf011b2c50035c5667cbdf5256446d13d8bb88b
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit 05490a327ac0d09107595607080b4493647ddaa3
Author: EeshanBembi <33...@users.noreply.github.com>
AuthorDate: Wed Jun 10 22:12:56 2026 +0530

    perf: fast-path inline strings in ByteViewGroupValueBuilder::vectorized_append (#21794)
    
    ## Which issue does this PR close?
    
    - Closes #21568.
    
    ## Rationale for this change
    
    `ByteViewGroupValueBuilder::vectorized_append` was doing unnecessary
    work for short strings (≤12 bytes): for each row it called
    `array.value(row)` to decode the u128 view into a `&[u8]`, then called
    `make_view` to re-encode it back into a u128. The input
    `GenericByteViewArray` already stores inline values in exactly that u128
    format, so the round-trip is redundant.
    
    This mirrors the existing `HAS_BUFFERS` specialisation in
    `vectorized_equal_to_inner`, which uses the same
    `data_buffers().is_empty()` guard to take a direct-view-compare fast
    path for inline strings.
    
    ## What changes are included in this PR?
    
    In `vectorized_append_inner`, the `Nulls::None` branch now dispatches on
    `arr.data_buffers().is_empty()`:
    
    - **Fast path** (no data buffers → all values ≤12 bytes inline): copies
    u128 views directly via `self.views.extend(rows.iter().map(|&row|
    arr.views()[row]))`. Arrow's validity invariant guarantees inline views
    are zero-padded, so direct copy is semantically identical to `value() →
    make_view()`.
    - **Slow path** (array has non-inline strings): adds
    `self.views.reserve(rows.len())` before the existing loop to avoid
    repeated reallocation.
    
    ## Are these changes tested?
    
    Covered by the existing 6 unit tests in `bytes_view::tests`, all passing
    unchanged. `test_byte_view_vectorized_operation_special_case` exercises
    the fast path directly (11-byte strings, no data buffers).
    
    ## Are there any user-facing changes?
    
    No. Internal performance improvement only.
    
    ## Benchmark
    
    `inline_null_0.0_size_1000/vectorized_append` (8-byte strings, no nulls,
    1 000 rows):
    
    | | time |
    |---|---|
    | Before | 3.37 µs |
    | After | 495 ns |
    | Change | **−85.3% (6.8× faster)** |
---
 datafusion/physical-plan/Cargo.toml                |  1 -
 .../physical-plan/benches/aggregate_vectorized.rs  | 15 +++---
 .../group_values/multi_group_by/bytes_view.rs      | 56 ++++++++++++++++++++--
 3 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/datafusion/physical-plan/Cargo.toml b/datafusion/physical-plan/Cargo.toml
index 0fc75043bf..4b2b31febe 100644
--- a/datafusion/physical-plan/Cargo.toml
+++ b/datafusion/physical-plan/Cargo.toml
@@ -123,7 +123,6 @@ required-features = ["test_utils"]
 [[bench]]
 harness = false
 name = "aggregate_vectorized"
-required-features = ["test_utils"]
 
 [[bench]]
 harness = false
diff --git a/datafusion/physical-plan/benches/aggregate_vectorized.rs b/datafusion/physical-plan/benches/aggregate_vectorized.rs
index 48ca76d80d..488647d5f8 100644
--- a/datafusion/physical-plan/benches/aggregate_vectorized.rs
+++ b/datafusion/physical-plan/benches/aggregate_vectorized.rs
@@ -21,7 +21,6 @@ use arrow::util::bench_util::{
     create_primitive_array, create_string_view_array_with_len,
     create_string_view_array_with_max_len,
 };
-use arrow::util::test_util::seedable_rng;
 use arrow_schema::DataType;
 use criterion::measurement::WallTime;
 use criterion::{
@@ -30,7 +29,9 @@ use criterion::{
 use datafusion_physical_plan::aggregates::group_values::multi_group_by::GroupColumn;
 use datafusion_physical_plan::aggregates::group_values::multi_group_by::bytes_view::ByteViewGroupValueBuilder;
 use datafusion_physical_plan::aggregates::group_values::multi_group_by::primitive::PrimitiveGroupValueBuilder;
+use rand::SeedableRng;
 use rand::distr::{Bernoulli, Distribution};
+use rand::rngs::StdRng;
 use std::hint::black_box;
 use std::sync::Arc;
 
@@ -128,7 +129,7 @@ fn bytes_bench(
         input,
         "0.75 true",
         {
-            let mut rng = seedable_rng();
+            let mut rng = StdRng::seed_from_u64(42);
             let d = Bernoulli::new(0.75).unwrap();
             (0..size).map(|_| d.sample(&mut rng)).collect::<Vec<_>>()
         },
@@ -141,7 +142,7 @@ fn bytes_bench(
         input,
         "0.5 true",
         {
-            let mut rng = seedable_rng();
+            let mut rng = StdRng::seed_from_u64(42);
             let d = Bernoulli::new(0.5).unwrap();
             (0..size).map(|_| d.sample(&mut rng)).collect::<Vec<_>>()
         },
@@ -154,7 +155,7 @@ fn bytes_bench(
         input,
         "0.25 true",
         {
-            let mut rng = seedable_rng();
+            let mut rng = StdRng::seed_from_u64(42);
             let d = Bernoulli::new(0.25).unwrap();
             (0..size).map(|_| d.sample(&mut rng)).collect::<Vec<_>>()
         },
@@ -236,7 +237,7 @@ fn bench_single_primitive<const NULLABLE: bool>(
         &input,
         "0.75 true",
         {
-            let mut rng = seedable_rng();
+            let mut rng = StdRng::seed_from_u64(42);
             let d = Bernoulli::new(0.75).unwrap();
             (0..size).map(|_| d.sample(&mut rng)).collect::<Vec<_>>()
         },
@@ -249,7 +250,7 @@ fn bench_single_primitive<const NULLABLE: bool>(
         &input,
         "0.5 true",
         {
-            let mut rng = seedable_rng();
+            let mut rng = StdRng::seed_from_u64(42);
             let d = Bernoulli::new(0.5).unwrap();
             (0..size).map(|_| d.sample(&mut rng)).collect::<Vec<_>>()
         },
@@ -262,7 +263,7 @@ fn bench_single_primitive<const NULLABLE: bool>(
         &input,
         "0.25 true",
         {
-            let mut rng = seedable_rng();
+            let mut rng = StdRng::seed_from_u64(42);
             let d = Bernoulli::new(0.25).unwrap();
             (0..size).map(|_| d.sample(&mut rng)).collect::<Vec<_>>()
         },
diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs
index e94e4547e1..abc3aba88a 100644
--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs
+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs
@@ -145,7 +145,11 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {
         }
     }
 
-    fn vectorized_append_inner(&mut self, array: &ArrayRef, rows: &[usize]) {
+    fn vectorized_append_inner(
+        &mut self,
+        array: &ArrayRef,
+        rows: &[usize],
+    ) -> Result<()> {
         let arr = array.as_byte_view::<B>();
         let null_count = array.null_count();
         let num_rows = array.len();
@@ -166,8 +170,50 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {
 
             Nulls::None => {
                 self.nulls.append_n(rows.len(), false);
-                for &row in rows {
-                    self.do_append_val_inner(arr, row);
+                if arr.data_buffers().is_empty() {
+                    // Fast path: all strings are inline (≤12 bytes).
+                    // The input array's u128 views are already in the correct format;
+                    // copy them directly instead of going through value() → make_view().
+                    self.views.extend(rows.iter().map(|&row| arr.views()[row]));
+                } else {
+                    // Slow path: some strings are non-inline (>12 bytes).
+                    // Read views directly to avoid array.value(row) overhead and
+                    // reuse the source view's prefix instead of recomputing it via make_view.
+                    self.views.try_reserve(rows.len()).map_err(|e| {
+                        datafusion_common::exec_datafusion_err!(
+                            "failed to reserve {0} views: {e}",
+                            rows.len()
+                        )
+                    })?;
+                    for &row in rows {
+                        let view = arr.views()[row];
+                        let len = view as u32;
+                        if len <= 12 {
+                            // This row happens to be inline; copy view directly.
+                            self.views.push(view);
+                        } else {
+                            let src = ByteView::from(view);
+                            // ensure_in_progress_big_enough must be called before computing
+                            // new_buffer_index / new_offset — it may flush in_progress to completed.
+                            self.ensure_in_progress_big_enough(len as usize);
+                            let new_buffer_index = self.completed.len() as u32;
+                            let new_offset = self.in_progress.len() as u32;
+                            let src_buf = &arr.data_buffers()[src.buffer_index as usize];
+                            self.in_progress.extend_from_slice(
+                                &src_buf[src.offset as usize
+                                    ..(src.offset + src.length) as usize],
+                            );
+                            // Reuse prefix from the source view — avoids re-reading first 4 bytes.
+                            let new_view = ByteView {
+                                length: src.length,
+                                prefix: src.prefix,
+                                buffer_index: new_buffer_index,
+                                offset: new_offset,
+                            }
+                            .as_u128();
+                            self.views.push(new_view);
+                        }
+                    }
                 }
             }
 
@@ -177,6 +223,7 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {
                 self.views.resize(new_len, 0);
             }
         }
+        Ok(())
     }
 
     fn do_append_val_inner(&mut self, array: &GenericByteViewArray<B>, row: usize)
@@ -548,8 +595,7 @@ impl<B: ByteViewType> GroupColumn for ByteViewGroupValueBuilder<B> {
     }
 
     fn vectorized_append(&mut self, array: &ArrayRef, rows: &[usize]) -> Result<()> {
-        self.vectorized_append_inner(array, rows);
-        Ok(())
+        self.vectorized_append_inner(array, rows)
     }
 
     fn len(&self) -> usize {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org