Skip to content

Commit 8beb037

Browse files
committed
add take struct benchmark back
Signed-off-by: Connor Tsui <[email protected]>
1 parent 353faf6 commit 8beb037

File tree

4 files changed

+119
-16
lines changed

4 files changed

+119
-16
lines changed

vortex-array/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,9 @@ harness = false
159159
name = "take_primitive"
160160
harness = false
161161

162+
[[bench]]
163+
name = "take_struct"
164+
harness = false
165+
162166
[package.metadata.cargo-machete]
163167
ignored = ["getrandom_v03"]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

vortex-compute/src/take/bit_buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mod tests {
6464
use crate::take::Take;
6565

6666
#[test]
67-
fn test_bit_buffer_take_small_and_large() {
67+
fn test_take_bit_buffer_take_small_and_large() {
6868
use vortex_buffer::BitBuffer;
6969

7070
// Small buffer (uses take_byte_bool path).

vortex-compute/src/take/vector/dvector.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ impl<D: NativeDecimalType, I: UnsignedPType> Take<PVector<I>> for &DVector<D> {
2424
impl<D: NativeDecimalType, I: UnsignedPType> Take<[I]> for &DVector<D> {
2525
type Output = DVector<D>;
2626

27-
fn take(self, _indices: &[I]) -> DVector<D> {
28-
todo!("TODO(connor): Implement `take` for `DVector` and figure out trait bounds");
29-
30-
/*
31-
27+
fn take(self, indices: &[I]) -> DVector<D> {
3228
let taken_elements = self.elements().take(indices);
3329
let taken_validity = self.validity().take(indices);
3430

@@ -38,19 +34,13 @@ impl<D: NativeDecimalType, I: UnsignedPType> Take<[I]> for &DVector<D> {
3834
// components must have the same length. The elements are unchanged, so they must still be
3935
// within the precision/scale bounds.
4036
unsafe { DVector::new_unchecked(self.precision_scale(), taken_elements, taken_validity) }
41-
42-
*/
4337
}
4438
}
4539

4640
fn take_nullable<D: NativeDecimalType, I: UnsignedPType>(
47-
_dvector: &DVector<D>,
48-
_indices: &PVector<I>,
41+
dvector: &DVector<D>,
42+
indices: &PVector<I>,
4943
) -> DVector<D> {
50-
todo!("TODO(connor): Implement `take` for `DVector` and figure out trait bounds");
51-
52-
/*
53-
5444
// We ignore nullability when taking the elements since we can let the `Mask` implementation
5545
// determine which elements are null.
5646
let taken_elements = dvector.elements().take(indices.elements().as_slice());
@@ -62,6 +52,4 @@ fn take_nullable<D: NativeDecimalType, I: UnsignedPType>(
6252
// same length. The elements are unchanged, so they must still be within the precision/scale
6353
// bounds.
6454
unsafe { DVector::new_unchecked(dvector.precision_scale(), taken_elements, taken_validity) }
65-
66-
*/
6755
}

0 commit comments

Comments
 (0)