|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![expect(clippy::unwrap_used)] |
| 5 | + |
| 6 | +//! Scalar construction and validation, stats reads and merges, and accumulator creation. |
| 7 | +//! |
| 8 | +//! Every benchmark does `N` operations per iteration, except the stats merge, which does `MERGES`. |
| 9 | +
|
| 10 | +use std::hint::black_box; |
| 11 | +use std::sync::LazyLock; |
| 12 | + |
| 13 | +use divan::Bencher; |
| 14 | +use divan::counter::ItemsCount; |
| 15 | +use mimalloc::MiMalloc; |
| 16 | +use vortex_array::IntoArray; |
| 17 | +use vortex_array::VortexSessionExecute; |
| 18 | +use vortex_array::aggregate_fn::AggregateFnVTableExt; |
| 19 | +use vortex_array::aggregate_fn::NumericalAggregateOpts; |
| 20 | +use vortex_array::aggregate_fn::fns::count::Count; |
| 21 | +use vortex_array::aggregate_fn::fns::sum_v2::SumV2; |
| 22 | +use vortex_array::array_session; |
| 23 | +use vortex_array::dtype::DType; |
| 24 | +use vortex_array::dtype::FieldNames; |
| 25 | +use vortex_array::dtype::Nullability; |
| 26 | +use vortex_array::dtype::PType; |
| 27 | +use vortex_array::dtype::StructFields; |
| 28 | +use vortex_array::expr::stats::Precision; |
| 29 | +use vortex_array::expr::stats::Stat; |
| 30 | +use vortex_array::expr::stats::StatsProvider; |
| 31 | +use vortex_array::scalar::PValue; |
| 32 | +use vortex_array::scalar::Scalar; |
| 33 | +use vortex_array::scalar::ScalarValue; |
| 34 | +use vortex_array::stats::StatsSet; |
| 35 | +use vortex_buffer::Buffer; |
| 36 | +use vortex_buffer::BufferString; |
| 37 | +use vortex_session::VortexSession; |
| 38 | + |
| 39 | +#[global_allocator] |
| 40 | +static GLOBAL: MiMalloc = MiMalloc; |
| 41 | + |
| 42 | +const N: usize = 1024; |
| 43 | +/// A merge costs about 300ns, so it runs fewer times to keep the CodSpeed simulation under 1ms. |
| 44 | +const MERGES: usize = 64; |
| 45 | + |
| 46 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(array_session); |
| 47 | + |
| 48 | +fn main() { |
| 49 | + LazyLock::force(&SESSION); |
| 50 | + divan::main(); |
| 51 | +} |
| 52 | + |
| 53 | +fn struct8_dtype() -> DType { |
| 54 | + DType::Struct( |
| 55 | + StructFields::new( |
| 56 | + FieldNames::from(["f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8"]), |
| 57 | + vec![DType::Primitive(PType::I64, Nullability::NonNullable); 8], |
| 58 | + ), |
| 59 | + Nullability::NonNullable, |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +fn struct8_value(i: i64) -> ScalarValue { |
| 64 | + ScalarValue::Tuple( |
| 65 | + (0..8) |
| 66 | + .map(|f| Some(ScalarValue::Primitive(PValue::I64(i + f)))) |
| 67 | + .collect(), |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +#[divan::bench] |
| 72 | +fn try_new_primitive(bencher: Bencher) { |
| 73 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 74 | + for i in 0..N { |
| 75 | + black_box( |
| 76 | + Scalar::try_new( |
| 77 | + DType::Primitive(PType::I64, Nullability::NonNullable), |
| 78 | + Some(ScalarValue::Primitive(PValue::I64(black_box(i as i64)))), |
| 79 | + ) |
| 80 | + .unwrap(), |
| 81 | + ); |
| 82 | + } |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +#[divan::bench] |
| 87 | +fn try_new_null(bencher: Bencher) { |
| 88 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 89 | + for _ in 0..N { |
| 90 | + black_box( |
| 91 | + Scalar::try_new(black_box(DType::Utf8(Nullability::Nullable)), None).unwrap(), |
| 92 | + ); |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +#[divan::bench] |
| 98 | +fn try_new_utf8(bencher: Bencher) { |
| 99 | + let value = BufferString::from("hello world, a string"); |
| 100 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 101 | + for _ in 0..N { |
| 102 | + black_box( |
| 103 | + Scalar::try_new( |
| 104 | + DType::Utf8(Nullability::NonNullable), |
| 105 | + Some(ScalarValue::Utf8(black_box(&value).clone())), |
| 106 | + ) |
| 107 | + .unwrap(), |
| 108 | + ); |
| 109 | + } |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +#[divan::bench] |
| 114 | +fn try_new_struct8(bencher: Bencher) { |
| 115 | + let dtype = struct8_dtype(); |
| 116 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 117 | + for i in 0..N { |
| 118 | + black_box(Scalar::try_new(dtype.clone(), Some(struct8_value(i as i64))).unwrap()); |
| 119 | + } |
| 120 | + }); |
| 121 | +} |
| 122 | + |
| 123 | +#[divan::bench] |
| 124 | +fn validate_struct8(bencher: Bencher) { |
| 125 | + let dtype = struct8_dtype(); |
| 126 | + let value = struct8_value(0); |
| 127 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 128 | + for _ in 0..N { |
| 129 | + black_box(Scalar::validate(black_box(&dtype), Some(black_box(&value)))).unwrap(); |
| 130 | + } |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +fn i64_stats(offset: i64) -> StatsSet { |
| 135 | + StatsSet::from_iter([ |
| 136 | + (Stat::Min, Precision::exact(ScalarValue::from(offset))), |
| 137 | + (Stat::Max, Precision::exact(ScalarValue::from(offset + 100))), |
| 138 | + (Stat::Sum, Precision::exact(ScalarValue::from(offset * 7))), |
| 139 | + (Stat::NullCount, Precision::exact(ScalarValue::from(3u64))), |
| 140 | + (Stat::IsConstant, Precision::exact(ScalarValue::from(false))), |
| 141 | + (Stat::IsSorted, Precision::exact(ScalarValue::from(true))), |
| 142 | + ( |
| 143 | + Stat::IsStrictSorted, |
| 144 | + Precision::exact(ScalarValue::from(true)), |
| 145 | + ), |
| 146 | + ( |
| 147 | + Stat::UncompressedSizeInBytes, |
| 148 | + Precision::exact(ScalarValue::from(800u64)), |
| 149 | + ), |
| 150 | + ]) |
| 151 | +} |
| 152 | + |
| 153 | +#[divan::bench] |
| 154 | +fn stats_get_min(bencher: Bencher) { |
| 155 | + let array = (0..1024i64).collect::<Buffer<i64>>().into_array(); |
| 156 | + array.statistics().set_iter(i64_stats(0).into_iter()); |
| 157 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 158 | + for _ in 0..N { |
| 159 | + black_box(black_box(&array).statistics().get(Stat::Min)); |
| 160 | + } |
| 161 | + }); |
| 162 | +} |
| 163 | + |
| 164 | +#[divan::bench] |
| 165 | +fn stats_compute_is_constant_cached(bencher: Bencher) { |
| 166 | + let array = (0..1024i64).collect::<Buffer<i64>>().into_array(); |
| 167 | + array.statistics().set_iter(i64_stats(0).into_iter()); |
| 168 | + let mut ctx = SESSION.create_execution_ctx(); |
| 169 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 170 | + for _ in 0..N { |
| 171 | + black_box(black_box(&array).statistics().compute_is_constant(&mut ctx)); |
| 172 | + } |
| 173 | + }); |
| 174 | +} |
| 175 | + |
| 176 | +#[divan::bench] |
| 177 | +fn stats_merge_ordered(bencher: Bencher) { |
| 178 | + let dtype = DType::Primitive(PType::I64, Nullability::NonNullable); |
| 179 | + let left = i64_stats(0); |
| 180 | + let right = i64_stats(1000); |
| 181 | + bencher |
| 182 | + .counter(ItemsCount::new(MERGES)) |
| 183 | + .with_inputs(|| vec![left.clone(); MERGES]) |
| 184 | + .bench_local_values(|sets| { |
| 185 | + for set in sets { |
| 186 | + black_box(set.merge_ordered(&right, &dtype)); |
| 187 | + } |
| 188 | + }); |
| 189 | +} |
| 190 | + |
| 191 | +#[divan::bench] |
| 192 | +fn accumulator_sum_v2(bencher: Bencher) { |
| 193 | + let dtype = DType::Primitive(PType::I64, Nullability::Nullable); |
| 194 | + let aggregate_fn = SumV2.bind(NumericalAggregateOpts::default()); |
| 195 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 196 | + for _ in 0..N { |
| 197 | + black_box(aggregate_fn.accumulator(black_box(&dtype)).unwrap()); |
| 198 | + } |
| 199 | + }); |
| 200 | +} |
| 201 | + |
| 202 | +#[divan::bench] |
| 203 | +fn accumulator_count_struct8(bencher: Bencher) { |
| 204 | + let dtype = struct8_dtype(); |
| 205 | + let aggregate_fn = Count.bind(NumericalAggregateOpts::default()); |
| 206 | + bencher.counter(ItemsCount::new(N)).bench_local(|| { |
| 207 | + for _ in 0..N { |
| 208 | + black_box(aggregate_fn.accumulator(black_box(&dtype)).unwrap()); |
| 209 | + } |
| 210 | + }); |
| 211 | +} |
0 commit comments