viirya commented on code in PR #12024: URL: https://github.com/apache/arrow-rs/pull/12024#discussion_r4487712516
########## arrow-buffer/benches/boolean_buffer_builder.rs: ########## @@ -0,0 +1,122 @@ +// 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. + // Lengths cycle through 1 to 7, which adds up to `CALLS * 4` values. + const CALLS: usize = 7 * 1024; + let short_inputs = [ + ( + "random", + (0..CALLS * 4).map(|_| rng.random_bool(0.5)).collect(), + ), + ("all_false", vec![false; CALLS * 4]), + ]; + + 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 + values.len()); + builder.append_n(3, true); + let mut start = 0; + for i in 0..CALLS { + let end = start + i % 7 + 1; + builder.append_slice(hint::black_box(&values[start..end])); + start = end; + } + hint::black_box(builder.finish()) + }) + }); + } + + group.finish(); + + // Many appends of 8 values each. At offset 3 every value goes to the head + // or the remainder, and none is packed. Review Comment: Thanks, updated in 9ae347f1e. ########## arrow-buffer/src/builder/boolean.rs: ########## @@ -276,10 +276,63 @@ impl BooleanBufferBuilder { self.advance(additional); let offset = self.len() - additional; - for (i, v) in slice.iter().enumerate() { - if *v { - unsafe { bit_util::set_bit_raw(self.buffer.as_mut_ptr(), offset + i) } + + // Up to 8 values touch at most two bytes, so pack them directly. This + // includes 8 values at an unaligned offset, which leave no whole byte + // for `pack_slice` to pack. + if additional <= 8 { Review Comment: I tried two versions of that on Apple M4 Max, against the same bench (7168 calls per case unless noted): - Widening the register path to a `u64`, so slices of up to 57 values skip `pack_slice`: 1–8 values got about 40% slower (17–18 → 24–27 µs), and 7168 appends of 9–57 values went from 58 → 69 µs. The fold over 57 values is a serial shift/OR chain, while `pack_slice` does one multiply per 8 values. - Packing 64 bools into a `u64` and storing it whole in `pack_slice`: 64Ki values went from 1.21–1.27 → 1.28–1.32 µs. The current loop already compiles to an unrolled multiply-and-store with no bounds checks, so combining the bytes only adds shifts and ORs. So I kept the 8-value cutoff and the byte-at-a-time loop. ########## arrow-buffer/src/builder/boolean.rs: ########## @@ -276,10 +276,63 @@ impl BooleanBufferBuilder { self.advance(additional); let offset = self.len() - additional; - for (i, v) in slice.iter().enumerate() { - if *v { - unsafe { bit_util::set_bit_raw(self.buffer.as_mut_ptr(), offset + i) } + + // Up to 8 values touch at most two bytes, so pack them directly. This + // includes 8 values at an unaligned offset, which leave no whole byte + // for `pack_slice` to pack. + if additional <= 8 { + if additional == 0 { + return; + } + // At most 8 bits starting at bit 7 end at bit 14, so they span at most two bytes + let bits = (Self::pack_bits(slice) as u16) << (offset % 8); + let buffer = self.buffer.as_slice_mut(); Review Comment: Yes, `get_unchecked_mut` works here since `advance` has already grown the buffer to hold bit `offset`. On Apple M4 Max it saved up to about 5% on 8-value appends at offset 3 (18.3 → 17.4 µs) and 1–2% on the 1–7 value case; the other cases were within noise. Given that, I'd rather keep this path free of `unsafe`, but I'm happy to add it if you think it's worth it. -- 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]
