|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![allow(clippy::unwrap_used)] |
| 5 | +#![allow(unexpected_cfgs)] |
| 6 | + |
| 7 | +use divan::Bencher; |
| 8 | +use mimalloc::MiMalloc; |
| 9 | +use rand::prelude::StdRng; |
| 10 | +use rand::{Rng, SeedableRng}; |
| 11 | +use vortex_array::arrays::PrimitiveArray; |
| 12 | +use vortex_fastlanes::BitPackedArray; |
| 13 | + |
| 14 | +#[global_allocator] |
| 15 | +static GLOBAL: MiMalloc = MiMalloc; |
| 16 | + |
| 17 | +pub fn main() { |
| 18 | + divan::main(); |
| 19 | +} |
| 20 | + |
| 21 | +// Cross product of NUM_ELEMENTS and VALIDITY_PCT. |
| 22 | +const BENCH_PARAMS: &[(usize, f64)] = &[ |
| 23 | + (1_000, 0.5), |
| 24 | + (1_000, 1.0), |
| 25 | + (10_000, 0.5), |
| 26 | + (10_000, 1.0), |
| 27 | + (100_000, 0.5), |
| 28 | + (100_000, 1.0), |
| 29 | + (1_000_000, 0.5), |
| 30 | + (1_000_000, 1.0), |
| 31 | + (10_000_000, 0.5), |
| 32 | + (10_000_000, 1.0), |
| 33 | +]; |
| 34 | + |
| 35 | +#[divan::bench(args = BENCH_PARAMS)] |
| 36 | +fn bitpack_pipeline_unpack(bencher: Bencher, (num_elements, validity_pct): (usize, f64)) { |
| 37 | + bencher |
| 38 | + .with_inputs(|| { |
| 39 | + let mut rng = StdRng::seed_from_u64(42); |
| 40 | + |
| 41 | + // Create array with randomized validity. |
| 42 | + // Keep values small enough to fit in the bit width (0-1023 for 10 bits). |
| 43 | + let values = (0..num_elements).map(|_| { |
| 44 | + let is_valid = rng.random_bool(validity_pct); |
| 45 | + is_valid.then(|| rng.random_range(0u32..1024)) |
| 46 | + }); |
| 47 | + |
| 48 | + let primitive = PrimitiveArray::from_option_iter(values).to_array(); |
| 49 | + |
| 50 | + // Encode with 10-bit width (supports values up to 1023). |
| 51 | + let bitpacked = BitPackedArray::encode(&primitive, 10).unwrap(); |
| 52 | + |
| 53 | + bitpacked.to_array() |
| 54 | + }) |
| 55 | + .bench_local_values(|array| array.execute().unwrap()); |
| 56 | +} |
| 57 | + |
| 58 | +#[divan::bench(args = BENCH_PARAMS)] |
| 59 | +fn bitpack_canonical_unpack(bencher: Bencher, (num_elements, validity_pct): (usize, f64)) { |
| 60 | + bencher |
| 61 | + .with_inputs(|| { |
| 62 | + let mut rng = StdRng::seed_from_u64(42); |
| 63 | + |
| 64 | + // Create array with randomized validity. |
| 65 | + // Keep values small enough to fit in the bit width (0-1023 for 10 bits). |
| 66 | + let values = (0..num_elements).map(|_| { |
| 67 | + let is_valid = rng.random_bool(validity_pct); |
| 68 | + is_valid.then(|| rng.random_range(0u32..1024)) |
| 69 | + }); |
| 70 | + |
| 71 | + let primitive = PrimitiveArray::from_option_iter(values).to_array(); |
| 72 | + |
| 73 | + // Encode with 10-bit width (supports values up to 1023). |
| 74 | + let bitpacked = BitPackedArray::encode(&primitive, 10).unwrap(); |
| 75 | + |
| 76 | + bitpacked.to_array() |
| 77 | + }) |
| 78 | + .bench_local_values(|array| array.to_canonical()); |
| 79 | +} |
0 commit comments