comphead commented on code in PR #7690:
URL: https://github.com/apache/datafusion-comet/pull/7690#discussion_r4634484373


##########
native/shuffle/src/bench_support.rs:
##########
@@ -119,3 +119,10 @@ impl BenchRepartitioner {
         self.inner.shuffle_write()
     }
 }
+
+/// 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]) {

Review 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.



##########
native/shuffle/src/comet_partitioning.rs:
##########
@@ -124,7 +124,8 @@ pub(crate) fn pmod(hash: u32, n: usize) -> usize {
     let hash = hash as i32;
     let n = n as i32;
     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 };

Review Comment:
   `i32::rem_euclid` does the same with one division, and the new test already 
uses it as the oracle:
   
   ```rust
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to