|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Baseline throughput for `byte_length` over UTF-8 view arrays. |
| 5 | +//! |
| 6 | +//! The arms cover inline and out-of-line views, plus nullable out-of-line views. Their names are |
| 7 | +//! intended to remain stable across scalar-function implementation changes so CodSpeed can compare |
| 8 | +//! them against `develop`. |
| 9 | +
|
| 10 | +#![expect(clippy::unwrap_used)] |
| 11 | + |
| 12 | +use std::sync::LazyLock; |
| 13 | + |
| 14 | +use divan::Bencher; |
| 15 | +use divan::counter::ItemsCount; |
| 16 | +use mimalloc::MiMalloc; |
| 17 | +use vortex_array::ArrayRef; |
| 18 | +use vortex_array::Canonical; |
| 19 | +use vortex_array::IntoArray; |
| 20 | +use vortex_array::VortexSessionExecute; |
| 21 | +use vortex_array::array_session; |
| 22 | +use vortex_array::arrays::MaskedArray; |
| 23 | +use vortex_array::arrays::ScalarFnArray; |
| 24 | +use vortex_array::arrays::VarBinViewArray; |
| 25 | +use vortex_array::scalar_fn::EmptyOptions; |
| 26 | +use vortex_array::scalar_fn::TypedScalarFnInstance; |
| 27 | +use vortex_array::scalar_fn::fns::byte_length::ByteLength; |
| 28 | +use vortex_array::validity::Validity; |
| 29 | +use vortex_session::VortexSession; |
| 30 | + |
| 31 | +// Scalar function execution allocates its output inside the timed region, so use the vendored |
| 32 | +// allocator instead of measuring glibc differences between CodSpeed runner images. |
| 33 | +#[global_allocator] |
| 34 | +static GLOBAL: MiMalloc = MiMalloc; |
| 35 | + |
| 36 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(array_session); |
| 37 | + |
| 38 | +fn main() { |
| 39 | + LazyLock::force(&SESSION); |
| 40 | + divan::main(); |
| 41 | +} |
| 42 | + |
| 43 | +const SIZES: &[usize] = &[4_096, 65_536]; |
| 44 | + |
| 45 | +fn long_strings(len: usize) -> ArrayRef { |
| 46 | + VarBinViewArray::from_iter_str((0..len).map(|i| format!("a string well past inline: {i}"))) |
| 47 | + .into_array() |
| 48 | +} |
| 49 | + |
| 50 | +fn short_strings(len: usize) -> ArrayRef { |
| 51 | + VarBinViewArray::from_iter_str((0..len).map(|i| format!("{}", i % 1_000))).into_array() |
| 52 | +} |
| 53 | + |
| 54 | +fn bench_byte_length(bencher: Bencher, input: ArrayRef) { |
| 55 | + let len = input.len(); |
| 56 | + bencher |
| 57 | + .counter(ItemsCount::new(len)) |
| 58 | + .with_inputs(|| { |
| 59 | + ( |
| 60 | + ScalarFnArray::try_new( |
| 61 | + TypedScalarFnInstance::new(ByteLength, EmptyOptions).erased(), |
| 62 | + vec![input.clone()], |
| 63 | + ) |
| 64 | + .unwrap() |
| 65 | + .into_array(), |
| 66 | + SESSION.create_execution_ctx(), |
| 67 | + ) |
| 68 | + }) |
| 69 | + .bench_values(|(array, mut ctx)| array.execute::<Canonical>(&mut ctx).unwrap()); |
| 70 | +} |
| 71 | + |
| 72 | +#[divan::bench(args = SIZES)] |
| 73 | +fn inline(bencher: Bencher, len: usize) { |
| 74 | + bench_byte_length(bencher, short_strings(len)); |
| 75 | +} |
| 76 | + |
| 77 | +#[divan::bench(args = SIZES)] |
| 78 | +fn out_of_line(bencher: Bencher, len: usize) { |
| 79 | + bench_byte_length(bencher, long_strings(len)); |
| 80 | +} |
| 81 | + |
| 82 | +#[divan::bench(args = SIZES)] |
| 83 | +fn nullable_out_of_line(bencher: Bencher, len: usize) { |
| 84 | + let validity = Validity::from_iter((0..len).map(|i| i % 8 != 0)); |
| 85 | + let input = MaskedArray::try_new(long_strings(len), validity) |
| 86 | + .unwrap() |
| 87 | + .into_array(); |
| 88 | + bench_byte_length(bencher, input); |
| 89 | +} |
| 90 | + |
| 91 | +#[divan::bench(args = SIZES)] |
| 92 | +fn nullable_out_of_line_90pct(bencher: Bencher, len: usize) { |
| 93 | + let validity = Validity::from_iter((0..len).map(|i| i.is_multiple_of(10))); |
| 94 | + let input = MaskedArray::try_new(long_strings(len), validity) |
| 95 | + .unwrap() |
| 96 | + .into_array(); |
| 97 | + bench_byte_length(bencher, input); |
| 98 | +} |
0 commit comments