|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![expect(clippy::cast_possible_truncation)] |
| 5 | + |
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +use divan::Bencher; |
| 9 | +use vortex_array::ArrayRef; |
| 10 | +use vortex_array::IntoArray; |
| 11 | +use vortex_array::arrays::FixedSizeListArray; |
| 12 | +use vortex_array::arrays::ListViewArray; |
| 13 | +use vortex_array::arrays::PrimitiveArray; |
| 14 | +use vortex_array::dtype::Nullability::NonNullable; |
| 15 | +use vortex_array::dtype::PType::I32; |
| 16 | +use vortex_array::scalar::Scalar; |
| 17 | +use vortex_array::validity::Validity; |
| 18 | +use vortex_buffer::Buffer; |
| 19 | +use vortex_error::VortexExpect; |
| 20 | +use vortex_sparse::Sparse; |
| 21 | + |
| 22 | +fn main() { |
| 23 | + divan::main(); |
| 24 | +} |
| 25 | + |
| 26 | +const LIST_ARGS: &[(usize, usize, usize)] = &[ |
| 27 | + // len, patch_stride, list_size |
| 28 | + (10_000, 7, 8), |
| 29 | + (50_000, 7, 8), |
| 30 | + (50_000, 11, 16), |
| 31 | +]; |
| 32 | + |
| 33 | +const FIXED_SIZE_LIST_ARGS: &[(usize, usize, u32)] = &[ |
| 34 | + // len, patch_stride, list_size |
| 35 | + (10_000, 7, 8), |
| 36 | + (50_000, 7, 8), |
| 37 | + (50_000, 11, 16), |
| 38 | +]; |
| 39 | + |
| 40 | +fn make_sparse_list(len: usize, patch_stride: usize, list_size: usize) -> ArrayRef { |
| 41 | + let patch_indices: Buffer<u32> = (0..len).step_by(patch_stride).map(|i| i as u32).collect(); |
| 42 | + let n_patches = patch_indices.len(); |
| 43 | + |
| 44 | + let patch_elements = PrimitiveArray::from_iter(0..(n_patches * list_size) as i32).into_array(); |
| 45 | + let patch_offsets: Buffer<u32> = (0..n_patches).map(|i| (i * list_size) as u32).collect(); |
| 46 | + let patch_sizes: Buffer<u32> = std::iter::repeat_n(list_size as u32, n_patches).collect(); |
| 47 | + let patch_values = ListViewArray::new( |
| 48 | + patch_elements, |
| 49 | + patch_offsets.into_array(), |
| 50 | + patch_sizes.into_array(), |
| 51 | + Validity::NonNullable, |
| 52 | + ) |
| 53 | + .into_array(); |
| 54 | + |
| 55 | + let fill_value = Scalar::list( |
| 56 | + Arc::new(I32.into()), |
| 57 | + (0..list_size as i32).map(Scalar::from).collect(), |
| 58 | + NonNullable, |
| 59 | + ); |
| 60 | + |
| 61 | + Sparse::try_new(patch_indices.into_array(), patch_values, len, fill_value) |
| 62 | + .vortex_expect("sparse list input should be valid") |
| 63 | + .into_array() |
| 64 | +} |
| 65 | + |
| 66 | +fn make_sparse_fixed_size_list(len: usize, patch_stride: usize, list_size: u32) -> ArrayRef { |
| 67 | + let patch_indices: Buffer<u32> = (0..len).step_by(patch_stride).map(|i| i as u32).collect(); |
| 68 | + let n_patches = patch_indices.len(); |
| 69 | + |
| 70 | + let patch_elements = |
| 71 | + PrimitiveArray::from_iter(0..(n_patches * list_size as usize) as i32).into_array(); |
| 72 | + let patch_values = |
| 73 | + FixedSizeListArray::new(patch_elements, list_size, Validity::NonNullable, n_patches) |
| 74 | + .into_array(); |
| 75 | + |
| 76 | + let fill_value = Scalar::list( |
| 77 | + Arc::new(I32.into()), |
| 78 | + (0..list_size as i32).map(Scalar::from).collect(), |
| 79 | + NonNullable, |
| 80 | + ); |
| 81 | + |
| 82 | + Sparse::try_new(patch_indices.into_array(), patch_values, len, fill_value) |
| 83 | + .vortex_expect("sparse fixed-size-list input should be valid") |
| 84 | + .into_array() |
| 85 | +} |
| 86 | + |
| 87 | +#[divan::bench(args = LIST_ARGS)] |
| 88 | +fn canonicalize_sparse_list( |
| 89 | + bencher: Bencher, |
| 90 | + (len, patch_stride, list_size): (usize, usize, usize), |
| 91 | +) { |
| 92 | + let sparse = make_sparse_list(len, patch_stride, list_size); |
| 93 | + |
| 94 | + bencher |
| 95 | + .with_inputs(|| sparse.clone()) |
| 96 | + .bench_values(|array| { |
| 97 | + divan::black_box(array.to_canonical().vortex_expect("sparse list canonicalization")) |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +#[divan::bench(args = FIXED_SIZE_LIST_ARGS)] |
| 102 | +fn canonicalize_sparse_fixed_size_list( |
| 103 | + bencher: Bencher, |
| 104 | + (len, patch_stride, list_size): (usize, usize, u32), |
| 105 | +) { |
| 106 | + let sparse = make_sparse_fixed_size_list(len, patch_stride, list_size); |
| 107 | + |
| 108 | + bencher |
| 109 | + .with_inputs(|| sparse.clone()) |
| 110 | + .bench_values(|array| { |
| 111 | + divan::black_box( |
| 112 | + array |
| 113 | + .to_canonical() |
| 114 | + .vortex_expect("sparse fixed-size-list canonicalization"), |
| 115 | + ) |
| 116 | + }); |
| 117 | +} |
0 commit comments