|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Benchmarks for the run-end filter inner loop (`filter_run_end_primitive`). |
| 5 | +//! |
| 6 | +//! This measures the kernel directly rather than going through the lazy |
| 7 | +//! `ArrayRef::filter` (which only builds a `FilterArray` node and does not run |
| 8 | +//! the kernel). The hot work is a per-run popcount of the predicate mask, which |
| 9 | +//! now uses `BitBuffer::count_range` (SIMD) instead of a bit-by-bit walk. |
| 10 | +
|
| 11 | +#![expect(clippy::cast_possible_truncation)] |
| 12 | +#![expect(clippy::cast_precision_loss)] |
| 13 | +#![expect(clippy::cast_sign_loss)] |
| 14 | +#![expect(clippy::expect_used)] |
| 15 | + |
| 16 | +use std::fmt; |
| 17 | + |
| 18 | +use divan::Bencher; |
| 19 | +use rand::SeedableRng; |
| 20 | +use rand::rngs::StdRng; |
| 21 | +use rand::seq::SliceRandom; |
| 22 | +use vortex_buffer::BitBuffer; |
| 23 | +use vortex_runend::_benchmarking::filter_run_end_primitive; |
| 24 | + |
| 25 | +fn main() { |
| 26 | + divan::main(); |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Clone, Copy)] |
| 30 | +struct FilterBenchArgs { |
| 31 | + /// Total logical length of the decoded array. |
| 32 | + length: usize, |
| 33 | + /// Average run length used when building the run-end array. |
| 34 | + run_length: usize, |
| 35 | + /// Fraction of mask bits that are set to `true`. |
| 36 | + density: f64, |
| 37 | +} |
| 38 | + |
| 39 | +impl fmt::Display for FilterBenchArgs { |
| 40 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | + write!( |
| 42 | + f, |
| 43 | + "len={}_run={}_density={:.1}", |
| 44 | + self.length, self.run_length, self.density |
| 45 | + ) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const FILTER_ARGS: &[FilterBenchArgs] = &[ |
| 50 | + FilterBenchArgs { |
| 51 | + length: 4_096, |
| 52 | + run_length: 16, |
| 53 | + density: 0.1, |
| 54 | + }, |
| 55 | + FilterBenchArgs { |
| 56 | + length: 4_096, |
| 57 | + run_length: 16, |
| 58 | + density: 0.5, |
| 59 | + }, |
| 60 | + FilterBenchArgs { |
| 61 | + length: 4_096, |
| 62 | + run_length: 16, |
| 63 | + density: 0.9, |
| 64 | + }, |
| 65 | + FilterBenchArgs { |
| 66 | + length: 16_384, |
| 67 | + run_length: 16, |
| 68 | + density: 0.1, |
| 69 | + }, |
| 70 | + FilterBenchArgs { |
| 71 | + length: 16_384, |
| 72 | + run_length: 16, |
| 73 | + density: 0.5, |
| 74 | + }, |
| 75 | + FilterBenchArgs { |
| 76 | + length: 16_384, |
| 77 | + run_length: 16, |
| 78 | + density: 0.9, |
| 79 | + }, |
| 80 | +]; |
| 81 | + |
| 82 | +/// Build the run-end boundaries (cumulative run lengths) for `length` rows. |
| 83 | +fn build_run_ends(length: usize, run_length: usize) -> Vec<u32> { |
| 84 | + let n_runs = length.div_ceil(run_length); |
| 85 | + (0..n_runs) |
| 86 | + .map(|r| (((r + 1) * run_length).min(length)) as u32) |
| 87 | + .collect() |
| 88 | +} |
| 89 | + |
| 90 | +/// Build a predicate mask of `length` bits with approximately `density` set bits, |
| 91 | +/// shuffled so the set bits are spread across runs. |
| 92 | +fn build_mask(length: usize, density: f64) -> BitBuffer { |
| 93 | + let n_true = (length as f64 * density).round() as usize; |
| 94 | + let mut bits = vec![false; length]; |
| 95 | + for b in bits.iter_mut().take(n_true) { |
| 96 | + *b = true; |
| 97 | + } |
| 98 | + let mut rng = StdRng::seed_from_u64(0x5eed); |
| 99 | + bits.shuffle(&mut rng); |
| 100 | + BitBuffer::from(bits) |
| 101 | +} |
| 102 | + |
| 103 | +#[divan::bench(args = FILTER_ARGS)] |
| 104 | +fn filter_run_end(bencher: Bencher, args: FilterBenchArgs) { |
| 105 | + let run_ends = build_run_ends(args.length, args.run_length); |
| 106 | + let mask = build_mask(args.length, args.density); |
| 107 | + let length = args.length as u64; |
| 108 | + bencher |
| 109 | + .with_inputs(|| (run_ends.clone(), mask.clone())) |
| 110 | + .bench_refs(|(run_ends, mask)| { |
| 111 | + filter_run_end_primitive::<u32>(run_ends, 0, length, mask).expect("filter") |
| 112 | + }); |
| 113 | +} |
0 commit comments