|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#[macro_use] |
| 19 | +extern crate criterion; |
| 20 | +extern crate arrow; |
| 21 | + |
| 22 | +use std::{hint::black_box, sync::Arc}; |
| 23 | + |
| 24 | +use crate::criterion::Criterion; |
| 25 | +use arrow::{ |
| 26 | + array::{ArrayRef, FixedSizeListArray, Int32Array, ListArray, ListViewArray}, |
| 27 | + buffer::{OffsetBuffer, ScalarBuffer}, |
| 28 | + datatypes::{DataType, Field}, |
| 29 | +}; |
| 30 | +use datafusion_functions_nested::reverse::array_reverse_inner; |
| 31 | + |
| 32 | +fn array_reverse(array: &ArrayRef) -> ArrayRef { |
| 33 | + black_box(array_reverse_inner(std::slice::from_ref(array)).unwrap()) |
| 34 | +} |
| 35 | + |
| 36 | +fn criterion_benchmark(c: &mut Criterion) { |
| 37 | + // Construct large arrays for benchmarking |
| 38 | + let array_len = 100000; |
| 39 | + let step_size: usize = 1000; |
| 40 | + let offsets: Vec<i32> = (0..array_len as i32).step_by(step_size).collect(); |
| 41 | + let offsets = ScalarBuffer::from(offsets); |
| 42 | + let sizes: Vec<i32> = vec![step_size as i32; array_len / step_size]; |
| 43 | + let values = (0..array_len as i32).collect::<Vec<i32>>(); |
| 44 | + let list_array: ArrayRef = Arc::new(ListArray::new( |
| 45 | + Arc::new(Field::new("a", DataType::Int32, false)), |
| 46 | + OffsetBuffer::new(offsets.clone()), |
| 47 | + Arc::new(Int32Array::from(values.clone())), |
| 48 | + None, |
| 49 | + )); |
| 50 | + let fixed_size_list_array: ArrayRef = Arc::new(FixedSizeListArray::new( |
| 51 | + Arc::new(Field::new("a", DataType::Int32, false)), |
| 52 | + step_size as i32, |
| 53 | + Arc::new(Int32Array::from(values.clone())), |
| 54 | + None, |
| 55 | + )); |
| 56 | + let list_view_array: ArrayRef = Arc::new(ListViewArray::new( |
| 57 | + Arc::new(Field::new("a", DataType::Int32, false)), |
| 58 | + offsets, |
| 59 | + ScalarBuffer::from(sizes), |
| 60 | + Arc::new(Int32Array::from(values)), |
| 61 | + None, |
| 62 | + )); |
| 63 | + |
| 64 | + c.bench_function("array_reverse_list", |b| { |
| 65 | + b.iter(|| array_reverse(&list_array)) |
| 66 | + }); |
| 67 | + |
| 68 | + c.bench_function("array_reverse_fixed_size_list", |b| { |
| 69 | + b.iter(|| array_reverse(&fixed_size_list_array)) |
| 70 | + }); |
| 71 | + |
| 72 | + c.bench_function("array_reverse_list_view", |b| { |
| 73 | + b.iter(|| array_reverse(&list_view_array)) |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +criterion_group!(benches, criterion_benchmark); |
| 78 | +criterion_main!(benches); |
0 commit comments