|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![allow(clippy::unwrap_used)] |
| 5 | + |
| 6 | +use divan::Bencher; |
| 7 | +use rand::Rng; |
| 8 | +use rand::SeedableRng; |
| 9 | +use rand::distr::Uniform; |
| 10 | +use rand::rngs::StdRng; |
| 11 | +use vortex_array::IntoArray; |
| 12 | +use vortex_array::arrays::StructArray; |
| 13 | +use vortex_array::compute::take; |
| 14 | +use vortex_array::validity::Validity; |
| 15 | +use vortex_buffer::Buffer; |
| 16 | +use vortex_dtype::FieldNames; |
| 17 | + |
| 18 | +fn main() { |
| 19 | + divan::main(); |
| 20 | +} |
| 21 | + |
| 22 | +const ARRAY_SIZE: usize = 100_000; |
| 23 | +const TAKE_SIZE: usize = 1000; |
| 24 | + |
| 25 | +#[divan::bench] |
| 26 | +fn take_struct_simple(bencher: Bencher) { |
| 27 | + let mut rng = StdRng::seed_from_u64(0); |
| 28 | + let range = Uniform::new(0i64, 100_000_000).unwrap(); |
| 29 | + |
| 30 | + // Create single field for the struct |
| 31 | + let field = (0..ARRAY_SIZE) |
| 32 | + .map(|_| rng.sample(range)) |
| 33 | + .collect::<Buffer<i64>>() |
| 34 | + .into_array(); |
| 35 | + |
| 36 | + let struct_array = StructArray::try_new( |
| 37 | + FieldNames::from(["value"]), |
| 38 | + vec![field], |
| 39 | + ARRAY_SIZE, |
| 40 | + Validity::NonNullable, |
| 41 | + ) |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + let indices: Buffer<u64> = (0..TAKE_SIZE) |
| 45 | + .map(|_| rng.random_range(0..ARRAY_SIZE) as u64) |
| 46 | + .collect(); |
| 47 | + let indices_array = indices.into_array(); |
| 48 | + |
| 49 | + bencher |
| 50 | + .with_inputs(|| (&struct_array, &indices_array)) |
| 51 | + .bench_refs(|(array, indices)| take(array.as_ref(), indices.as_ref()).unwrap()); |
| 52 | +} |
| 53 | + |
| 54 | +#[divan::bench(args = [8])] |
| 55 | +fn take_struct_wide(bencher: Bencher, width: usize) { |
| 56 | + let mut rng = StdRng::seed_from_u64(0); |
| 57 | + let range = Uniform::new(0i64, 100_000_000).unwrap(); |
| 58 | + |
| 59 | + let fields: Vec<_> = (0..width) |
| 60 | + .map(|_| { |
| 61 | + (0..ARRAY_SIZE) |
| 62 | + .map(|_| rng.sample(range)) |
| 63 | + .collect::<Buffer<i64>>() |
| 64 | + .into_array() |
| 65 | + }) |
| 66 | + .collect(); |
| 67 | + |
| 68 | + let field_names = FieldNames::from([ |
| 69 | + "field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", |
| 70 | + ]); |
| 71 | + |
| 72 | + let struct_array = |
| 73 | + StructArray::try_new(field_names, fields, ARRAY_SIZE, Validity::NonNullable).unwrap(); |
| 74 | + |
| 75 | + let indices: Buffer<u64> = (0..TAKE_SIZE) |
| 76 | + .map(|_| rng.random_range(0..ARRAY_SIZE) as u64) |
| 77 | + .collect(); |
| 78 | + let indices_array = indices.into_array(); |
| 79 | + |
| 80 | + bencher |
| 81 | + .with_inputs(|| (&struct_array, &indices_array)) |
| 82 | + .bench_refs(|(array, indices)| take(array.as_ref(), indices.as_ref()).unwrap()); |
| 83 | +} |
| 84 | + |
| 85 | +#[divan::bench] |
| 86 | +fn take_struct_sequential_indices(bencher: Bencher) { |
| 87 | + let mut rng = StdRng::seed_from_u64(0); |
| 88 | + let range = Uniform::new(0i64, 100_000_000).unwrap(); |
| 89 | + |
| 90 | + // Create single field for the struct |
| 91 | + let field = (0..ARRAY_SIZE) |
| 92 | + .map(|_| rng.sample(range)) |
| 93 | + .collect::<Buffer<i64>>() |
| 94 | + .into_array(); |
| 95 | + |
| 96 | + let struct_array = StructArray::try_new( |
| 97 | + FieldNames::from(["value"]), |
| 98 | + vec![field], |
| 99 | + ARRAY_SIZE, |
| 100 | + Validity::NonNullable, |
| 101 | + ) |
| 102 | + .unwrap(); |
| 103 | + |
| 104 | + // Sequential indices for better cache performance |
| 105 | + let indices: Buffer<u64> = (0..TAKE_SIZE as u64).collect(); |
| 106 | + let indices_array = indices.into_array(); |
| 107 | + |
| 108 | + bencher |
| 109 | + .with_inputs(|| (&struct_array, &indices_array)) |
| 110 | + .bench_refs(|(array, indices)| take(array.as_ref(), indices.as_ref()).unwrap()); |
| 111 | +} |
0 commit comments