You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "neilconway (via GitHub)" <gi...@apache.org> on 2026/04/08 13:42:17 UTC
[PR] perf: Optimize `lcd`, `gcd` NULL handling [datafusion]
neilconway opened a new pull request, #21468:
URL: https://github.com/apache/datafusion/pull/21468
## Which issue does this PR close?
- Closes #21467.
## Rationale for this change
This PR implements three distinct optimizations:
1. `lcm` was computing the result NULL buffer iteratively. This is relatively slow. Switching to Arrow's `try_binary` kernel both makes the implementation more concise and also improves performance by computing the result NULL buffer via the bitwise union of the input NULL buffers.
2. The `gcd` scalar arg path was doing similarly; switching to Arrow's `try_unary` yields a similar speedup.
3. For the `gcd` scalar path, computing the GCD can only fail in a few edge cases (e.g., `gcd(i64::MIN, i64::MIN)`). It is cheap to check for these edge-cases; for most `gcd` inputs, we can use Arrow's `unary` kernel instead of `try_unary`. The former is more efficient because it allows LLVM to vectorize the code more effectively.
Benchmarks (ARM64):
```
- gcd array and scalar: 2.9ms → 2.2ms, -25% faster
- lcm both array: 2.7ms → 2.0ms, -26% faster
```
## What changes are included in this PR?
* Add benchmark for `lcd`
* Improve SLT test coverage
* Move Rust unit test for `lcd` to SLT
* Optimize `lcd` and `gcm` NULL handling
* Optimize `gcm` to avoid overhead for edge cases
## Are these changes tested?
Yes.
## Are there any user-facing changes?
No.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Optimize NULL handling in `lcm`, `gcd` [datafusion]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich merged PR #21468:
URL: https://github.com/apache/datafusion/pull/21468
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Optimize NULL handling in `lcm`, `gcd` [datafusion]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #21468:
URL: https://github.com/apache/datafusion/pull/21468#discussion_r3065352325
##########
datafusion/functions/src/math/gcd.rs:
##########
@@ -126,18 +126,23 @@ fn compute_gcd_for_arrays(a: &ArrayRef, b: &ArrayRef) -> Result<ColumnarValue> {
}
fn compute_gcd_with_scalar(arr: &ArrayRef, scalar: Option<i64>) -> Result<ColumnarValue> {
+ let prim = arr.as_primitive::<Int64Type>();
match scalar {
+ Some(scalar_value) if scalar_value != 0 && scalar_value != i64::MIN => {
+ // The gcd result divides both inputs' absolute values. When the
Review Comment:
This is a slick optimization.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org