GH-50645: [C++][Compute] Optimize min/max_element_wise with word at a time validity handling#50647
Open
Kanishk-Dendukuri wants to merge 1 commit into
Open
Conversation
…d at a time validity handling
Reranko05
reviewed
Jul 26, 2026
Reranko05
left a comment
Contributor
There was a problem hiding this comment.
Can you run python -m pre_commit run clang-format --files cpp/src/arrow/compute/kernels/scalar_compare.cc cpp/src/arrow/compute/kernels/scalar_compare_test.cc to fix the formatting issues?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
The variadic min_element_wise / max_element_wise kernels (ScalarMinMax in scalar_compare.cc) fold arguments using a multi pass approach: an antiextreme sentinel fill, a separate validity bitmap pass (BitmapOr/BitmapAnd), and a per element value pass with a per-element validity branch.
This can be rewritten as a single word at a time pass (handling validity 64 bits at a time) that fuses validity and value computation and keeps fully-valid words branchless. That removes the redundant passes and the per-element mispredicting branch, giving a measured ~2-7x speedup on numeric types, with no behavioral change.
What changes are included in this PR?
Rewrote the numeric ScalarMinMax fold to a word at a time algorithm (CombineWordwise / CombineArrays / FoldArrayIntoOutput), with an all-valid branchless fast path and an all null skip.
Added a random, sliced/offset test (MinMaxElementWiseRandom) covering the new word loop across null densities and offsets.
Benchmarks
archery benchmark diff(this branch vsmain), 5 reps; throughput, change %:Are these changes tested?
Yes. All existing ElementWise tests pass, plus a new MinMaxElementWiseRandom typed test that cross checks the kernel against a reference across lengths (65/128/200), null densities (0–100%), offsets (0/1/3/7), both skip_nulls, and min/max, over all numeric types.
Are there any user-facing changes?
No
AI Usage
Parts of the code, tests, and this description were written with AI assistance.
Closes #50645