|
| 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 | +extern crate criterion; |
| 19 | + |
| 20 | +use arrow::datatypes::{DataType, Field, Float32Type, Float64Type}; |
| 21 | +use arrow::util::bench_util::create_primitive_array; |
| 22 | +use criterion::{Criterion, SamplingMode, criterion_group, criterion_main}; |
| 23 | +use datafusion_common::ScalarValue; |
| 24 | +use datafusion_common::config::ConfigOptions; |
| 25 | +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs}; |
| 26 | +use datafusion_functions::math::round; |
| 27 | +use std::hint::black_box; |
| 28 | +use std::sync::Arc; |
| 29 | +use std::time::Duration; |
| 30 | + |
| 31 | +fn criterion_benchmark(c: &mut Criterion) { |
| 32 | + let round_fn = round(); |
| 33 | + let config_options = Arc::new(ConfigOptions::default()); |
| 34 | + |
| 35 | + for size in [1024, 4096, 8192] { |
| 36 | + let mut group = c.benchmark_group(format!("round size={size}")); |
| 37 | + group.sampling_mode(SamplingMode::Flat); |
| 38 | + group.sample_size(10); |
| 39 | + group.measurement_time(Duration::from_secs(10)); |
| 40 | + |
| 41 | + // Float64 array benchmark |
| 42 | + let f64_array = Arc::new(create_primitive_array::<Float64Type>(size, 0.1)); |
| 43 | + let batch_len = f64_array.len(); |
| 44 | + let f64_args = vec![ |
| 45 | + ColumnarValue::Array(f64_array), |
| 46 | + ColumnarValue::Scalar(ScalarValue::Int32(Some(2))), |
| 47 | + ]; |
| 48 | + |
| 49 | + group.bench_function("round_f64_array", |b| { |
| 50 | + b.iter(|| { |
| 51 | + let args_cloned = f64_args.clone(); |
| 52 | + black_box( |
| 53 | + round_fn |
| 54 | + .invoke_with_args(ScalarFunctionArgs { |
| 55 | + args: args_cloned, |
| 56 | + arg_fields: vec![ |
| 57 | + Field::new("a", DataType::Float64, true).into(), |
| 58 | + Field::new("b", DataType::Int32, false).into(), |
| 59 | + ], |
| 60 | + number_rows: batch_len, |
| 61 | + return_field: Field::new("f", DataType::Float64, true).into(), |
| 62 | + config_options: Arc::clone(&config_options), |
| 63 | + }) |
| 64 | + .unwrap(), |
| 65 | + ) |
| 66 | + }) |
| 67 | + }); |
| 68 | + |
| 69 | + // Float32 array benchmark |
| 70 | + let f32_array = Arc::new(create_primitive_array::<Float32Type>(size, 0.1)); |
| 71 | + let f32_args = vec![ |
| 72 | + ColumnarValue::Array(f32_array), |
| 73 | + ColumnarValue::Scalar(ScalarValue::Int32(Some(2))), |
| 74 | + ]; |
| 75 | + |
| 76 | + group.bench_function("round_f32_array", |b| { |
| 77 | + b.iter(|| { |
| 78 | + let args_cloned = f32_args.clone(); |
| 79 | + black_box( |
| 80 | + round_fn |
| 81 | + .invoke_with_args(ScalarFunctionArgs { |
| 82 | + args: args_cloned, |
| 83 | + arg_fields: vec![ |
| 84 | + Field::new("a", DataType::Float32, true).into(), |
| 85 | + Field::new("b", DataType::Int32, false).into(), |
| 86 | + ], |
| 87 | + number_rows: batch_len, |
| 88 | + return_field: Field::new("f", DataType::Float32, true).into(), |
| 89 | + config_options: Arc::clone(&config_options), |
| 90 | + }) |
| 91 | + .unwrap(), |
| 92 | + ) |
| 93 | + }) |
| 94 | + }); |
| 95 | + |
| 96 | + // Scalar benchmark (the optimization we added) |
| 97 | + let scalar_f64_args = vec![ |
| 98 | + ColumnarValue::Scalar(ScalarValue::Float64(Some(std::f64::consts::PI))), |
| 99 | + ColumnarValue::Scalar(ScalarValue::Int32(Some(2))), |
| 100 | + ]; |
| 101 | + |
| 102 | + group.bench_function("round_f64_scalar", |b| { |
| 103 | + b.iter(|| { |
| 104 | + let args_cloned = scalar_f64_args.clone(); |
| 105 | + black_box( |
| 106 | + round_fn |
| 107 | + .invoke_with_args(ScalarFunctionArgs { |
| 108 | + args: args_cloned, |
| 109 | + arg_fields: vec![ |
| 110 | + Field::new("a", DataType::Float64, false).into(), |
| 111 | + Field::new("b", DataType::Int32, false).into(), |
| 112 | + ], |
| 113 | + number_rows: 1, |
| 114 | + return_field: Field::new("f", DataType::Float64, false) |
| 115 | + .into(), |
| 116 | + config_options: Arc::clone(&config_options), |
| 117 | + }) |
| 118 | + .unwrap(), |
| 119 | + ) |
| 120 | + }) |
| 121 | + }); |
| 122 | + |
| 123 | + let scalar_f32_args = vec![ |
| 124 | + ColumnarValue::Scalar(ScalarValue::Float32(Some(std::f32::consts::PI))), |
| 125 | + ColumnarValue::Scalar(ScalarValue::Int32(Some(2))), |
| 126 | + ]; |
| 127 | + |
| 128 | + group.bench_function("round_f32_scalar", |b| { |
| 129 | + b.iter(|| { |
| 130 | + let args_cloned = scalar_f32_args.clone(); |
| 131 | + black_box( |
| 132 | + round_fn |
| 133 | + .invoke_with_args(ScalarFunctionArgs { |
| 134 | + args: args_cloned, |
| 135 | + arg_fields: vec![ |
| 136 | + Field::new("a", DataType::Float32, false).into(), |
| 137 | + Field::new("b", DataType::Int32, false).into(), |
| 138 | + ], |
| 139 | + number_rows: 1, |
| 140 | + return_field: Field::new("f", DataType::Float32, false) |
| 141 | + .into(), |
| 142 | + config_options: Arc::clone(&config_options), |
| 143 | + }) |
| 144 | + .unwrap(), |
| 145 | + ) |
| 146 | + }) |
| 147 | + }); |
| 148 | + |
| 149 | + group.finish(); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +criterion_group!(benches, criterion_benchmark); |
| 154 | +criterion_main!(benches); |
0 commit comments