mbutrovich commented on code in PR #7658:
URL: https://github.com/apache/datafusion-comet/pull/7658#discussion_r4634373914


##########
native/spark-expr/src/math_funcs/internal/checkoverflow.rs:
##########
@@ -249,6 +248,38 @@ impl PhysicalExpr for CheckOverflow {
     }
 }
 
+/// Values per chunk in the no-null scan of [`fits_precision`].
+const FITS_PRECISION_CHUNK: usize = 64;
+
+/// Whether every non-null value of `array` fits `precision`, the check that
+/// `Decimal128Type::is_valid_decimal_precision` makes for one value.
+#[inline]
+fn fits_precision(array: &Decimal128Array, precision: u8) -> bool {
+    let bounds = (
+        MIN_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+        MAX_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+    );
+    match bounds {
+        (Some(&min), Some(&max)) if array.null_count() == 0 => {
+            // Without nulls every slot holds a value, so scan the values 
buffer directly instead
+            // of reading the null bitmap per slot. Shifting by `min` turns 
the two bounds checks
+            // into one unsigned comparison. The check is branchless within a 
chunk, which lets
+            // the compiler unroll it, and the scan still stops at the first 
chunk that holds an
+            // overflow.
+            let range = max.wrapping_sub(min) as u128;
+            array.values().chunks(FITS_PRECISION_CHUNK).all(|chunk| {
+                chunk.iter().fold(true, |fits, &v| {
+                    fits & (v.wrapping_sub(min) as u128 <= range)
+                })
+            })

Review Comment:
   > I cross-compiled this PR and `main` for `x86_64-apple-darwin` with the 
`release` profile and LTO off, which is how `libcomet` builds 
(apache/arrow-rs#12326).
   
   The released binary has the same loop. The linux/amd64 `libcomet.so` in the 
1.2.0 jar is built by `make core-amd64-libs` with `-Ctarget-cpu=x86-64-v3`. In 
it, `CheckOverflow::evaluate` calls `is_valid_decimal_precision` through the 
GOT on every value of the no-overflow scan:
   
   ```asm
   mov   rax, qword ptr [rsp + 0x8]        # reload the array from the stack
   mov   rax, qword ptr [rax + 0x20]       # its values pointer
   mov   rdi, qword ptr [rax + r12]        # low half of v
   mov   rsi, qword ptr [rax + r12 + 0x8]  # high half
   mov   edx, dword ptr [rsp + 0x10]       # reload the precision from the stack
   call  qword ptr [rip + 0x4bff5e0]       # GOT slot for 
is_valid_decimal_precision
   ```
   
   That loop is 14 instructions, and the callee is 18 on the path for a value 
that fits, so 32 per value against this PR's 12. The 21 in my comment came from 
a darwin build, which keeps frame pointers. The linux/aarch64 `libcomet.so` 
makes the same call with a `bl` on every value.
   
   Built with the release flags, this PR's scan is the same 12-instruction loop 
with `-Ctarget-cpu=x86-64-v3`, and 9 or 10 instructions with 
`-Ctarget-cpu=neoverse-n1`, with no calls or stack traffic.



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