Fokko commented on code in PR #9426:
URL: https://github.com/apache/arrow-rs/pull/9426#discussion_r2832742355
##########
arrow-array/src/builder/generic_bytes_builder.rs:
##########
@@ -110,6 +110,19 @@ impl<T: ByteArrayType> GenericByteBuilder<T> {
self.offsets_builder.push(self.next_offset());
}
+ /// Appends a value of type `T` into the builder `n` times.
+ ///
+ /// See [`Self::append_value`] for more panic information.
+ #[inline]
+ pub fn append_value_n(&mut self, value: impl AsRef<T::Native>, n: usize) {
+ let bytes = value.as_ref().as_ref();
+ for _ in 0..n {
+ self.value_builder.extend_from_slice(bytes);
Review Comment:
My friend Claude created a benchmark:
```rust
// 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_array::builder::StringBuilder;
use criterion::*;
use std::hint;
fn bench_append_value(c: &mut Criterion) {
let mut group = c.benchmark_group("append_value");
for &str_len in &[5, 30, 1024] {
let value = "x".repeat(str_len);
for &n in &[100, 1000, 10000, 100000] {
group.throughput(Throughput::Elements(n as u64));
group.bench_with_input(
BenchmarkId::new(format!("n={n}"), format!("len={str_len}")),
&(&value, n),
|b, &(value, n)| {
b.iter(|| {
let mut builder = StringBuilder::new();
for _ in 0..n {
builder.append_value(value);
}
hint::black_box(builder.finish());
})
},
);
}
}
group.finish();
}
fn bench_append_value_n(c: &mut Criterion) {
let mut group = c.benchmark_group("append_value_n");
for &str_len in &[5, 30, 1024] {
let value = "x".repeat(str_len);
for &n in &[100, 1000, 10000, 100000] {
group.throughput(Throughput::Elements(n as u64));
group.bench_with_input(
BenchmarkId::new(format!("n={n}"), format!("len={str_len}")),
&(&value, n),
|b, &(value, n)| {
b.iter(|| {
let mut builder = StringBuilder::new();
builder.append_value_n(value, n);
hint::black_box(builder.finish());
})
},
);
}
}
group.finish();
}
criterion_group!(benches, bench_append_value, bench_append_value_n);
criterion_main!(benches);
```
Let me know if you find this valuable to add to the repository.
This resulted in:
```
append_value_n improvements with reserve():
┌────────────────────┬─────────┬─────────┬─────────────┐
│ Config │ Before │ After │ Improvement │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=1000, len=5 │ 2.96 µs │ 2.68 µs │ ~9% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=10000, len=5 │ 29.3 µs │ 26.4 µs │ ~10% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=100000, len=5 │ 268 µs │ 249 µs │ ~7% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=100, len=30 │ 628 ns │ 548 ns │ ~13% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=1000, len=30 │ 4.22 µs │ 3.68 µs │ ~13% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=10000, len=30 │ 43.9 µs │ 36.3 µs │ ~17% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=100000, len=30 │ 404 µs │ 350 µs │ ~13% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=100, len=1024 │ 3.16 µs │ 1.93 µs │ ~39% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=1000, len=1024 │ 13.7 µs │ 11.1 µs │ ~19% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=10000, len=1024 │ 729 µs │ 103 µs │ ~86% │
├────────────────────┼─────────┼─────────┼─────────────┤
│ n=100000, len=1024 │ 8.68 ms │ 1.02 ms │ ~88% │
└────────────────────┴─────────┴─────────┴─────────────┘
```
Keep in mind that the benchmark uses `::new` rather than `::with_capacity`.
We can see that `.reserve` offers some benefits if you don't pre-allocate.
--
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]