Skip to content

perf: divide once per row in pmod and div_round_half_up - #7690

Open
mbutrovich wants to merge 2 commits into
apache:mainfrom
mbutrovich:perf-divide-once
Open

mbutrovich wants to merge 2 commits into
apache:mainfrom
mbutrovich:perf-divide-once

Conversation

@mbutrovich

@mbutrovich mbutrovich commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #6781.

Rationale for this change

See #6781.

What changes are included in this PR?

pmod uses i32::rem_euclid, which adds n to a negative remainder instead of dividing again. div_round_half_up computes the remainder from the quotient instead of with a second i256 division.

The hash-to-partition loop moves from the two sites in multi_partition.rs into hash_partition_ids next to pmod. A new pmod benchmark in the shuffle crate calls it through bench_support, and a wide_decimal multiply case has 48-digit products.

Benchmark results

These are local runs on an Apple M5 Max. They don't predict the Neoverse-N1 and x86-64-v3 builds that Comet releases. Both sides used -Ctarget-cpu=native and thin LTO, and they alternated within one run. The aligned layout adds -C llvm-args=-align-all-functions=7 -C llvm-args=-align-all-nofallthru-blocks=7 to both sides.

divide-once-local

Random hashes are negative half the time, and those rows no longer divide twice. Non-negative hashes never took the second division, so they have nothing to gain.

Could a reviewer run the same comparison on x86 with -Ctarget-cpu=x86-64-v3 and on Neoverse-N1 with -Ctarget-cpu=neoverse-n1? On both sides:

cd native
RUSTFLAGS="-Ctarget-cpu=x86-64-v3" cargo bench -p datafusion-comet-shuffle --bench pmod
RUSTFLAGS="-Ctarget-cpu=x86-64-v3" cargo bench -p datafusion-comet-spark-expr --bench wide_decimal -- '^wide_decimal/fused/multiply'

How are these changes tested?

test_pmod_edge_cases in comet_partitioning.rs checks pmod against i64::rem_euclid at the partition boundaries and at i32::MIN, i32::MAX, and -1. test_div_round_half_up_matches_reference in wide_decimal_binary_expr.rs checks div_round_half_up against the two-division version for divisors from 10 to 10^76, at exact halves and at the i128 and i256 bounds. Both tests pass on main too.

AI usage disclosure

I wrote this PR with help from Claude but understand and support the core approach, implementation, and test coverage.

@mbutrovich
mbutrovich marked this pull request as ready for review November 19, 2026 18:42
@github-actions github-actions Bot added enhancement New feature or request performance area:shuffle Shuffle (JVM and native) area:expressions Expression evaluation labels Nov 19, 2026

@comphead comphead 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.

Both rewrites look right to me. In pmod, a negative r lies in (-n, 0). In div_round_half_up, the callers only divide by 10^k, so quot * divisor stays within |value|. Two points inline.

A question on scope: n is fixed for the whole shuffle, and #6781 reports that the precomputed divisors from apache/arrow-rs#11421 made pmod 4.0x faster in the prototype. The i32 case doesn't need an arrow-rs release. Is that planned as a follow-up?

let r = hash % n;
let result = if r < 0 { (r + n) % n } else { r };
// A negative `r` is in `(-n, 0)`, so `r + n` is already in `(0, n)`.
let result = if r < 0 { r + n } else { r };

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.

i32::rem_euclid does the same with one division, and the new test already uses it as the oracle:

pub(crate) fn pmod(hash: u32, n: usize) -> usize {
    (hash as i32).rem_euclid(n as i32) as usize
}

For n > 0 it adds n.wrapping_abs() to a negative remainder, which equals r + n. In an aarch64 build (rustc 1.99.1, -O -C target-cpu=neoverse-n1), LLVM hoists that abs out of the loop. A local timing on an M-series Mac (8192 random hashes, 16 and 200 partitions) gave 3.62 us for both versions. I haven't checked x86.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, switched to rem_euclid. On x86-64-v3 the loop body matches the previous version apart from register names, with the abs hoisted out of the loop as on aarch64.

#7690 (comment)

Thanks, moved it next to pmod. Both multi_partition.rs sites call it, and bench_support re-exports it.

Comment thread native/shuffle/src/bench_support.rs Outdated
}

/// Maps each hash to its partition the way hash partitioning does.
pub fn hash_partition_ids(hashes: &[u32], num_partitions: usize, partition_ids: &mut [u32]) {

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.

This repeats the hash-to-partition loop in multi_partition.rs (lines 302-308 and 436-441 at this head). Those copies index partition_ids[idx] instead of zipping, so the benchmark measures a different loop from the one the shuffle runs. Moving this function next to pmod in comet_partitioning.rs as pub(crate), calling it from both multi_partition.rs sites, and re-exporting it here would make the benchmark track the production code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, moved it next to pmod. Both multi_partition.rs sites call it, and bench_support re-exports it.

@mbutrovich

Copy link
Copy Markdown
Contributor Author

The i32 case doesn't need an arrow-rs release. Is that planned as a follow-up?

The i32 divisor landed in arrow-rs as DivisorI32 (apache/arrow-rs#11467), so pmod will switch to it once Comet upgrades to an arrow release that includes it. We won't port it into Comet first, so Comet and arrow-rs keep one implementation. #7697 tracks it, along with the other Comet loops that divide by a fixed divisor.

@mbutrovich

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, @comphead!

@mbutrovich
mbutrovich requested a review from comphead November 19, 2026 22:05

@comphead comphead 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, great PR

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

Labels

area:expressions Expression evaluation area:shuffle Shuffle (JVM and native) enhancement New feature or request performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: divide once per row in pmod and div_round_half_up

2 participants