Skip to content

Commit cb52c89

Browse files
0ax1a10y
authored andcommitted
chore: benchmark hygene (#5499)
- black box via `with_inputs` - minimize work done in `with_inputs` to minimize total time to run the benchmarks - only `clone` the input if the argument is consumed by the benchmarked function - pass argument by ref `bench_refs` to exclude `drop` from being benchmarked --------- Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
1 parent 40b139c commit cb52c89

26 files changed

+181
-196
lines changed

encodings/alp/benches/alp_compress.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ fn compress_alp<T: ALPFloat + NativePType>(bencher: Bencher, args: (usize, f64,
6464
Validity::NonNullable
6565
};
6666
let values = values.freeze();
67+
let array = PrimitiveArray::new(values, validity);
6768

6869
bencher
69-
.with_inputs(|| (values.clone(), validity.clone()))
70-
.bench_values(|(values, validity)| {
71-
alp_encode(&PrimitiveArray::new(values, validity), None).unwrap()
72-
})
70+
.with_inputs(|| &array)
71+
.bench_values(|array| alp_encode(array, None).unwrap())
7372
}
7473

7574
#[divan::bench(types = [f32, f64], args = BENCH_ARGS)]
@@ -105,7 +104,10 @@ fn decompress_alp<T: ALPFloat + NativePType>(bencher: Bencher, args: (usize, f64
105104
fn compress_rd<T: ALPRDFloat>(bencher: Bencher, n: usize) {
106105
let primitive = PrimitiveArray::new(buffer![T::from(1.23).unwrap(); n], Validity::NonNullable);
107106
let encoder = RDEncoder::new(&[T::from(1.23).unwrap()]);
108-
bencher.bench(|| encoder.encode(&primitive));
107+
108+
bencher
109+
.with_inputs(|| (&primitive, &encoder))
110+
.bench_refs(|(primitive, encoder)| encoder.encode(primitive))
109111
}
110112

111113
#[divan::bench(types = [f32, f64], args = [10_000, 100_000])]
@@ -115,6 +117,6 @@ fn decompress_rd<T: ALPRDFloat>(bencher: Bencher, n: usize) {
115117
let encoded = encoder.encode(&primitive);
116118

117119
bencher
118-
.with_inputs(move || encoded.clone())
119-
.bench_values(|encoded| encoded.to_canonical());
120+
.with_inputs(|| &encoded)
121+
.bench_refs(|encoded| encoded.to_canonical());
120122
}

encodings/fastlanes/benches/compute_between.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ mod primitive {
9393
let mut rng = StdRng::seed_from_u64(0);
9494
let arr = generate_primitive_array::<T>(&mut rng, len);
9595

96-
bencher.with_inputs(|| arr.clone()).bench_refs(|arr| {
96+
bencher.with_inputs(|| &arr).bench_refs(|arr| {
9797
boolean(
9898
&compare(
9999
arr.as_ref(),
@@ -127,7 +127,7 @@ mod primitive {
127127
let mut rng = StdRng::seed_from_u64(0);
128128
let arr = generate_primitive_array::<T>(&mut rng, len);
129129

130-
bencher.with_inputs(|| arr.clone()).bench_refs(|arr| {
130+
bencher.with_inputs(|| &arr).bench_refs(|arr| {
131131
between(
132132
arr.as_ref(),
133133
ConstantArray::new(min, arr.len()).as_ref(),
@@ -175,7 +175,7 @@ mod bitpack {
175175
let mut rng = StdRng::seed_from_u64(0);
176176
let arr = generate_bit_pack_primitive_array::<T>(&mut rng, len);
177177

178-
bencher.with_inputs(|| arr.clone()).bench_refs(|arr| {
178+
bencher.with_inputs(|| &arr).bench_refs(|arr| {
179179
boolean(
180180
&compare(
181181
arr.as_ref(),
@@ -208,7 +208,7 @@ mod bitpack {
208208
let mut rng = StdRng::seed_from_u64(0);
209209
let arr = generate_bit_pack_primitive_array::<T>(&mut rng, len);
210210

211-
bencher.with_inputs(|| arr.clone()).bench_refs(|arr| {
211+
bencher.with_inputs(|| &arr).bench_refs(|arr| {
212212
between(
213213
arr.as_ref(),
214214
ConstantArray::new(min, arr.len()).as_ref(),

encodings/fastlanes/benches/pipeline_bitpacking_compare_scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ pub fn eval<T: NativePType + Into<Scalar>>(bencher: Bencher, fraction_kept: f64)
7272

7373
bencher
7474
// Be sure to reconstruct the mask to avoid cached set_indices
75-
.with_inputs(|| (Mask::from_buffer(mask.clone()), array.clone()))
76-
.bench_refs(|(mask, array)| {
75+
.with_inputs(|| (&array, Mask::from_buffer(mask.clone())))
76+
.bench_refs(|(array, mask)| {
7777
// We run the filter first, then compare.
7878
let array = filter(array.as_ref(), mask).unwrap();
7979
expr.evaluate(&array).unwrap().to_canonical()

encodings/fastlanes/benches/pipeline_rle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn compress(bencher: Bencher, (length, run_step): (usize, usize)) {
4848
);
4949

5050
bencher
51-
.with_inputs(|| values.clone())
51+
.with_inputs(|| &values)
5252
.bench_refs(|values| RLEArray::encode(values).unwrap());
5353
}
5454

encodings/fastlanes/benches/pipeline_v2_bitpacking_basic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ fn bitpack_pipeline_unpack(bencher: Bencher, (num_elements, validity_pct): (usiz
4444

4545
// Encode with 10-bit width (supports values up to 1023).
4646
let bitpacked = BitPackedArray::encode(&primitive, 10).unwrap();
47+
let array = bitpacked.to_array();
4748

4849
bencher
49-
.with_inputs(|| bitpacked.to_array())
50+
.with_inputs(|| &array)
5051
.bench_refs(|array| array.execute().unwrap());
5152
}
5253

encodings/fsst/benches/chunked_dict_fsst_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn chunked_dict_fsst_canonical_into(
4343
) {
4444
let chunk = make_dict_fsst_chunks::<u16>(len, unique_values, chunk_count);
4545

46-
bencher.with_inputs(|| chunk.clone()).bench_values(|chunk| {
46+
bencher.with_inputs(|| &chunk).bench_refs(|chunk| {
4747
let mut builder = builder_with_capacity(chunk.dtype(), len * chunk_count);
4848
chunk.append_to_builder(builder.as_mut());
4949
builder.finish()
@@ -58,6 +58,6 @@ fn chunked_dict_fsst_into_canonical(
5858
let chunk = make_dict_fsst_chunks::<u16>(len, unique_values, chunk_count);
5959

6060
bencher
61-
.with_inputs(|| chunk.clone())
62-
.bench_values(|chunk| chunk.to_canonical())
61+
.with_inputs(|| &chunk)
62+
.bench_refs(|chunk| chunk.to_canonical())
6363
}

encodings/fsst/benches/fsst_compress.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ const BENCH_ARGS: &[(usize, usize, u8)] = &[
4747
fn compress_fsst(bencher: Bencher, (string_count, avg_len, unique_chars): (usize, usize, u8)) {
4848
let array = generate_test_data(string_count, avg_len, unique_chars);
4949
let compressor = fsst_train_compressor(&array);
50-
bencher.bench(|| fsst_compress(&array, &compressor))
50+
bencher
51+
.with_inputs(|| (&array, &compressor))
52+
.bench_refs(|(array, compressor)| fsst_compress(*array, compressor))
5153
}
5254

5355
#[divan::bench(args = BENCH_ARGS)]
@@ -57,14 +59,16 @@ fn decompress_fsst(bencher: Bencher, (string_count, avg_len, unique_chars): (usi
5759
let encoded = fsst_compress(array, &compressor);
5860

5961
bencher
60-
.with_inputs(|| encoded.clone())
61-
.bench_values(|encoded| encoded.to_canonical())
62+
.with_inputs(|| &encoded)
63+
.bench_refs(|encoded| encoded.to_canonical())
6264
}
6365

6466
#[divan::bench(args = BENCH_ARGS)]
6567
fn train_compressor(bencher: Bencher, (string_count, avg_len, unique_chars): (usize, usize, u8)) {
6668
let array = generate_test_data(string_count, avg_len, unique_chars);
67-
bencher.bench(|| fsst_train_compressor(&array))
69+
bencher
70+
.with_inputs(|| &array)
71+
.bench_refs(|array| fsst_train_compressor(array))
6872
}
6973

7074
#[divan::bench(args = BENCH_ARGS)]
@@ -75,7 +79,7 @@ fn pushdown_compare(bencher: Bencher, (string_count, avg_len, unique_chars): (us
7579
let constant = ConstantArray::new(Scalar::from(&b"const"[..]), array.len());
7680

7781
bencher
78-
.with_inputs(|| (fsst_array.clone(), constant.clone()))
82+
.with_inputs(|| (&fsst_array, &constant))
7983
.bench_refs(|(fsst_array, constant)| {
8084
compare(fsst_array.as_ref(), constant.as_ref(), Operator::Eq).unwrap();
8185
})
@@ -92,7 +96,7 @@ fn canonicalize_compare(
9296
let constant = ConstantArray::new(Scalar::from(&b"const"[..]), array.len());
9397

9498
bencher
95-
.with_inputs(|| (fsst_array.clone(), constant.clone()))
99+
.with_inputs(|| (&fsst_array, &constant))
96100
.bench_refs(|(fsst_array, constant)| {
97101
compare(
98102
fsst_array.to_canonical().as_ref(),
@@ -123,7 +127,7 @@ fn chunked_canonicalize_into(
123127
) {
124128
let array = generate_chunked_test_data(chunk_size, string_count, avg_len, unique_chars);
125129

126-
bencher.with_inputs(|| array.clone()).bench_values(|array| {
130+
bencher.with_inputs(|| &array).bench_refs(|array| {
127131
let mut builder =
128132
VarBinViewBuilder::with_capacity(DType::Binary(Nullability::NonNullable), array.len());
129133
array.append_to_builder(&mut builder);
@@ -139,8 +143,8 @@ fn chunked_into_canonical(
139143
let array = generate_chunked_test_data(chunk_size, string_count, avg_len, unique_chars);
140144

141145
bencher
142-
.with_inputs(|| array.clone())
143-
.bench_values(|array| array.to_canonical());
146+
.with_inputs(|| &array)
147+
.bench_refs(|array| array.to_canonical());
144148
}
145149

146150
/// Helper function to generate random string data.

encodings/pco/benches/pco.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ pub fn pco_pipeline(bencher: Bencher, (size, selectivity): (usize, f64)) {
5454
.collect::<BitBuffer>();
5555

5656
bencher
57-
.with_inputs(|| (Mask::from_buffer(mask.clone()), pco_array.clone()))
58-
.bench_refs(|(mask, pco_array)| pco_array.execute_with_selection(mask).unwrap());
57+
// Be sure to reconstruct the mask to avoid cached set_indices
58+
.with_inputs(|| (&pco_array, Mask::from_buffer(mask.clone())))
59+
.bench_refs(|(pco_array, mask)| pco_array.execute_with_selection(mask).unwrap());
5960
}
6061

6162
#[divan::bench(args = [
@@ -87,6 +88,7 @@ pub fn pco_canonical(bencher: Bencher, (size, selectivity): (usize, f64)) {
8788
.collect::<BitBuffer>();
8889

8990
bencher
90-
.with_inputs(|| (Mask::from_buffer(mask.clone()), pco_array.clone()))
91-
.bench_refs(|(mask, pco_array)| filter(pco_array.to_canonical().as_ref(), mask).unwrap());
91+
// Be sure to reconstruct the mask to avoid cached set_indices
92+
.with_inputs(|| (&pco_array, Mask::from_buffer(mask.clone())))
93+
.bench_refs(|(pco_array, mask)| filter(pco_array.to_canonical().as_ref(), mask).unwrap());
9294
}

encodings/runend/benches/run_end_compress.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn compress(bencher: Bencher, (length, run_step): (usize, usize)) {
5353
);
5454

5555
bencher
56-
.with_inputs(|| values.clone())
56+
.with_inputs(|| &values)
5757
.bench_refs(|values| runend_encode(values));
5858
}
5959

@@ -71,10 +71,11 @@ fn decompress<T: IntegerPType>(bencher: Bencher, (length, run_step): (usize, usi
7171
.into_array();
7272

7373
let run_end_array = RunEndArray::new(ends, values);
74+
let array = run_end_array.to_array();
7475

7576
bencher
76-
.with_inputs(|| run_end_array.to_array())
77-
.bench_values(|array| array.to_canonical());
77+
.with_inputs(|| &array)
78+
.bench_refs(|array| array.to_canonical());
7879
}
7980

8081
#[divan::bench(args = BENCH_ARGS)]
@@ -90,9 +91,11 @@ fn take_indices(bencher: Bencher, (length, run_step): (usize, usize)) {
9091

9192
let source_array = PrimitiveArray::from_iter(0..(length as i32)).into_array();
9293
let (ends, values) = runend_encode(&values);
93-
let runend_array = RunEndArray::try_new(ends.into_array(), values).unwrap();
94+
let runend_array = RunEndArray::try_new(ends.into_array(), values)
95+
.unwrap()
96+
.to_array();
9497

9598
bencher
96-
.with_inputs(|| (source_array.clone(), runend_array.to_array()))
97-
.bench_refs(|(array, indices)| take(array, indices).unwrap());
99+
.with_inputs(|| (&source_array, &runend_array))
100+
.bench_refs(|(array, indices)| take(array.as_ref(), indices.as_ref()).unwrap());
98101
}

encodings/runend/benches/run_end_null_count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn null_count_run_end(bencher: Bencher, (n, run_step, valid_density): (usize, us
5252
let array = fixture(n, run_step, valid_density).into_array();
5353

5454
bencher
55-
.with_inputs(|| array.clone())
55+
.with_inputs(|| &array)
5656
.bench_refs(|array| array.invalid_count());
5757
}
5858

0 commit comments

Comments
 (0)