Skip to content

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
apache:mainfrom
Kanishk-Dendukuri:opt-minmax-element-wise
Open

Kanishk-Dendukuri wants to merge 1 commit into
apache:mainfrom
Kanishk-Dendukuri:opt-minmax-element-wise

Conversation

@Kanishk-Dendukuri

@Kanishk-Dendukuri Kanishk-Dendukuri commented Jul 26, 2026 •

Copy link
Copy Markdown

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 vs main), 5 reps; throughput, change %:

benchmark null baseline contender change %
ArrayArray Int64 50% 285.338M/s 2.429G/s +751.4%
ArrayArray Int64 0.01% 808.895M/s 3.594G/s +344.3%
ArrayArray Int64 1% 645.295M/s 2.823G/s +337.5%
ArrayArray Int64 10% 610.443M/s 2.433G/s +298.5%
ArrayArray Int64 0% 1.422G/s 5.447G/s +283.1%
ArrayArray Int64 100% 5.696G/s 14.671G/s +157.6%
ArrayScalar Int64 50% 1.075G/s 2.748G/s +155.7%
ArrayScalar Int64 1% 1.657G/s 3.387G/s +104.4%
ArrayScalar Int64 10% 1.628G/s 2.758G/s +69.4%
ArrayScalar Int64 0.01% 2.300G/s 3.817G/s +66.0%
ArrayScalar Int64 0% 2.482G/s 3.782G/s +52.4%
ArrayScalar Int64 100% 8.625G/s 10.134G/s +17.5%

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

@Reranko05 Reranko05 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@Kanishk-Dendukuri

Copy link
Copy Markdown
Author

I ran it and see that the C++ Format has passed and the rest were skipped. Is there something you noticed that I should reformat?

@Kanishk-Dendukuri
Kanishk-Dendukuri force-pushed the opt-minmax-element-wise branch from e35f877 to f1f7743 Compare July 27, 2026 01:52
@Reranko05

Copy link
Copy Markdown
Collaborator

I re-ran it locally and realized the reported formatting issue isn't related to the changes in this PR. It looks like it's a pre-existing issue elsewhere in the file. Sorry for the confusion. Your formatting is fine.

Comment on lines +502 to +505
auto left_reader = ::arrow::internal::BitmapUInt64Reader(
left_valid, left_valid ? left_offset : 0, left_valid ? length : 0);
auto right_reader = ::arrow::internal::BitmapUInt64Reader(
right_valid, right_valid ? right_offset : 0, right_valid ? length : 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is re-implementing logic that is already exposed as e.g. VisitTwoSetBitRuns. I expect you could massively simplify this code by using existing facilities.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for VisitTwoSetBitRuns it looks to be AND only, but we need OR as either side can make the output valid. I tried looking for existing OR facilities but wasn’t able to find something that would be good for this case (you would know more about this so lmk if there are others I should look into or if I am missing something). Previously I looked at VisitWords but it has the same issue as BitmapOr where we need to look at the validity bitmaps again to see which side to take

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. There's BinaryBitBlockCounter and OptionalBinaryBitBlockCounter that could fit here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These give me the popcount of the OR/AND result, so I can't tell which positions changed validity to decide whether to take the left value, right value, or Op(left, right) at each lane.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, fair enough! The remaining question is whether we think the additional complexity here is worth the performance gain. I'd like @zanmato1984 's opinion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the added complexity is justified if the benchmark numbers hold. The existing helpers make bitmap traversal simpler, but they don't seem to expose both input validity words in a way that lets us compute output validity and choose left/right/op in one pass. I'd keep this, but add a short comment explaining the tradeoff.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment on CombineWordwise explaining the tradeoff

const uint64_t right_word = right_valid ? right_reader.NextWord() : ~uint64_t(0);
const uint64_t out_word =
skip_nulls ? (left_word | right_word) : (left_word & right_word);
// out_valid is allocated at bit offset 0, so store the word directly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could compute the output bitmap separately using either BitmapAnd or BitmapOr.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding computing the bitmap separately means reading the left/right validity twice. once in BitmapAnd/Or, then again per element in the value loop, since with skip_nulls a slot can be valid with one side null and we need to know which side to take. The fused loop reads each validity word once and uses it for both.

OutputArrayWriter<OutType> writer(&out_span);
ArrayIterator<OutType> out_it(out_span);
int64_t index = 0;
VisitArrayValuesInline<OutType>(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could simply have rewritten this using VisitTwoSetBitRuns :-)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the discussion above: VisitTwoSetBitRuns only visits slots where both inputs are valid, but with skip_nulls a slot is valid if either input is, so it doesn't cover this case. CombineWordwise handles both modes in one pass.

@Kanishk-Dendukuri

Copy link
Copy Markdown
Author

Hi @pitrou, This has been open for a couple months. would appreciate a review when you have bandwidth. Happy to address any feedback.

@Kanishk-Dendukuri

Copy link
Copy Markdown
Author

Hi @zanmato1984, when you have time, is there anything else needed from me to get this merged? If not, could you help merge it? Thanks!

@zanmato1984 zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up, @Kanishk-Dendukuri. I'm happy with the approach, but I'd prefer to wait for @pitrou's final feedback before merging, given his concerns about the implementation.

@pitrou

pitrou commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks for taking a look @zanmato1984 . I'm now convinced that the approach is fine.

However, there seem to be CI issues worth fixing (also consider merging/rebasing from main).

@Kanishk-Dendukuri

Copy link
Copy Markdown
Author

Thanks @pitrou! I rebased on main, so the CI issues should be fixed once the runs are approved. The AMD64 Conda C++ AVX2 job may still fail, but that's also failing on main.

@pitrou

pitrou commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks @Kanishk-Dendukuri . Also, can you make sure the benchmark numbers in the PR description are up to date?

@Kanishk-Dendukuri

Copy link
Copy Markdown
Author

@pitrou I reran the benchmarks with 5 reps and got basically the same numbers, so the ones in the description are up to date (only tested on one machine though).

Two of the CI jobs (macOS Python and GLib & Ruby) passed before the rebase but failed/timed out this time. I'm not sure why, as my code didn't change. Would rerunning the two help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][Compute] Optimize min/max_element_wise with word at a time validity handling

5 participants