|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![expect(clippy::unwrap_used)] |
| 5 | + |
| 6 | +use std::sync::LazyLock; |
| 7 | + |
| 8 | +use divan::Bencher; |
| 9 | +use vortex_array::ArrayRef; |
| 10 | +use vortex_array::IntoArray; |
| 11 | +use vortex_array::VortexSessionExecute; |
| 12 | +use vortex_array::aggregate_fn::DynGroupedAccumulator; |
| 13 | +use vortex_array::aggregate_fn::GroupedAccumulator; |
| 14 | +use vortex_array::aggregate_fn::NumericalAggregateOpts; |
| 15 | +use vortex_array::aggregate_fn::fns::sum_v2::SumV2; |
| 16 | +use vortex_array::aggregate_fn::fns::sum_v2::sum_v2; |
| 17 | +use vortex_array::arrays::FixedSizeListArray; |
| 18 | +use vortex_array::arrays::PrimitiveArray; |
| 19 | +use vortex_array::validity::Validity; |
| 20 | +use vortex_buffer::Buffer; |
| 21 | +use vortex_runend::RunEnd; |
| 22 | +use vortex_session::VortexSession; |
| 23 | + |
| 24 | +const LEN: usize = 2_048; |
| 25 | +const RUN_LENGTH: usize = 64; |
| 26 | + |
| 27 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { |
| 28 | + let session = vortex_array::array_session(); |
| 29 | + vortex_runend::initialize(&session); |
| 30 | + session |
| 31 | +}); |
| 32 | + |
| 33 | +fn main() { |
| 34 | + LazyLock::force(&SESSION); |
| 35 | + divan::main(); |
| 36 | +} |
| 37 | + |
| 38 | +fn runend_with_null_runs() -> ArrayRef { |
| 39 | + let ends = |
| 40 | + PrimitiveArray::from_iter((RUN_LENGTH..=LEN).step_by(RUN_LENGTH).map(|end| end as u64)); |
| 41 | + let values = PrimitiveArray::from_option_iter( |
| 42 | + (0..ends.len()) |
| 43 | + .map(|index| (index % 5 != 0).then_some(i32::try_from(index % 100).unwrap())), |
| 44 | + ); |
| 45 | + RunEnd::try_new( |
| 46 | + ends.into_array(), |
| 47 | + values.into_array(), |
| 48 | + &mut SESSION.create_execution_ctx(), |
| 49 | + ) |
| 50 | + .unwrap() |
| 51 | + .into_array() |
| 52 | +} |
| 53 | + |
| 54 | +fn bench_fixed_size_group_sum(bencher: Bencher, elements: ArrayRef, group_size: u32) { |
| 55 | + let dtype = elements.dtype().clone(); |
| 56 | + let groups = FixedSizeListArray::try_new( |
| 57 | + elements, |
| 58 | + group_size, |
| 59 | + Validity::NonNullable, |
| 60 | + LEN / group_size as usize, |
| 61 | + ) |
| 62 | + .unwrap() |
| 63 | + .into_array(); |
| 64 | + bencher |
| 65 | + .with_inputs(|| { |
| 66 | + ( |
| 67 | + GroupedAccumulator::try_new( |
| 68 | + SumV2, |
| 69 | + NumericalAggregateOpts::default(), |
| 70 | + dtype.clone(), |
| 71 | + ) |
| 72 | + .unwrap(), |
| 73 | + SESSION.create_execution_ctx(), |
| 74 | + ) |
| 75 | + }) |
| 76 | + .bench_refs(|(acc, ctx)| { |
| 77 | + acc.accumulate_list(&groups, ctx).unwrap(); |
| 78 | + acc.finish() |
| 79 | + .unwrap() |
| 80 | + .execute::<PrimitiveArray>(ctx) |
| 81 | + .unwrap() |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +#[divan::bench] |
| 86 | +fn whole_array_sum_partially_valid(bencher: Bencher) { |
| 87 | + let array = runend_with_null_runs(); |
| 88 | + bencher |
| 89 | + .with_inputs(|| SESSION.create_execution_ctx()) |
| 90 | + .bench_refs(|ctx| sum_v2(&array, ctx).unwrap()); |
| 91 | +} |
| 92 | + |
| 93 | +#[divan::bench(args = [Validity::AllValid, Validity::AllInvalid])] |
| 94 | +fn whole_array_sum_uniform_validity(bencher: Bencher, validity: &Validity) { |
| 95 | + let array = runend_with_validity(validity); |
| 96 | + |
| 97 | + bencher |
| 98 | + .with_inputs(|| SESSION.create_execution_ctx()) |
| 99 | + .bench_refs(|ctx| sum_v2(&array, ctx).unwrap()); |
| 100 | +} |
| 101 | + |
| 102 | +#[divan::bench(consts = [2, 128])] |
| 103 | +fn fixed_size_group_sum_partially_valid<const GROUP_SIZE: u32>(bencher: Bencher) { |
| 104 | + bench_fixed_size_group_sum(bencher, runend_with_null_runs(), GROUP_SIZE); |
| 105 | +} |
| 106 | + |
| 107 | +fn runend_with_validity(validity: &Validity) -> ArrayRef { |
| 108 | + let ends = |
| 109 | + PrimitiveArray::from_iter((RUN_LENGTH..=LEN).step_by(RUN_LENGTH).map(|end| end as u64)); |
| 110 | + let values = PrimitiveArray::new( |
| 111 | + (0..ends.len()) |
| 112 | + .map(|index| i32::try_from(index).unwrap()) |
| 113 | + .collect::<Buffer<_>>(), |
| 114 | + validity.clone(), |
| 115 | + ); |
| 116 | + RunEnd::try_new( |
| 117 | + ends.into_array(), |
| 118 | + values.into_array(), |
| 119 | + &mut SESSION.create_execution_ctx(), |
| 120 | + ) |
| 121 | + .unwrap() |
| 122 | + .into_array() |
| 123 | +} |
| 124 | + |
| 125 | +#[divan::bench(consts = [2, 128])] |
| 126 | +fn fixed_size_group_sum_all_valid<const GROUP_SIZE: u32>(bencher: Bencher) { |
| 127 | + bench_fixed_size_group_sum( |
| 128 | + bencher, |
| 129 | + runend_with_validity(&Validity::AllValid), |
| 130 | + GROUP_SIZE, |
| 131 | + ); |
| 132 | +} |
0 commit comments