Repository navigation
perf: divide once per row in pmod and div_round_half_up - #7690
mbutrovich wants to merge 2 commits into
Conversation
comphead
left a comment
There was a problem hiding this comment.
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 }; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thanks, moved it next to pmod. Both multi_partition.rs sites call it, and bench_support re-exports it.
| } | ||
|
|
||
| /// 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]) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks, moved it next to pmod. Both multi_partition.rs sites call it, and bench_support re-exports it.
The |
|
Thanks for the feedback, @comphead! |
Which issue does this PR close?
Closes #6781.
Rationale for this change
See #6781.
What changes are included in this PR?
pmodusesi32::rem_euclid, which addsnto a negative remainder instead of dividing again.div_round_half_upcomputes the remainder from the quotient instead of with a secondi256division.The hash-to-partition loop moves from the two sites in
multi_partition.rsintohash_partition_idsnext topmod. A newpmodbenchmark in the shuffle crate calls it throughbench_support, and awide_decimalmultiply 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=nativeand 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=7to both sides.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-v3and on Neoverse-N1 with-Ctarget-cpu=neoverse-n1? On both sides:How are these changes tested?
test_pmod_edge_casesincomet_partitioning.rscheckspmodagainsti64::rem_euclidat the partition boundaries and ati32::MIN,i32::MAX, and -1.test_div_round_half_up_matches_referenceinwide_decimal_binary_expr.rschecksdiv_round_half_upagainst the two-division version for divisors from 10 to 10^76, at exact halves and at thei128andi256bounds. Both tests pass onmaintoo.AI usage disclosure
I wrote this PR with help from Claude but understand and support the core approach, implementation, and test coverage.