sunchao commented on code in PR #12024:
URL: https://github.com/apache/arrow-rs/pull/12024#discussion_r4445718870


##########
arrow-buffer/src/builder/boolean.rs:
##########
@@ -276,9 +276,51 @@ impl BooleanBufferBuilder {
         self.advance(additional);
 
         let offset = self.len() - additional;
-        for (i, v) in slice.iter().enumerate() {
+
+        // Too short to fill a whole byte, so packing would only add overhead
+        if additional < 8 {

Review Comment:
   Eight values starting at bit offset 3 still go through `pack_slice`, but 
five values fill the head and three fill the tail, leaving zero whole bytes to 
pack. Repeated eight-value appends preserve that offset. In a preallocated 
builder on AMD EPYC Milan (rustc 1.97.1, release), all-false went from 5.9 
ns/call on main to 10.0 here, and all-true from 11.3 to 14.1. Selecting the 
scalar loop when no aligned byte fits brought those to 7.0 and 11.1 ns/call. 
Could you include this boundary in the timing and consider accounting for 
alignment in the cutoff? The aligned eight-value random case already benefits 
substantially from packing.



##########
arrow-buffer/benches/boolean_buffer_builder.rs:
##########
@@ -0,0 +1,85 @@
+// 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 arrow_buffer::BooleanBufferBuilder;
+use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, 
criterion_main};
+use rand::rngs::StdRng;
+use rand::{RngExt, SeedableRng};
+use std::hint;
+
+const LEN: usize = 64 * 1024;
+
+fn criterion_benchmark(c: &mut Criterion) {
+    let mut rng = StdRng::seed_from_u64(42);
+    let inputs = [
+        ("random", (0..LEN).map(|_| rng.random_bool(0.5)).collect()),
+        (
+            "mostly_true",
+            (0..LEN).map(|_| rng.random_bool(0.9)).collect(),
+        ),
+        ("all_true", vec![true; LEN]),
+    ];
+
+    let mut group = c.benchmark_group("BooleanBufferBuilder append_slice");
+    group.throughput(Throughput::Elements(LEN as u64));
+
+    for (name, values) in &inputs {
+        // An offset of 3 bits makes the slice start in the middle of a byte
+        for offset in [0, 3] {
+            let id = BenchmarkId::new(format!("offset_{offset}"), name);
+            group.bench_with_input(id, values, |b, values: &Vec<bool>| {
+                b.iter(|| {
+                    let mut builder = BooleanBufferBuilder::new(offset + LEN);
+                    builder.append_n(offset, true);
+                    builder.append_slice(hint::black_box(values));
+                    hint::black_box(builder.finish())
+                })
+            });
+        }
+    }
+
+    group.finish();
+
+    // Many appends of 1 to 7 values each, starting in the middle of a byte
+    const CALLS: usize = 7 * 1024;
+    let short_inputs = [
+        ("random", (0..8).map(|_| rng.random_bool(0.5)).collect()),
+        ("all_false", vec![false; 8]),
+    ];
+
+    let mut group = c.benchmark_group("BooleanBufferBuilder append_slice 
short");
+    group.throughput(Throughput::Elements(CALLS as u64));
+
+    for (name, values) in &short_inputs {
+        group.bench_with_input(*name, values, |b, values: &Vec<bool>| {
+            b.iter(|| {
+                let mut builder = BooleanBufferBuilder::new(3 + CALLS * 4);
+                builder.append_n(3, true);
+                for i in 0..CALLS {
+                    let len = i % 7 + 1;
+                    builder.append_slice(hint::black_box(&values[..len]));

Review Comment:
   The short `random` case starts at `values[0]` on every call. Lengths 1 
through 7 therefore repeat the same 28 booleans 1,024 times. In a control with 
identical slice lengths and consecutive memory accesses, that repeating pattern 
took about 6.7 ns/call, versus 12–14 ns/call with independently generated 
values. Could you generate `CALLS * 4` seeded booleans before timing and 
advance a cursor through them? That would make the random row in the PR 
description exercise the workload it describes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to