Skip to content

perf: Optimize lcd, gcd NULL handling#21468

Open
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/perf-lcm-gcd-nulls
Open

perf: Optimize lcd, gcd NULL handling#21468
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/perf-lcm-gcd-nulls

Conversation

@neilconway
Copy link
Copy Markdown
Contributor

@neilconway neilconway commented Apr 8, 2026

Which issue does this PR close?

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 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. Benchmark results above. I inspected the generated code for the gcd case to confirm that LLVM is able to generate better code for the unary case than for the try_unary case.

Are there any user-facing changes?

No.

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize NULL handling for lcm, gcd

1 participant