Skip to content

Commit 4d8d48c

Browse files
authored
perf: Optimize scalar performance for cot (#19888)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Part of apache/datafusion-comet#2986. ## Rationale for this change The cot function currently converts scalar inputs to arrays before processing, even for single scalar values. This adds unnecessary overhead from array allocation and conversion. Adding a scalar fast path avoids this overhead. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Added scalar fast path - Added benchmark - Update tests | Type | Before | After | Speedup | |------|--------|-------|---------| | **cot_f64_scalar** | 229 ns | 67 ns | **3.4x** | | **cot_f32_scalar** | 247 ns | 59 ns | **4.2x** | <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 406e5aa commit 4d8d48c

File tree

2 files changed

+287
-61
lines changed

2 files changed

+287
-61
lines changed

datafusion/functions/benches/cot.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ use datafusion_functions::math::cot;
2727
use std::hint::black_box;
2828

2929
use arrow::datatypes::{DataType, Field};
30+
use datafusion_common::ScalarValue;
3031
use datafusion_common::config::ConfigOptions;
3132
use std::sync::Arc;
3233

3334
fn criterion_benchmark(c: &mut Criterion) {
3435
let cot_fn = cot();
36+
let config_options = Arc::new(ConfigOptions::default());
37+
38+
// Array benchmarks - run for different sizes
3539
for size in [1024, 4096, 8192] {
3640
let f32_array = Arc::new(create_primitive_array::<Float32Type>(size, 0.2));
3741
let f32_args = vec![ColumnarValue::Array(f32_array)];
@@ -42,7 +46,6 @@ fn criterion_benchmark(c: &mut Criterion) {
4246
Field::new(format!("arg_{idx}"), arg.data_type(), true).into()
4347
})
4448
.collect::<Vec<_>>();
45-
let config_options = Arc::new(ConfigOptions::default());
4649

4750
c.bench_function(&format!("cot f32 array: {size}"), |b| {
4851
b.iter(|| {
@@ -59,6 +62,7 @@ fn criterion_benchmark(c: &mut Criterion) {
5962
)
6063
})
6164
});
65+
6266
let f64_array = Arc::new(create_primitive_array::<Float64Type>(size, 0.2));
6367
let f64_args = vec![ColumnarValue::Array(f64_array)];
6468
let arg_fields = f64_args
@@ -86,6 +90,47 @@ fn criterion_benchmark(c: &mut Criterion) {
8690
})
8791
});
8892
}
93+
94+
// Scalar benchmarks - run only once since size doesn't affect scalar performance
95+
let scalar_f32_args = vec![ColumnarValue::Scalar(ScalarValue::Float32(Some(1.0)))];
96+
let scalar_f32_arg_fields = vec![Field::new("a", DataType::Float32, false).into()];
97+
let return_field_f32 = Field::new("f", DataType::Float32, false).into();
98+
99+
c.bench_function("cot f32 scalar", |b| {
100+
b.iter(|| {
101+
black_box(
102+
cot_fn
103+
.invoke_with_args(ScalarFunctionArgs {
104+
args: scalar_f32_args.clone(),
105+
arg_fields: scalar_f32_arg_fields.clone(),
106+
number_rows: 1,
107+
return_field: Arc::clone(&return_field_f32),
108+
config_options: Arc::clone(&config_options),
109+
})
110+
.unwrap(),
111+
)
112+
})
113+
});
114+
115+
let scalar_f64_args = vec![ColumnarValue::Scalar(ScalarValue::Float64(Some(1.0)))];
116+
let scalar_f64_arg_fields = vec![Field::new("a", DataType::Float64, false).into()];
117+
let return_field_f64 = Field::new("f", DataType::Float64, false).into();
118+
119+
c.bench_function("cot f64 scalar", |b| {
120+
b.iter(|| {
121+
black_box(
122+
cot_fn
123+
.invoke_with_args(ScalarFunctionArgs {
124+
args: scalar_f64_args.clone(),
125+
arg_fields: scalar_f64_arg_fields.clone(),
126+
number_rows: 1,
127+
return_field: Arc::clone(&return_field_f64),
128+
config_options: Arc::clone(&config_options),
129+
})
130+
.unwrap(),
131+
)
132+
})
133+
});
89134
}
90135

91136
criterion_group!(benches, criterion_benchmark);

0 commit comments

Comments
 (0)