-
Notifications
You must be signed in to change notification settings - Fork 340
perf: optimize spark_ceil (3x faster)
#4926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,3 +139,7 @@ harness = false | |
| [[bench]] | ||
| name = "to_json" | ||
| harness = false | ||
|
|
||
| [[bench]] | ||
| name = "ceil" | ||
| harness = false | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::array::Decimal128Array; | ||
| use arrow::datatypes::DataType; | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use datafusion::physical_plan::ColumnarValue; | ||
| use datafusion_comet_spark_expr::spark_ceil; | ||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
|
|
||
| const NUM_ROWS: i128 = 8192; | ||
|
|
||
| fn decimal_args(precision: u8, scale: i8, spread: i128) -> Vec<ColumnarValue> { | ||
| let values: Vec<i128> = (0..NUM_ROWS) | ||
| .map(|i| (i - NUM_ROWS / 2) * spread + i % 7) | ||
| .collect(); | ||
| let array = Decimal128Array::from(values) | ||
| .with_precision_and_scale(precision, scale) | ||
| .unwrap(); | ||
| vec![ColumnarValue::Array(Arc::new(array))] | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| // decimal(18, 4): unscaled values fit comfortably in 64 bits | ||
| let narrow = decimal_args(18, 4, 1_000_003); | ||
| // decimal(38, 6): unscaled values exceed the 64-bit range | ||
| let wide = decimal_args(38, 6, 1_000_000_000_000_000_003); | ||
|
|
||
| let mut group = c.benchmark_group("ceil"); | ||
| group.bench_function("ceil_decimal_18_4", |b| { | ||
| b.iter(|| { | ||
| black_box(spark_ceil( | ||
| black_box(&narrow), | ||
| black_box(&DataType::Decimal128(18, 0)), | ||
| )) | ||
| }) | ||
| }); | ||
| group.bench_function("ceil_decimal_38_6", |b| { | ||
| b.iter(|| { | ||
| black_box(spark_ceil( | ||
| black_box(&wide), | ||
| black_box(&DataType::Decimal128(38, 0)), | ||
| )) | ||
| }) | ||
| }); | ||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,7 @@ pub(crate) fn make_decimal_array( | |
| array: &ArrayRef, | ||
| precision: u8, | ||
| scale: i8, | ||
| f: &dyn Fn(i128) -> i128, | ||
| f: impl Fn(i128) -> i128, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change: pass the closure by value at |
||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| let array = array.as_primitive::<Decimal128Type>(); | ||
| let result: Decimal128Array = arrow::compute::kernels::arity::unary(array, f); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ceil.rs:97-254tests only small unscaled values (max12999), which all take the new i64 fast path. The entire point of this PR is the branch atceil.rs:85, and the fallback arm (_ => div_ceil(x, div)atceil.rs:93) has zero direct coverage. A regression that broke only the wide path would pass CI.Suggested change: add an array test with a
Decimal128(38, s)input whose unscaled values exceedi64::MAX(positive and negative), asserting the ceil result. Example: unscaled20_000_000_000_000_000_000(> i64::MAX) at scale 6 targetingDecimal128(38, 0), plus a negative sibling and an exact multiple, so both the fast and fallback arms plus the negative-remainder branch are covered in one suite.